2015 Nanyang Ccpc e-ba Gua zhen Gaussian elimination element XOR Max

Source: Internet
Author: User

Ba Gua Zhen

Time Limit:1 Sec

Memory limit:256 MB

Topic Connection

No

Descriptionduring The Three-kingdom period, there is a general named Xun Lu who belonged to Kingdom Wu. Once his troop were chasing Bei Liu, he is stuck in the Ba Gua Zhen from Liang Zhuge. The puzzle could be considered as a undirected graph with N vertexes and M edges. Each edge in the puzzle connected, vertexes which were UI and vi with a length of WI. Liang Zhuge had great interests in the beauty of he puzzle, so there were no self-loops and between each pair of vertexes , there would is at the very one edge in the puzzle. And it is also guaranteed that there is at least one path to go between each pair of vertexes.

Fortunately, there was a old man named Chengyan Huang of who is willing to help Xun Lu to hack the puzzle. Chengyan told Xun Lu that he had to choose a vertex as the start point, then walk through some of the edges and return to The start point is at last. During His walk, he could go through some edges any times. Since Liang Zhuge had some mysterious magic, Xun Lu could hack the puzzle if and only if he could find such a path with th E maximum XOR sum of all the edges length he has passed. If the He passed some edge multiple times, the length would also is calculated by multiple times. Now, could-Xun Lu which is the maximum XOR circuit path in this puzzle to help him hack the puzzle?



Inputthe first line of the input gives the number of test cases, T (1≤t≤30). T test Cases follow.

Each test case is begins with the integers N (2≤n≤5x104) and M (1≤m≤105) in the one line. Then M lines follow. Each line contains three integers UI, VI and WI (1≤ui,vi≤n, 0≤wi≤260−1) to describe all the edges in the puzzle. Outputfor each test case, output one line containing case #x: Y, where x is the test Case number (starting from 1) and Y-is The maximum XOR sum of one circuit path in the puzzle.Sample Input
2
3 3
1 2 1
1 3 2
2 3 0
6 7
1 2 1
1 3 1
2 3 1
3 4 4
4 5 2
4 6 2
5 6 2

Sample Output

Case #1:3
Case #2:3
HINT

A XOR takes both bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding Bits. The result of each position was 1 if only the first bit was 1 or only the second bit was 1, but would be a 0 if both is 0 or Bo TH is 1. In this we perform the comparison of both bits, being 1 if the bits is different, and 0 if they is the same.

Test instructions

There is an <=50000 graph with an n (x) vertex m (<=100000) edge, each edge has a Benquan (0<= Benquan <2^60), which is the maximum value of the Edge XOR in all loops.

Exercises

First DFS, run out the XOR value of all rings, for example, there are k rings inside

Then we can convert to, give you the number of K, let you choose any number, so that the difference or value maximum

This will be done with Gaussian elimination: Http://wenku.baidu.com/link?url= Bfic5zoh7tkgkltgrrta5otflimsghlacqlx-xyjmfpglh14ujao33sdtlbfhhyn6jogt2b1d9xsxmp97degfpb8qzus_ezjnewgbhpvsco

Code:

#include <iostream>#include<math.h>#include<vector>#include<stdio.h>#include<cstring>using namespacestd;#defineMAXN 50005Vector<pair<int,Long Long> >G[maxn];vector<Long Long>ans;Long LongXOR[MAXN];intVIS[MAXN];voidDfsintXintPreLong LongAns) {    if(Vis[x]) {Long Longp = Ans ^Xor[x];        Ans.push_back (P); return; } Vis[x]=1;  for(intI=0; I<g[x].size (); i++)    {        intv =G[x][i].first; if(pre = = v)Continue; if(!vis[v]) xor[v]=ans^G[x][i].second; DFS (V,x,ans^G[x][i].second); }}intMain () {intT;SCANF ("%d",&t);  for(intcas=1; cas<=t;cas++) {ans.clear (); memset (Vis,0,sizeof(VIS)); memset (Xor,0,sizeof(Xor)); intN,M;SCANF ("%d%d",&n,&m);  for(intI=1; i<=n;i++) g[i].clear ();  for(intI=1; i<=m;i++)        {            intx, y;Long LongZ; scanf ("%d%d%lld",&x,&y,&z);            G[x].push_back (Make_pair (y,z));        G[y].push_back (Make_pair (x,z)); } DFS (1,-1,0); intk=0;  for(intI= -; i>=0; i--)        {            intJ;  for(J=k;j<ans.size (); j + +)            if((ans[j]& (1ll<<i))! =0)            {                 Break; }            if(J==ans.size ())Continue; if(j!=k) Swap (ans[k],ans[j]); //cout<<d[j]<<endl;             for(j=k+1; J<ans.size (); j + +)                if((ans[j]& (1ll<<i))! =0) Ans[j]^=Ans[k]; K++; }        Long LongAns =0;  for(intI=0; i<k;i++) Ans= Max (Ans,ans ^Ans[i]); printf ("Case #%d:%lld\n", Cas,ans); }}

2015 Nanyang Ccpc e-ba Gua zhen Gaussian elimination element XOR Max

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.