Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define loop(i,from,to) for (int i = from; i < to; ++i)
- #include <bits/stdc++.h>
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
- using std::string;
- using std::map;
- using std::pair;
- const int INF = 1e9+7;
- const double eps = 1e-6;
- signed main() {
- std::ios::sync_with_stdio(false);
- cin.tie(0);
- int n; cin >> n;
- int a, b; cin >> a >> b;
- vector<int> parents(n + 1);
- parents[1] = 1;
- for (int i = 2; i <= n; ++i)
- cin >> parents[i];
- vector<int> PofA;
- int i = a;
- while (i > 1) {
- PofA.emplace_back(i);
- i = parents[i];
- }
- PofA.emplace_back(1);
- i = b;
- int answ;
- bool f = false;
- while (i > 1 && !f) {
- for (int j = 0; j < (int)PofA.size(); ++j)
- if (i == PofA[j]) {
- answ = i;
- f = true;
- break;
- }
- i = parents[i];
- }
- if (!f) answ = 1;
- cout << answ;
- //std::cout << "Hello World!\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement