Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int T;
- void dfs(int v, vector<vector<int> > &g, vector<int> &used, vector<int> &tin, vector<int> &tout)
- {
- used[v] = 1;
- tin[v] = T++;
- for (auto to : g[v])
- {
- if (!used[to])
- dfs(to, g, used, tin, tout);
- }
- tout[v] = T++;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- //freopen("input.txt", "r", stdin);
- int n;
- cin >> n;
- vector<vector<int> > g(n);
- for (int i = 0; i < n - 1; i++)
- {
- int x;
- cin >> x;
- g[x - 1].push_back(i + 1);
- g[i + 1].push_back(x - 1);
- }
- vector<int> tin (n ,0), tout(n, 0);
- T = 0;
- vector<int> used (n, 0);
- dfs(0, g, used, tin, tout);
- int m;
- cin >> m;
- for (int i = 0; i < m; i++)
- {
- int u, v;
- cin >> u >> v;
- u--, v--;
- if (tin[u] < tin[v] && tout[u] > tout[v])
- cout << 1 << endl;
- else if (tin[u] > tin[v] && tout[u] < tout[v])
- cout << 2 << endl;
- else
- cout << 3 << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement