2 条题解

  • 1
    @ 2026-8-3 9:37:58
    #include<bits/stdc++.h>
    using namespace std;
    const int N=2e5+10;
    int fa[N];
    int find(int x)
    {
    	if(fa[x]==x)return x;
    	return fa[x]=find(fa[x]);
    }
    void he(int x,int y)
    {
    	fa[find(x)]=find(y);
    }
    int main()
    {
    	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);//加速!! 
    	int n,m;cin>>n>>m;
    	for(int i=1;i<=n;i++)fa[i]=i;
    	while(m--)
    	{
    		int z,x,y;cin>>z>>x>>y;
    		if(z==0)he(x,y);
    		else
    		{
    			if(find(x)==find(y))puts("1");
    			else puts("0");
    		}
    	}
    }
    
    • 1
      @ 2025-12-7 9:31:40
      #include<bits/stdc++.h>
      using namespace std;
      typedef long long ll;
      int n,q,fa[200010];
      int find(int x){
      	return fa[x]=(fa[x]==x?x:find(fa[x]));
      }
      int main(){
      	ios::sync_with_stdio(0);
      	cin.tie(0);
      	cin>>n>>q;
      	for(int i=1;i<=n;i++)fa[i]=i;
      	while(q--){
      		int op,x,y;
      		cin>>op>>x>>y;
      		if(op==0){
      			fa[find(x)]=find(y);
      		}
      		else{
      			if(find(x)!=find(y))cout<<"0\n";
      			else cout<<"1\n";
      		}
      	}
      	return 0;
      }
      
      
      • 1

      信息

      ID
      8120
      时间
      1000ms
      内存
      1024MiB
      难度
      6
      标签
      递交数
      52
      已通过
      17
      上传者