P5651
题目大意
求一个x到y简单路径的异或和.
思路:
因为图中肯定有环我们肯定是不能走环的.
然后我就随便找了一颗生成树,然后在生成树上找到每一个点到根节点
异或和,查询的时候直接将两个异或和异或起来就好了
关于子树上的点到根节点路径重复的问题?
因为我们可以知道两个点从他们的lca到根节点的路径都是重复的
然后两个值异或起来之后就消掉了,所以我们可以直接将两个点到根节点的异或值直接异或起来.
code:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 100010
#define M 1010
using namespace std;
struct node {
int next, to, dis;
}edge[N << 1];
struct tree {
int x, y, dis;
}bian[N];
int n, m, q, head[N << 1], add_edge;
int fath[N], dis[N];
int read() {
int s = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
}
void add(int from, int to, int dis) {
edge[++add_edge].next = head[from];
edge[add_edge].to = to;
edge[add_edge].dis = dis;
head[from] = add_edge;
}
bool cmp(tree a, tree b) {
return a.dis < b.dis;
}
int father(int x) {
if (x != fath[x]) fath[x] = father(fath[x]);
return fath[x];
}
void kruskal() {
for (int i = 1; i <= n; i++) fath[i] = i;
int cnt = 0;
for (int i = 1; i <= m; i++) {
if (father(bian[i].x) != father(bian[i].y)) {
fath[bian[i].x] = bian[i].y;
add(bian[i].x, bian[i].y, bian[i].dis);
add(bian[i].y, bian[i].x, bian[i].dis);
cnt ++;
}
if (cnt == n - 1) break;
}
}
void dfs(int x, int fa) {
for (int i = head[x]; i; i = edge[i].next) {
int to = edge[i].to;
if (to == fa) continue;
dis[to] = (dis[x] ^ edge[i].dis);
dfs(to, x);
}
}
int main() {
n = read(), m = read(), q = read();
for (int i = 1; i <= m; i++)
bian[i].x = read(), bian[i].y = read(), bian[i].dis = read();
kruskal();
dfs(1, 0);
for (int i = 1, x, y; i <= q; i++) {
x = read(), y = read();
printf("%d\n", (dis[x] ^ dis[y]));
}
return 0;
}