Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<int> g[100005];
- int timer = 0;
- int tin[100005], tout[100005];
- int dfs_time(int node, int parent)
- {
- timer++;
- tin[node] = timer;
- for(auto child : g[node])
- {
- if(child==parent)
- {
- continue;
- }
- dfs_time(child, node);
- }
- timer++;
- tout[node] = timer; timer++;
- return 0;
- }
- vector<int> solve(int N, vector<vector<int>> edge, int Q, vector<vector<int>> query)
- {
- vector<int> ans(Q);
- for(int i=0; i<N-1; i++)
- {
- g[edge[i][0]].push_back(edge[i][1]);
- g[edge[i][1]].push_back(edge[i][0]);
- }
- dfs_time(1, 1);
- for(int i=0; i<Q; i++)
- {
- ans[i] = (tin[query[i][0]]<=tin[query[i][1]] && tout[query[i][0]]>=tout[query[i][1]]);
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment