Advertisement
mickypinata

TOI14: Technology

Nov 20th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. enum checkStatus{
  5.     unchecked = 0,
  6.     checking = 1,
  7.     checked = 2
  8. };
  9.  
  10. const int N = 1e5;
  11. const int K = 1e4;
  12.  
  13. vector<int> adj[N + 10];
  14. vector<int> req[K + 10];
  15. vector<checkStatus> sts(N + 10, unchecked);
  16. int nTech, mxLv, limTime, currTime;
  17. bool cycle;
  18.  
  19. void DFSInvent(int u){
  20.     if(sts[u] == checked){
  21.         return;
  22.     }
  23.     sts[u] = checking;
  24.     for(int v : adj[u]){
  25.         if(cycle || currTime > limTime){
  26.             return;
  27.         }
  28.         if(sts[v] == unchecked){ // Invent required tech
  29.             DFSInvent(v);
  30.         } else if(sts[v] == checking){ // Found cycle
  31.             cycle = true;
  32.             return;
  33.         }
  34.     }
  35.     ++currTime;
  36.     sts[u] = checked;
  37.     return;
  38. }
  39.  
  40. int main(){
  41.  
  42.     scanf("%d %d %d", &nTech, &mxLv, &limTime);
  43.     for(int i = 1; i <= nTech; ++i){
  44.         int techLv, nPrev;
  45.         scanf("%d %d", &techLv, &nPrev);
  46.         req[techLv].push_back(i);
  47.         for(int j = 1; j <= nPrev; ++j){
  48.             int prev;
  49.             scanf("%d", &prev);
  50.             adj[i].push_back(prev);
  51.         }
  52.     }
  53.  
  54.     currTime = 0;
  55.     cycle = false;
  56.     int currLv = 0;
  57.     for(int i = 1; i <= mxLv; ++i){
  58.         for(int tech : req[i]){
  59.             if(cycle || currTime > limTime){
  60.                 break;
  61.             }
  62.             DFSInvent(tech);
  63.         }
  64.         if(cycle || currTime > limTime){
  65.             break;
  66.         }
  67.         ++currLv;
  68.     }
  69.  
  70.     if(currLv == 0){
  71.         cout << "-1";
  72.     } else {
  73.         cout << currLv;
  74.     }
  75.  
  76.     return 0;
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement