Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int solve(vector <int> celeb, int n){
- int result = -1, count, flagCeleb;
- for(int i = 1; i <= n; i++){
- vector <bool> flag(n + 1, false);
- count = 0;
- int temp = i;
- while(!flag[celeb[temp]]){
- flag[celeb[temp]] = true;
- temp = celeb[temp];
- count++;
- if(count > result){
- result = count;
- flagCeleb = i;
- }
- }
- }
- return flagCeleb;
- }
- int main(){
- int n, a, b;
- vector <int> celeb;
- while(cin >> n){
- celeb.resize(n + 1);
- for(int i = 0; i < n; i++){
- cin >> a >> b;
- celeb[a] = b;
- }
- cout << solve(celeb, n) << endl;
- celeb.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement