Advertisement
simushin_pavel

Untitled

Dec 21st, 2020
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <utility>
  5. using namespace std;
  6. const long long SIZE=1005;
  7. long long table[SIZE];
  8. int main(){
  9.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  10.     long long d, f, i, p, mx=0, cur, j; cin >> d >> f;
  11.     vector<pair<string, long long>> files(d+5);
  12.     vector<bool> used(f+5, false);
  13.     string filename, ans;
  14.     for(i=1; i<=d; ++i){
  15.         cin >> filename >> p;
  16.         files[i]={filename, p};
  17.     }
  18.     for(i=1; i<=f; ++i){
  19.         cin >> p;
  20.         table[i]=p;
  21.     }
  22.     for(i=1; i<=d; ++i){
  23.         cur=0;
  24.         used=vector<bool>(f+5, false);
  25.         for(j=files[i].second; j!=-1; j=table[j], ++cur){
  26.             if(used[j]){
  27.                  cout << files[i].first << " CYCLE";
  28.                  return 0;
  29.             }
  30.             if(!j){
  31.                 cout << files[i].first << " BAD";
  32.                 return 0;
  33.             }
  34.             used[j]=true;
  35.         }
  36.         if(cur>mx){
  37.             mx=cur;
  38.             ans=files[i].first;
  39.         }
  40.     }
  41.     cout << ans;
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement