Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4.  
  5. bool dfs(int v, list<int>* graph, int * visited, bool & DRAW, list <int> & win_result)
  6. {
  7.     // VISITED:
  8.     //0 - nieodwiedzony
  9.     //1 - odwiedzony w obecnym wywolaniu
  10.     //2 - odwiedzony w poprzednim wywolaniu
  11.  
  12.     visited[v]=1;
  13.     win_result.push_back(v/2);
  14.     if (graph[v].empty() && v%2==0) //brak sasiadow i zielony wierzcholek
  15.         return true;
  16.  
  17.     for (int u: graph[v])
  18.         if (visited[u]==1) //cykl
  19.             DRAW=true;
  20.         else
  21.             if (visited[u]==0 && dfs(u,graph,visited,DRAW,win_result))
  22.                 return true;
  23.  
  24.     visited[v]=2;
  25.     win_result.pop_back();
  26.     return false;
  27. }
  28.  
  29. int main()
  30. {
  31.     ios_base::sync_with_stdio(false);
  32.     int t,n,m,x,y,s;
  33.     bool DRAW;
  34.     list<int> * graph;
  35.     int * visited;
  36.     list <int> win_result;
  37.  
  38.     cin >> t;
  39.     for (int i=0;i<t;i++)
  40.     {
  41.         win_result.clear();
  42.         DRAW=false;
  43.         cin >> n >> m >> s;
  44.  
  45.         graph=new list<int>[2*n]; // zielony wierzcholek i jest pod indeksem 2*i, czerwony pod indeksem 2*i+1
  46.         visited=new int[2*n];
  47.         for (int j=0;j<2*n;j++)
  48.             visited[j]=0;
  49.  
  50.         for (int j=0;j<m;j++)
  51.         {
  52.             cin >> x >> y;
  53.             graph[2*x].push_back(2*y+1); // zielony x -> czerwonego y
  54.             graph[2*x+1].push_back(2*y); // czerwony x -> zielony y
  55.         }
  56.  
  57.         if (dfs(2*s,graph,visited,DRAW,win_result)) // zaczynam od zielonego s
  58.         {
  59.             cout << "TAK" << endl;
  60.             for (int k: win_result)
  61.                 cout << k <<" ";
  62.             cout << endl;
  63.         }
  64.         else
  65.             if (DRAW)
  66.                 cout << "REMIS" << endl;
  67.             else
  68.                 cout << "NIE" << endl;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement