http://acm.hdu.edu.cn/showproblem.php?pid=5544
思路:先把图dfs成多个环,
之后问题转为: 给n个数,选任意个,求最大异或值
问题等价于 http://blog.csdn.net/viphong/article/details/52809982
。有坑是。点的下标居然可以为0,题面是1到n。。不清空点为0的vector会RE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define MAXM 200010
#define MAXN 200010
typedef long long ll;
ll n,m,cnt,ans, vis[MAXN],cir[MAXM];
ll val[MAXM];
vector<pair<int ,ll> >mp[MAXM];
void dfs(int x,int fa)
{
vis[x]=1;
for (int i=0; i<mp[x].size(); i++)
{
ll v=mp[x][i].first,w=mp[x][i].second;
if (v==x)continue;
if(!vis[v])
{
val[v]=val[x]^w;
dfs(v,x);
}
else cir[++cnt]=val[x]^val[v]^w;
}
}
void solve(ll init,int N,ll A[])
{
ll rt=0;
for (int i=62; i>=0; i--)
{
for (int j=1; j<=N; j++)
{
if((A[j]>>i)&1)
{
if(!((rt>>i)&1)) rt^=A[j];
for (int k=1; k<=N; k++)
if((k!=j)&&((A[k]>>i)&1))
A[k]^=A[j];
A[j]=0;
}
}
}
cout<<rt<<endl;
}
int main()
{
//freopen("cin.in","r",stdin);
//freopen("cout.out","w",stdout);
int t;
int cases=1;
cin>>t;
while(t--)
{
cin>>n>>m;
cnt=0;
for (int i=0;i<=n;i++) mp[i].clear();
memset(vis,0,sizeof vis);
memset(val,0,sizeof val);
memset(cir,0,sizeof cir);
ll u,v,w;
for(int i=1; i<=m; i++)
{
scanf("%lld%lld%lld",&u,&v,&w);
mp[u].push_back(make_pair(v,w));
mp[v].push_back(make_pair(u,w));
}
dfs(1,0);
printf("Case #%d: ",cases++);
solve(val[n],cnt,cir);
}
return 0;
}