您的位置:首页 > 产品设计 > UI/UE

UESTC OJ1219 Ba Gua Zhen

2016-07-20 15:39 489 查看

链接:http://mozhu.today/#/problem/show/1219

题意:给定n个点m条边有边权的无向图,求一个异或值最大的回路。

分析:其实就是将所有环的异或值找出来然后任选一些组成最大的异或值。从k个数中任选一些求最大的异或值在莫涛的高斯消元解XOR方程组里面讲得很清楚。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=50010;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int MAX=1000000010;
const ll INF=1ll<<55;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
int tot,q
,u
,v[4*N],pre[4*N];
ll xo
,d[4*N],w[4*N];
void add(int a,int b,ll c) {
v[tot]=b;w[tot]=c;pre[tot]=u[a];u[a]=tot++;
}
void dfs(int x,int f,ll c) {
if (q[x]) {
d[++tot]=c^xo[x];return ;
}
q[x]=1;xo[x]=c;
for (int i=u[x];i!=-1;i=pre[i])
if (v[i]==f) continue ;
else dfs(v[i],x,c^w[i]);
}
int main()
{
int a,b,i,j,k,n,m,t,ca;
ll c,ans;
scanf("%d", &t);
for (ca=1;ca<=t;ca++) {
scanf("%d%d", &n, &m);
tot=0;memset(u,-1,sizeof(u));
for (i=1;i<=m;i++) {
scanf("%d%d%lld", &a, &b, &c);
add(a,b,c);add(b,a,c);
}
memset(q,0,sizeof(q));
tot=0;ans=0;
dfs(1,-1,0);
for (i=60,k=1;i>=0;i--) {
for (j=k;j<=tot;j++)
if ((d[j]&(1ll<<i))!=0) break ;
if (j==tot+1) continue ;
if (j!=k) swap(d[k],d[j]);
for (j=k+1;j<=tot;j++)
if ((d[j]&(1ll<<i))!=0) d[j]^=d[k];
k++;
}
for (i=1;i<k;i++) ans=max(ans,ans^d[i]);
printf("Case #%d: %lld\n", ca, ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: