Advertisement
aurko96

Codeforce - 117C

Nov 15th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int>edge[5005];
  4. int vis[50000];
  5. int cycle;
  6. void dfs(int node,int pre,int pre_pre,int pre_pre_pre)
  7. {
  8.     if(cycle) return;
  9.     if(vis[node]!=0)
  10.     {
  11.         if(vis[node]==vis[pre] && vis[node]!=vis[pre_pre] && node==pre_pre_pre)
  12.         {
  13.             cycle=1;
  14.             cout<<node<<" "<<pre_pre<<" "<<pre;
  15.             return;
  16.         }
  17.         else return;
  18.     }
  19.     else if(vis[node]==0)
  20.     {
  21.         if(vis[pre]==1) vis[node]=2;
  22.         else if(vis[pre]==2) vis[node]=1;
  23.         else vis[node]=1;
  24.     }
  25.     for(int i=0;i<edge[node].size();i++)
  26.     {
  27.         int x=edge[node][i];
  28.         if(x==pre) continue;
  29.         dfs(x,node,pre,pre_pre);
  30.     }
  31. }
  32. int main()
  33. {
  34.     ios_base::sync_with_stdio(0);
  35.     cin.tie(nullptr);
  36.     int i,j,n,x,y,z;
  37.     string mat[5005];
  38.     cin>>n;
  39.     for(i=0;i<n;i++)
  40.     {
  41.         cin>>mat[i];
  42.     }
  43.     for(i=0;i<n;i++)
  44.     {
  45.         for(j=0;j<n;j++)
  46.         {
  47.             if(mat[i][j]=='1')
  48.             {
  49.                 edge[i+1].push_back(j+1);
  50.             }
  51.         }
  52.     }
  53.     if(n>500)
  54.     {
  55.         for(i=1;i<=n;i++)
  56.         {
  57.             if(vis[i]==0)
  58.             {
  59.                 dfs(i,0,0,0);
  60.             }
  61.             if(cycle) break;
  62.         }
  63.     }
  64.     else
  65.     {
  66.         for(i=1;i<=n;i++)
  67.         {
  68.             memset(vis,0,sizeof(vis));
  69.             dfs(i,0,0,0);
  70.             if(cycle) break;
  71.         }
  72.     }
  73.  
  74.     if(!cycle) cout<<-1;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement