BZOJ 2115: [Wc2011] Xor 线性基 dfs

https://www.lydsy.com/JudgeOnline/problem.php?id=2115

每一条从1到n的道路都可以表示为一条从1到n的道路异或若干个环的异或值。

那么把全部的环丢到线性基里基本操作就可以了。。

https://blog.csdn.net/qaq__qaq/article/details/53812883 这个博客非常好

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 #define LL long long
 8 const int maxn=50010;
 9 int n,m;
10 struct nod{
11     int y,next;LL v;
12 }e[maxn*4];
13 int head[maxn]={},tot=0;
14 LL b[65]={},c[65]={},d[maxn]={},val[maxn*4]={},cnt=0,tem=0;
15 bool vis[maxn]={};
16 void init(int x,int y,LL v){
17     e[++tot].y=y;e[tot].v=v;e[tot].next=head[x];head[x]=tot;
18 }
19 void dfs(int x){
20     vis[x]=1;
21     for(int i=head[x];i;i=e[i].next){
22         if(!vis[e[i].y]){
23             d[e[i].y]=d[x]^e[i].v;
24             dfs(e[i].y);
25         }
26         else val[++cnt]=d[e[i].y]^d[x]^e[i].v;
27     }
28 }
29 void getit(LL x){
30     for(int i=60;i>0;i--){
31         if(x&c[i]){
32             if(!b[i]){b[i]=x;break;}
33             x^=b[i];
34         }
35     }
36 }
37 int main(){
38     int x,y;LL z;
39     scanf("%d%d",&n,&m);
40     for(int i=1;i<=m;i++){
41         scanf("%d%d%lld",&x,&y,&z);
42         init(x,y,z);init(y,x,z);
43     }
44     dfs(1);c[1]=1;
45     for(int i=2;i<=60;i++)c[i]=c[i-1]*2;
46     for(int i=1;i<=cnt;i++){getit(val[i]);}
47     LL ans=d[n];
48     for(int i=60;i>0;i--)ans=max(ans,ans^b[i]);
49     printf("%lld\n",ans);
50     return 0;
51 }
View Code

 

posted @ 2018-04-14 15:18  鲸头鹳  阅读(105)  评论(0编辑  收藏  举报