YEZAELP

SMMR-145: Cultist

Jun 7th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. vector <int> g[100001];
  4. int color[100001];
  5. bool dfs(int u,int c){
  6.     if(color[u]!=0)
  7.         return true;
  8.     color[u]=c;
  9.     for(auto v:g[u]){
  10.         if(color[v]==0){
  11.             dfs(v,c);
  12.         }
  13.     }
  14. }
  15. int main(){
  16.     int n,m;
  17.     scanf("%d %d",&n,&m);
  18.     for(int i=1;i<=m;i++){
  19.         int u,v;
  20.         scanf("%d %d",&u,&v);
  21.         g[u].push_back(v);
  22.         g[v].push_back(u);
  23.     }
  24.     int c=1;
  25.     for(int i=1;i<=n;i++){
  26.         if(color[i]==0){
  27.             dfs(i,c);
  28.             c++;
  29.         }
  30.     }
  31.     int q;
  32.     scanf("%d",&q);
  33.     for(int i=0;i<q;i++){
  34.         int a,b;
  35.         scanf("%d %d",&a,&b);
  36.         if(color[a]==color[b]) printf("Yes");
  37.         else printf("No");
  38.         printf("\n");
  39.     }
  40.     return 0;
  41. }
Add Comment
Please, Sign In to add comment