SuitNdtie

Include Prog 1057

Apr 18th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<vector>
  3. #include<queue>
  4. using namespace std;
  5.  
  6. int main(){
  7.     int n;
  8.     scanf("%d",&n);
  9.    
  10.     vector<int> adj[n+1];
  11.     for(int i=1;i<=n;i++){
  12.         int k;
  13.         scanf("%d",&k);
  14.         for(int j=1;j<=k;j++){
  15.             int v;
  16.             scanf("%d",&v);
  17.             adj[i].push_back(v);
  18.         }
  19.     }
  20.    
  21.     for(int i=1;i<=n;i++){
  22.         bool visited[n+1];for(int j=0;j<=n;j++)visited[j] = false;
  23.         queue<int> q;
  24.         q.push(i);
  25.        
  26.         bool check = false;
  27.         while(!q.empty() && !check){
  28.             int u = q.front();
  29.             q.pop();
  30.             if(visited[u]){
  31.                 check = true;
  32.                 continue;
  33.             }
  34.             visited[u] = true;
  35.             for(int j=0;j<adj[u].size();j++){
  36.                 q.push(adj[u][j]);
  37.             }
  38.         }
  39.         printf("%s\n",(check ? "YES" : "NO"));
  40.         while(!q.empty())q.pop();
  41.     }
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment