2 条题解

  • 2
    @ 2021-10-16 15:59:27
    /*****************************************
    备注:
    ******************************************/
    #include <queue>
    #include <math.h>
    #include <stack>
    #include <stdio.h>
    #include <iostream>
    #include <vector>
    #include <iomanip>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    #define LL long long
    const int N = 2e5 + 10;
    const int INF = 0x3f3f3f3f;
    int a[N];
    void init(int n)
    {
    	for(int i = 0 ; i <= n ; i++)
    		a[i] = i;
    }
    int fa(int x)
    {
    	if(x == a[x])
    		return x;
    	return a[x] = fa(a[x]);
    }
    inline int read()
    {
    	char ch = getchar();
    	while(ch > '9' || ch < '0')
    		ch = getchar();
    	int sum = 0;
    	while(ch <= '9' && ch >= '0')
    	{
    		sum = (sum << 3) + (sum << 1) + ch - '0';
    		ch = getchar();
    	}
    	return sum;
    }
    int main()
    {
    	int n , m;
    
    	n = read() , m = read();
    	init(n);
    	while(m--)
    	{
    		int x,y;
    		x = read() , y =read();
    		x = fa(x) , y= fa(y);
    		a[x] = y;
    	}
    	int t;
    	t = read();
    	while(t--)
    	{
    		int x, y;
    		x = read(); y = read();
    		x = fa(x);
    		y = fa(y);
    		if(x != y)	cout << "No\n";
    		else cout << "Yes\n";
    	}
    	return 0;
    }
    
    • 0
      @ 2026-10-26 17:08:22

      #include<bits/stdc++.h> using namespace std; int a[1010100],b[11011]; int in[101010],ans; int fa[1010101]; int n,m; int u,v,q; int find(int x){ if(x==fa[x]) return x; return fa[x]=find(fa[x]); } void uni(int u,int v){ fa[u]=v; } int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ fa[i]=i; } while(m--){ cin>>u>>v; u=find(u); v=find(v); uni(u,v); } for(int i=1;i<=n;i++){ find(i); } cin>>q; while(q--){ cin>>u>>v; if(fa[u]==fa[v]){ cout<<"Yes"<<endl; }else{ cout<<"No"<<endl; } } return 0;

      1. }
      • 1

      信息

      ID
      1314
      时间
      1000ms
      内存
      256MiB
      难度
      6
      标签
      递交数
      276
      已通过
      91
      上传者