求大佬帮忙看看,错哪里了?
查看原帖
求大佬帮忙看看,错哪里了?
338117
CWJ1029楼主2023/4/22 20:09

代码如下

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;

int m , f[30005] , d[30005] , sz[30005];

int find(int x)
{
	if (f[x] == x) return x;
	int fa = find(f[x]);
	d[x] += d[f[x]];
	return f[x] = fa;
}

void link(int x , int y)
{
	x = find(x);
	y = find(y);
	if (x == y) return;
	d[x] = sz[y];
	sz[y] += sz[x];
	f[x] = y;
}

int main()
{
	scanf("%d" , &m);
	for (int i = 1; i <= 30000; i++)
	{
		f[i] = i;
		d[i] = 0;
		sz[i] = 1;
	}
	for (int i = 1; i <= m; i++)
	{
		int x , y;
		char op;
		cin >> op >> x;
		if (op == 'M')
		{
			scanf("%d" , &y);
			link(x , y);
		}
		if (op == 'C')
		{
			x = find(x);
			printf("%d\n" , d[x]);
		}
	}
	
	return 0;
}
2023/4/22 20:09
加载中...