Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int n;
- vector <int> g[100010];
- int in[100010];
- int out[100010];
- int t = 1 ;
- int q = 0;
- bool used[100010];
- void dfs (int v)
- {
- in[v] = t;
- t++;
- used[v] = true;
- for (int i = 0; i < g[v].size() ; i++)
- {
- int u = g[v][i];
- if (!used[u])
- {
- dfs(u);
- }
- }
- out[v] = t;
- t++;
- }
- int main()
- {
- freopen("ancestor.in","r",stdin);
- freopen("ancestor.out","w",stdout);
- cin >> n;
- for (int i = 0; i < n; i++)
- {
- int a = -1;
- cin >> a;
- if (a != 0)
- {
- g[i].push_back(a - 1);
- g[a - 1].push_back(i);
- }
- else
- {
- q = i;
- }
- }
- for (int i = 0; i < n; i++)
- {
- used[i] = false;
- }
- dfs(q);
- int m;
- cin >> m;
- for (int i = 0; i < m; i++)
- {
- int c, d;
- cin >> c >> d;
- if (in[c - 1] < in[d - 1] && out[d - 1] < out [c - 1])
- {
- cout << "1" << endl;
- }
- else
- {
- cout << "0" << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement