Advertisement
SuitNdtie

Xor tree

May 5th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<vector>
  3. #include<queue>
  4. using namespace std;
  5. typedef long long int ll;
  6. int main()
  7. {
  8.     int n,m;
  9.     scanf("%d %d",&n,&m);
  10.     vector<pair<ll,int> > adj[n+1];
  11.     for(int i = 1 ; i < n ; i ++){
  12.         int u,v;
  13.         ll w;
  14.         scanf("%d %d %lld",&u,&v,&w);
  15.         adj[u].push_back({w,v});
  16.         adj[v].push_back({w,u});
  17.     }
  18.     ll qxor[n+1];
  19.     bool visited[n+1];
  20.     for(int i = 1 ; i <= n ; i ++ ){
  21.         visited[i] = false;
  22.     }
  23.     queue<int> q;
  24.     qxor[1] = 0;
  25.     q.push(1);
  26.     while(!q.empty()){
  27.         int u = q.front();
  28.         q.pop();
  29.         if(visited[u])continue;
  30.         visited[u] = true;
  31.         for(int i = 0 ; i <adj[u].size() ; i ++){
  32.             int v = adj[u][i].second;
  33.             ll w = adj[u][i].first;
  34.             if(!visited[v]){
  35.                 qxor[v] = qxor[u] xor w;
  36.                 q.push(v);
  37.             }
  38.         }
  39.     }
  40.     for(int i = 0 ; i < m ; i ++){
  41.         int u,v;
  42.         scanf("%d %d",&u,&v);
  43.         printf("%lld\n",qxor[u] xor qxor[v]);
  44.     }
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement