P5651 基础最短路练习题
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define ll long long
using namespace std;
const int N = 100005;
struct edge {
int to, next, v;
} e[N * 4];
int head[N], tot, n, m, Q;
int vis[N], val[N];
void add(int x, int y, int v) {
e[++tot] = (edge){y, head[x], v};
head[x] = tot;
}
void dfs(int x) {
vis[x] = 1;
for (int i = head[x]; i; i = e[i].next)
if (!vis[e[i].to]) {
val[e[i].to] = val[x] ^ e[i].v;
dfs(e[i].to);
}
}
int main() {
scanf("%d%d%d", &n, &m, &Q);
For(i, 1, m) {
int x, y, v;
scanf("%d%d%d", &x, &y, &v);
add(x, y, v);
add(y, x, v);
}
dfs(1);
while (Q--) {
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", val[x] ^ val[y]);
}
}
题解: