厌氧代码求调
查看原帖
厌氧代码求调
965334
1_1_1_1_1_1_楼主2023/8/26 11:02

吸了氧还是超时3个

#include<bits/stdc++.h>

using namespace std;

int n, m;

int p[10001];

int find(int t)
{
	int y;
	
	y = p[t];
	while(p[y] != y)
	{
		y = p[y];
	}
	return(y);
}

void Union(int x, int y)
{
	int f1, f2;
	
	f1 = find(x);
	f2 = find(y);
	p[f1] = f2;
}

int main()
{
	int i;
	int num;
	int x, y;
	int f1, f2;
	
	cin >> n >> m;
	for(i = 1; i <= n; i++)
	{
		p[i] = i;
	}
	for(i = 0; i < m; i++)
	{
		cin >> num >> x >> y;
		if(num == 2)
		{
			f1 = find(x);
			f2 = find(y);
			if(f1 == f2)
			{
				cout << 'Y';
			}
			else
			{
				cout << 'N';
			}
			cout << endl;
		}
		else
		{
			Union(x, y);
		}
	}
}
2023/8/26 11:02
加载中...