You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
2 years ago
const int N = 1000 * 1000 + 10, M = N << 1;
2 years ago
int n, m;
// 二维转一维的办法,坐标从(1,1)开始
2 years ago
int get(int x, int y) {
2 years ago
// m为列宽
return (x - 1) * m + y;
}
struct Edge {
2 years ago
int a, b, c;
2 years ago
const bool operator<(const Edge &t) const {
2 years ago
return c < t.c;
2 years ago
}
2 years ago
} edge[M];
2 years ago
int el;
// 并查集
int p[N];
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
// 先连1的边再连2的边
void create_edges() {
2 years ago
for (int i = 1; i <= (n - 1) * m; i++) // 前n-1行的每个点都可以向下引一条边权为1的边
edge[el++] = {i, i + m, 1}; // i -> i+m,边权为1
2 years ago
2 years ago
for (int i = 1; i <= n * m; i++) { // 向右引边,注意最后一列不能向右引边
if (i % m == 0) continue; // 最后一列放过
edge[el++] = {i, i + 1, 2}; // i->i+1,边权为2
2 years ago
}
// 因为加进去就是按边权由小到大录入的,所以不用再排序了
}
int main() {
cin >> n >> m;
// 建边
create_edges();
// 并查集初始化
for (int i = 1; i <= n * m; i++) p[i] = i;
int x1, y1, x2, y2;
// 利用二维转一维办法,将(x,y)映射成节点编号
// 某些点之间已经有连线了=> 标识这些点已在并查集中
while (cin >> x1 >> y1 >> x2 >> y2) {
int a = get(x1, y1), b = get(x2, y2);
p[find(a)] = find(b);
}
int res = 0; // 用Kruskal算法即可
for (int i = 0; i < el; i++) {
2 years ago
int a = find(edge[i].a), b = find(edge[i].b), c = edge[i].c;
if (a != b) p[a] = b, res += c;
2 years ago
}
2 years ago
cout << res << endl;
2 years ago
return 0;
}