Advertisement
Guest User

Untitled

a guest
Nov 26th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. typedef long long ll;
  4.  
  5. using namespace std;
  6.  
  7. ll tests=0, keycount=0, hintcount=0;
  8. int zeros=0;
  9.  
  10. int d[10002];
  11. int c[10002]; // for cycles
  12.  
  13. vector<int> k[10002];
  14. queue<int> order;
  15. vector<int> sequence;
  16.  
  17. stack<int> unvisited;
  18. set<int> visiting;
  19. set<int> visited;
  20.  
  21. void doTopo(){
  22.     int maxQueueSize=0;
  23.  
  24.     int tmp_keys=keycount;
  25.  
  26.     while(!order.empty()){
  27.         int cur = order.front();
  28.         order.pop();
  29.         tmp_keys--;
  30.         sequence.push_back(cur);
  31.  
  32.         for(int i=0;i<k[cur].size();i++){
  33.             d[k[cur][i]]--;
  34.             if(d[k[cur][i]]==0){
  35.                 order.push(k[cur][i]);
  36.             }
  37.         }
  38.         maxQueueSize=max(maxQueueSize,(int)order.size());
  39.  
  40.     }
  41.  
  42.     if(zeros==1 && tmp_keys==0 && maxQueueSize==1){
  43.         for(int i=0;i<sequence.size();i++){
  44.             cout << sequence[i] << " ";
  45.         }
  46.     }else{
  47.         cout << "missing hints";
  48.     }
  49.  
  50. }
  51.  
  52. bool dfs(int v){
  53.  
  54.     visiting.insert(v);
  55.  
  56.     for(int c=0;c<k[v].size();c++){
  57.         int child = k[v][c];
  58.         if(visited.find(child)!=visited.end())continue;
  59.         if(visiting.find(child)!=visiting.end())return true;
  60.         if(dfs(child))return true;
  61.     }
  62.     visiting.erase(v);
  63.     visited.insert(v);
  64.  
  65.     return false;
  66. }
  67.  
  68. bool findCycle(){
  69.  
  70.     while(!unvisited.empty()){
  71.         int cur = unvisited.top();
  72.         unvisited.pop();
  73.  
  74.         if(dfs(cur)){
  75.             return true;
  76.         }
  77.  
  78.     }
  79.     return false;
  80. }
  81.  
  82. int main()
  83. {
  84.     ifstream cin("data.in");
  85.  
  86.     cin >> tests;
  87.  
  88.     for(int te=0;te<tests;te++){
  89.  
  90.         keycount=0;
  91.         hintcount=0;
  92.         sequence.clear();
  93.         for(int i=0;i<=10002;i++){
  94.             d[i]=0;
  95.             c[i]=-1;
  96.             k[i].clear();
  97.         }
  98.         while(!order.empty()){order.pop();}
  99.         while(!unvisited.empty()){unvisited.pop();}
  100.         visiting.clear();
  101.         visited.clear();
  102.  
  103.         cin >> keycount >> hintcount;
  104.  
  105.         if(hintcount == 0){
  106.             cout << "missing hints";
  107.             continue;
  108.         }
  109.  
  110.         for(int i=1;i<=keycount;i++){
  111.             unvisited.push(i);
  112.         }
  113.  
  114.         for(int i=0;i<hintcount;i++){
  115.             int a,b;
  116.             cin >> a >> b; // a comes before b
  117.             d[b]+=1;
  118.             k[a].push_back(b);
  119.         }
  120.  
  121.         zeros = 0;
  122.  
  123.         for(int i=1;i<=keycount;i++){
  124.             if(d[i]==0){
  125.                 zeros+=1;
  126.                 order.push(i);
  127.             }
  128.         }
  129.  
  130.  
  131.         if(findCycle()){
  132.             cout << "recheck hints";
  133.         }else{
  134.             doTopo();
  135.         }
  136.  
  137.         if(te!=tests-1) cout << "\n";
  138.  
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement