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; }
相关文章推荐
- Unique Paths II
- 334. Increasing Triplet Subsequence
- UIStackView 的使用
- String、StringBuffer、StringBuilder的区别
- EF CreateQuery()的命名空间
- easyUI 获取记录数 页数 当前页
- 去掉UICollectionView 在 reloadItems时默认附加的隐式fade动画。
- Unique Paths
- Windows下设备GUID常识
- LeetCode Unique Binary Search Trees动态规划方法详解
- 安装Flash Builder4.6遇到的问题
- iOS开发UI篇-自定义CALayer
- UI阅读笔记
- iOS UILabel设置行间距
- iOS开发从入门到精通-- UIProgressView进度条&UISlider滑动条
- 如何做好UI设计
- UITableView中cell上按钮获取所选cell
- EasyUI之Layout布局
- EasyUI之Layout布局
- EasyUI之Layout布局