2115: [Wc2011] Xor
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2568 Solved: 1086
[ 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
Source
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 1E5 + 10;
typedef long long LL;
struct E{
int to,num; LL va;
E(){}
E(int to,int num,LL va): to(to),num(num),va(va){}
};
int n,m,tot;
LL w[maxn],A[maxn],B[66],ans;
bool bo[maxn],vis[maxn];
vector <E> v[maxn];
bool cmp(const LL &x,const LL &y) {return x > y;}
void Dfs(int x,int fa)
{
for (int i = 0; i < v[x].size(); i++) {
int to = v[x][i].to;
if (to == fa) continue;
if (vis[to]) {
if (bo[v[x][i].num]) continue;
A[++tot] = (w[x]^w[to]^v[x][i].va);
bo[v[x][i].num] = 1;
continue;
}
vis[to] = 1;
w[to] = (v[x][i].va^w[x]);
Dfs(to,x);
}
}
LL getLL()
{
LL ret = 0;
char ch = getchar();
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = 10LL*ret + 1LL*(ch - '0'),ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
n = getLL();
m = getLL();
for (int i = 1; i <= m; i++) {
int x,y; LL w;
x = getLL();
y = getLL();
w = getLL();
v[x].push_back(E(y,i,w));
v[y].push_back(E(x,i,w));
}
vis[1] = 1; Dfs(1,0); ans = w[n];
if (!vis[n]) {cout << 0 << endl; return 0;}
sort(A + 1,A + tot + 1,cmp);
int las = 1;
for (LL j = 60; j >= 0; j--)
for (int i = las; i <= tot; i++)
if (A[i]&(1LL<<j)) {
for (int k = i + 1; k <= tot; k++)
if (A[k]&(1LL<<j))
A[k] ^= A[i];
B[j] = A[i];
las = i + 1;
}
for (int i = 60; i >= 0; i--)
ans = (ans^B[i]) > ans?(ans^B[i]):ans;
cout << ans << endl;
return 0;
}