求助大佬 只过了4个样例,实在想不到哪里出错了
查看原帖
求助大佬 只过了4个样例,实在想不到哪里出错了
645942
arival楼主2022/11/5 11:26

不知道哪错了 我的思路是计入一个节点的敌人和朋友数 敌人数 不为0 就 ans+1 , 碰到 根节点也ans+1

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int d[N], p[N], cnt[N], bnt[N];
int n, m;
int find(int x)
{
	if (p[x] != x)
	{
		int t = p[x];
		p[x] = find(p[x]);
		d[x] += d[t];
	}
	return p[x];
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		p[i] = i;
		cnt[i] = 0;
		bnt[i] = 0;
	}
	while (m--)
	{
		char op;
		int x, y;
		cin >> op >> x >> y;
		int ix = find(x), iy = find(y);
		if (op == 'E')
		{
			if (ix != iy)
			{
				p[ix] = iy;
				d[ix] = 1 + d[y] - d[x];
				bnt[iy] = bnt[iy] + cnt[ix]+ 1;
				cnt[iy] = cnt[iy] + bnt[ix];
			}
		}
		else
		{
			if (ix != iy)
			{
				p[ix] = iy;
				d[ix] = d[y] - d[x];
				cnt[iy] = cnt[ix] + cnt[iy] + 1;
				bnt[iy] = bnt[iy] + bnt[ix];
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		if (p[i] == i)
		{
			ans++;
			cout << i << endl;
		}
	}
	cout << ans << '\n';
	return 0;
}
2022/11/5 11:26
加载中...