Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #define loop(i,from,to) for (int i = from; i < to; ++i)
  2. #include <bits/stdc++.h>
  3.  
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. using std::vector;
  8. using std::string;
  9. using std::map;
  10. using std::pair;
  11.  
  12. const int INF = 1e9+7;
  13. const double eps = 1e-6;
  14.  
  15.  
  16. signed main() {
  17. std::ios::sync_with_stdio(false);
  18. cin.tie(0);
  19. int n; cin >> n;
  20. int a, b; cin >> a >> b;
  21. vector<int> parents(n + 1);
  22. parents[1] = 1;
  23. for (int i = 2; i <= n; ++i)
  24. cin >> parents[i];
  25. vector<int> PofA;
  26. int i = a;
  27. while (i > 1) {
  28. PofA.emplace_back(i);
  29. i = parents[i];
  30. }
  31. PofA.emplace_back(1);
  32. i = b;
  33. int answ;
  34. bool f = false;
  35. while (i > 1 && !f) {
  36. for (int j = 0; j < (int)PofA.size(); ++j)
  37. if (i == PofA[j]) {
  38. answ = i;
  39. f = true;
  40. break;
  41. }
  42. i = parents[i];
  43. }
  44. if (!f) answ = 1;
  45. cout << answ;
  46. //std::cout << "Hello World!\n";
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement