bzoj2115: [Wc2011] Xor

一道不知道讲过多少遍的线性基。

发现路径一定是一条路径一条若干环拼成的。路径是什么无所谓,总能通过走一些环调整成其他路径。

所以答案就是1到n的随意一条路径加若干环能异或出的最大值。如何找环,建出树,每一条非树边对应一个环。

然后直接用线性基就可以了。

 1 //Achen
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<cstdio>
 7 #include<vector>
 8 #include<queue>
 9 #include<cmath>
10 #define For(i,a,b) for(int i=(a);i<=(b);i++)
11 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
12 const int N=2e5+7;
13 typedef long long LL;
14 using namespace std;
15 int n,m,tot;
16 LL a[N],b[60];
17 
18 template<typename T> void read(T &x) {
19     T f=1; x=0; char ch=getchar();
20     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
21     if(ch=='-') f=-1,ch=getchar();
22     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
23 }
24 
25 int ecnt,fir[N],nxt[N],to[N],vis[N];
26 LL val[N];
27 void add(int u,int v,LL x) {
28     nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=x; 
29     nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u; val[ecnt]=x;
30 }
31 
32 LL R[N];
33 void dfs(int x,int fa) {
34     vis[x]=1;
35     for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
36         if(!vis[to[i]]) {
37             R[to[i]]=R[x]^val[i];
38             dfs(to[i],x);
39         }
40         else a[++tot]=R[x]^R[to[i]]^val[i];
41     }
42 } 
43 
44 int main() {
45 #ifdef DEBUG
46     freopen("1.in","r",stdin);
47     freopen("1.out","w",stdout);
48 #endif
49     read(n); read(m);
50     For(i,1,m) {
51         int x,y; LL d;
52         read(x); read(y); read(d);
53         add(x,y,d);
54     }
55     dfs(1,0);
56     LL qs=R[n];
57     For(i,1,tot)
58         Rep(j,59,0) if(a[i]&(1LL<<j)) {
59             if(!b[j]) { b[j]=a[i]; break; }
60             else a[i]^=b[j];
61         } 
62     Rep(i,59,0) if((qs^b[i])>qs) qs^=b[i];
63     printf("%lld\n",qs);
64     return 0;
65 }
66 /*
67 5 7 
68 1 2 2 
69 1 3 2 
70 2 4 1 
71 2 5 1 
72 4 5 3 
73 5 3 4 
74 4 3 2
75 */ 
View Code

 

posted @ 2018-04-02 15:08  啊宸  阅读(84)  评论(0编辑  收藏  举报