#include <bits/stdc++.h> using namespace std; map<int, int> f; int find(int a) { if (f[a] != a) return f[a] = find(f[a]); else return a; } int hb(int a, int b) { f[find(b)] = find(a); } int main() { int n, m, p, q; cin >> n >> m >> p >> q; for (int i = -m; i <= n; i++) { if (i == 0) continue; f[i] = i; } f[-1] = f[1]; int x, y; while (p--) { cin >> x >> y; hb(x, y); } while (q--) { cin >> x >> y; hb(x, y); } int girl = 0, boy = 0; for (int i = -m; i < 0; i++) { if (find(i) == 1) girl++; } for (int i = 1; i <= n; i++) { if (find(i) == 1) boy++; } cout << min(boy, girl); return 0; }换成c语言
时间: 2024-01-10 16:02:58 浏览: 129
这是将C++代码转换为C语言代码的结果:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 100010
int f[MAXN];
int find(int a) {
if (f[a] != a)
return f[a] = find(f[a]);
else
return a;
}
void hb(int a, int b) {
f[find(b)] = find(a);
}
int main() {
int n, m, p, q;
scanf("%d %d %d %d", &n, &m, &p, &q);
for (int i = -m; i <= n; i++) {
if (i == 0) continue;
f[i] = i;
}
f[-1] = f[1];
int x, y;
while (p--) {
scanf("%d %d", &x, &y);
hb(x, y);
}
while (q--) {
scanf("%d %d", &x, &y);
hb(x, y);
}
int girl = 0, boy = 0;
for (int i = -m; i < 0; i++) {
if (find(i) == 1) girl++;
}
for (int i = 1; i <= n; i++) {
if (find(i) == 1) boy++;
}
printf("%d", boy < girl ? boy : girl);
return 0;
}
```
主要的改动包括:
- 使用头文件`<stdio.h>`和`<stdlib.h>`代替`<bits/stdc++.h>`;
- 将`cin`和`cout`改成`scanf`和`printf`;
- 将`map`改成一维数组`f`;
- 去掉`using namespace std`;
- 修改`int hb(int a, int b)`函数为`void hb(int a, int b)`函数;
- 修改`min(boy, girl)`为`boy < girl ? boy : girl`。
阅读全文