80WA求助
  • 板块P2078 朋友
  • 楼主MH_SLC
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/7/18 09:17
  • 上次更新2023/10/27 19:47:04
查看原帖
80WA求助
352536
MH_SLC楼主2022/7/18 09:17
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
const int MAX = 30010;
inline int read() {
	int x = 0;
	char c = getchar();
	while (c < '0' || c > '9') c = getchar();
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x;
}
int vis[MAX], n = read(), m = read(), p = read(), q = read(), x, y, ans1, ans2;
int find(int x) {
	return vis[x] == x ? x : vis[x] = find(vis[x]);
}
void merge(int x, int y) {
	if (find(x) != find(y))vis[find(x)] = find(y);
}
int main() {
	for (int i = 1; i <= 2 * n; i++)vis[i] = i;
	for (int i = 1; i <= p; i++) {
		x = read(), y = read();
		if (find(x) != find(y))merge(x, y);
	}
	for (int i = 1; i <= q; i++) {
		x = read(), y = read();
		if (find(abs(x) + n) != find(abs(y) + n))merge(abs(x) + n, abs(y) + n);
	}
	for (int i = 1; i <= n; i++)
		if (find(i) == find(1))ans1++;
	for (int i = n + 1; i <= m + n; i++)
		if (find(i) == find(n + 1))ans2++;
	cout << min(ans1, ans2);
	return 0;
}

80分,wa了

2022/7/18 09:17
加载中...