The5threic1-1

Shubh_Q1

Apr 3rd, 2024
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | Software | 0 0
  1. vector<int> g[100005];
  2. int timer = 0;
  3. int tin[100005], tout[100005];
  4. int dfs_time(int node, int parent)
  5. {
  6.     timer++;
  7.     tin[node] = timer;
  8.     for(auto child : g[node])
  9.     {  
  10.         if(child==parent)
  11.         {
  12.             continue;  
  13.         }  
  14.         dfs_time(child, node);
  15.     }
  16.     timer++;
  17.     tout[node] = timer; timer++;
  18.     return 0;          
  19. }
  20. vector<int> solve(int N, vector<vector<int>> edge, int Q, vector<vector<int>> query)
  21. {
  22.     vector<int> ans(Q);
  23.     for(int i=0; i<N-1; i++)
  24.     {
  25.         g[edge[i][0]].push_back(edge[i][1]);
  26.         g[edge[i][1]].push_back(edge[i][0]);       
  27.     }
  28.     dfs_time(1, 1);
  29.     for(int i=0; i<Q; i++)
  30.     {
  31.         ans[i] = (tin[query[i][0]]<=tin[query[i][1]] && tout[query[i][0]]>=tout[query[i][1]]); 
  32.     }
  33.     return ans;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment