2115: [Wc2011] Xor
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1459 Solved: 637
[ Submit][ Status][ Discuss]
Description
Input
第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目。 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边。 图中可能有重边或自环。
Output
仅包含一个整数,表示最大的XOR和(十进制结果) 。
Sample Input
5 7
1 2 2
1 3 2
2 4 1
2 5 1
4 5 3
5 3 4
4 3 2
1 2 2
1 3 2
2 4 1
2 5 1
4 5 3
5 3 4
4 3 2
Sample Output
6
HINT
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#define ll long long
using namespace std;
const int MAXN = 50000 + 100;
const int MAXM = MAXN << 2;
struct Edge
{int to, next; ll w;} edge[MAXM<<1];
int tot, head[MAXN];
void init(){tot = 0, memset(head, -1, sizeof(head));}
void addedge(int u, int v, ll w)
{edge[tot].to = v; edge[tot].next = head[u];edge[tot].w = w;head[u] = tot++;}
int vis[MAXN], n, m, u, v, circle;
ll d[MAXN], a[MAXM], w, bit[100];
void dfs(int u)
{
vis[u] = 1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v = edge[i].to;
if(!vis[v]) d[v] = d[u] ^ edge[i].w, dfs(v);
else a[circle++] = d[u] ^ d[v] ^ edge[i].w;
}
}
void gauss()
{
int i, j, row = 0;
for(i=63;i>=0;i--)
{
for(j=row;j<circle;j++) if(a[j] & bit[i]) break;
if(j != circle)
{
swap(a[j], a[row]);
for(j=0;j<circle;j++)
{
if(j == row) continue;
if(a[j] & bit[i]) a[j] ^= a[row];
}
row++;
}
}
long long ans = d[n];
for(int i=63;i>=0;i--)
if((ans ^ a[i]) > ans) ans ^= a[i];
printf("%lld\n",ans);
}
int main()
{
bit[0] = 1; for(int i=1;i<64;i++) bit[i] = bit[i-1] << 1;
init();circle = 0;
scanf("%d%d", &n, &m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%lld", &u, &v, &w);
addedge(u, v, w); addedge(v, u, w);
}
dfs(1);
gauss();
return 0;
}