解题思路:
注意事项:
参考代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e6+10;
int n, m;
int p[N];
int findth(int x)
{
if (x == p[x]) return x;
return p[x] = findth(p[x]);
}
int unionn(int x, int y)
{
int xx = findth(x);
int yy = findth(y);
if (xx != yy) {
p[yy] = xx;
return 1;
}
return 0;
}
int main()
{
scanf("%d %d", &n, &m);
for (int i = 1;i <= n * m;i++) p[i] = i;
int x1, y1, x2, y2;
while (scanf("%d %d %d %d", &x1, &y1, &x2, &y2) == 4) {
unionn((x1 - 1)*m + y1, (x2 - 1)*m + y2);
}
int sum = 0;
for (int i = 1;i <= m;i++) {
for (int j = 1;j < n;j++) {
if (unionn((j - 1)*m + i, j*m + i))
sum++;
}
}
for (int i = 1;i <= n;i++) {
for (int j = 1;j < m;j++) {
if (unionn((i - 1)*m + j, (i - 1)*m + j + 1) ) sum += 2;
}
}
printf("%d\n", sum);
return 0;
}
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题6.11 (C语言代码)浏览:566 |
WU-整除问题 (C++代码)浏览:648 |
Cylinder (C语言描述,蓝桥杯)浏览:1279 |
1908题解浏览:680 |
Cylinder (C语言描述+详细分析)浏览:3379 |
数对 (C语言代码)浏览:762 |
数组与指针的问题浏览:760 |
简单的a+b (C语言代码)浏览:572 |
单词个数统计 (C语言代码)浏览:1046 |
C语言程序设计教程(第三版)课后习题10.7 (用指针求解)浏览:1542 |