"BZOJ-2115" Xor linear base + DFS

Source: Internet
Author: User

2115: [Wc2011] Xor time limit:10 Sec Memory limit:259 MB
submit:2142 solved:893
[Submit] [Status] [Discuss] Description

Input

The first line contains two integers n and M, indicating the number of points in the graph and the number of edges. The next M-line describes M-bar, with three integers per line si,ti, Di, which indicates that there is a non-forward edge between Si and Ti with a weighted value of Di. There may be heavy edges or self-loops in the diagram.

Output

Contains only an integer that represents the maximum XOR and (decimal result), and note that the output is followed by a newline carriage return.

Sample Input5 7
1 2 2
1 3 2
2 4 1
2 5 1
4 5 3
5 3 4
4 3 2Sample Output6HINT

SourceSolution

Linear base

The idea is to take a path at random, then save each ring, ask for a linear basis, and then take the XOR with the previous path, take the maximum is the answer

As for the linear base, the meaning of it is that the range of the elements in the S1 is different from or obtained from the original set S by a minimum subset S1 of the original set S.

Then XJB disorderly, specific written more clear or oxer fold more

Code
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>using namespacestd;Long LongRead () {Long Longx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 500010#defineMAXM 5000010intN,m,num;structedgenode{intNext,to;Long LongVal;} edge[maxm<<1];inthead[maxn],cnt;voidAddintUintVLong LongW) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v; edge[cnt].val=W;}voidInsertintUintVLong LongW) {Add (u,v,w); add (v,u,w);}BOOLVISIT[MAXN];Long LongVAL[MAXN],A[MAXN];voidDFS (intNow ) {Visit[now]=1;  for(intI=head[now]; I I=edge[i].next)if(!visit[edge[i].to]) val[edge[i].to]=val[now]^Edge[i].val,dfs (edge[i].to); Elsea[++num]=val[edge[i].to]^edge[i].val^Val[now];}intGauss () {inttmp=1;  for(intp= -; p>=0; p--)        {            intt=0;  for(intj=tmp; j<=num; J + +)if((a[j]>>p) &1) {t=j; Break;} if(t) {swap (a[t],a[tmp]);  for(intj=1; j<=num; J + +)if(J!=tmp && (a[j]>>p) &1)) a[j]^=A[tmp]; TMP++; }        }    returntmp-1;}intMain () {n=read (); m=read ();  for(intU,v,i=1; i<=m; i++) U=read (), v=read (), insert (U,v,read ()); DFS (1); Long Longans=Val[n]; Num=Gauss ();  for(intI=1; i<=num; i++) Ans=max (ans,ans^A[i]); printf ("%lld\n", ans); return 0;}

Hsy do not write the problem, the program or the hzwer of the replica

"BZOJ-2115" Xor linear base + DFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.