Advertisement
Infernale

Get Signature [NCTU Floor 3]

Dec 18th, 2019
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int solve(vector <int> celeb, int n){
  6.     int result = -1, count, flagCeleb;
  7.     for(int i = 1; i <= n; i++){
  8.         vector <bool> flag(n + 1, false);
  9.         count = 0;
  10.         int temp = i;
  11.         while(!flag[celeb[temp]]){
  12.             flag[celeb[temp]] = true;
  13.             temp = celeb[temp];
  14.             count++;
  15.             if(count > result){
  16.                 result = count;
  17.                 flagCeleb = i;
  18.             }
  19.         }
  20.     }
  21.     return flagCeleb;
  22. }
  23.  
  24. int main(){
  25.     int n, a, b;
  26.     vector <int> celeb;
  27.     while(cin >> n){
  28.         celeb.resize(n + 1);
  29.         for(int i = 0; i < n; i++){
  30.             cin >> a >> b;
  31.             celeb[a] = b;
  32.         }
  33.         cout << solve(celeb, n) << endl;
  34.         celeb.clear();
  35.     }
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement