玄学提问
查看原帖
玄学提问
353264
RainbowSheep_楼主2024/11/25 21:59

为什么merge写s[x]=find(y);会20pts wa 而s[find(x)]=find(y);就能ac? 附代码:

//https://www.luogu.com.cn/problem/P3367
#include <iostream>
using namespace std;

#define MAX 10010

int s[MAX],n,m,op,x,y;

int find(int x)
{
	if(s[x]!=x)
		s[x]=find(s[x]);
	return s[x];
}

inline void merge(int x,int y)
{
	s[find(x)]=find(y);
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for(int i = 1;i<=n;i++)
		s[i]=i;

	while(m--)
	{
		cin >> op >> x >> y;
		switch(op)
		{
		case 1:
			merge(x,y);
			break;
		default:
			cout << (find(x)==find(y)?'Y':'N') << endl;
		}
	}
	return 0;
}
2024/11/25 21:59
加载中...