Let it XOR

Title Description

Isopropyl or a magic operation, most people it does not carry addition summarized.

In life ... xor operation are also common. For example, to answer a question, it is 1, otherwise 0. Then:

(A whether male) XOR (B whether male) = A and B is able to become a couple

Well, now we have to make and deal with complex situations. For example, we will give a tree, it's nice that they have N nodes. Each side of the tree has a weight. We want to ask M times, each time asking for, we want to know all sides of XOR value right on a path between two points.

Input Format

The first line of the input file contains an integer N, the happy Zheke tree of nodes owned, there is the following row N-1, described these sides, each row has three numbers, u, v, W, u and v represent the w has a value between the right side. The next line there is an integer M, represents the number of inquiries. After the M rows, each row two numbers u, v, represents the exclusive OR of challenge value on the right path between the two points.

Output Format

M output lines, each an integer that represents the exclusive OR value

Sample input and output

Input # 1
5
1 4 9644
2 5 15004
3 1 14635
5 3 9684
3
2 4
5 4
1 1
Output # 1
975
14675
0

Description / Tips

For 40% of the data, it is 1 ≤ N, M ≤ 3000;

To 100% of the data, it is 1 ≤ N, M≤ 100000.

 

 

Metaphysics tree section

 

 

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 100000 + 10;
vector<pair<int,int> >edges[maxn];
int n,m,dis[maxn];

inline int read(){
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){
            w=-1;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        s=s*10+ch-'0';
        ch=getchar();
    }
    return s*w;
}


inline void dfs(int now,int f,int Xor) {
    dis[now] = Xor;
    for (size_t i = 0;i < edges[now].size();i++){
        if (edges[now][i].first != f){
            dfs(edges[now][i].first,now,Xor^edges[now][i].second);
        }
    }
}
int main() {
    n=read();
    for (int i = 1,u,v,w;i < n;i++) {
        u=read();
        v=read();
        w=read();
        edges[u].push_back(make_pair(v,w));
        edges[v].push_back(make_pair(u,w));
    }
    dfs(1,1,1);
    m=read();
    for (int i = 1,u,v;i <= m;i++) {
        u=read();
        v=read();
        printf("%d\n",dis[u]^dis[v]);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hrj1/p/11230935.html
XOR
let