Advertisement
Guest User

Untitled

a guest
Sep 28th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. int c[5000], pi[5000], a[3], n;
  6. string graph[5000];
  7. vector<int> res;
  8.  
  9. bool DFS(int v){
  10.     c[v]=1;
  11.     bool flag=false;
  12.     for(int i=0;i<n;i++)
  13.         if(graph[v][i]=='1' && c[i]==0){
  14.             pi[i]=v;
  15.             flag=flag || DFS(i);
  16.         } else if(graph[v][i]=='1' && c[i]==1){
  17.             if(pi[v]!=-1 && pi[pi[v]]!=-1 && pi[pi[v]]==i){
  18.                 res.push_back(i);
  19.                 res.push_back(pi[v]);
  20.                 res.push_back(v);
  21.                 return true;
  22.             }
  23.         }
  24.         return flag;
  25. }
  26.  
  27. int main()
  28. {
  29.     cin>>n;
  30.     for(int i=0;i<n;i++){
  31.         cin>>graph[i];
  32.         c[i]=0;
  33.         pi[i]=0;
  34.     }
  35.     if(DFS(0))
  36.         cout<<res[0]+1<<' '<<res[1]+1<<' '<<res[2]+1;
  37.     else
  38.         cout<<-1;
  39.     return 0;
  40. }
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement