The 2015 China Collegiate Programming Contest E. Ba Gua Zhen hdu 5544

Ba Gua Zhen

Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 304    Accepted Submission(s): 93


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

Fortunately, there was an old man named Chengyan Huang who was 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 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 the maximum XOR sum of all the edges length he has passed. If the he passed some edge multiple times, the length would also be calculated by multiple times. Now, could you tell Xun Lu which is the maximum XORcircuit path in this puzzle to help him hack the puzzle?
 

 

Input
The first line of the input gives the number of test cases, T(1T30)T test cases follow.

Each test case begins with two integers N(2N5×104) and M(1M105) in one line. Then M lines follow. Each line contains three integers uiviand wi(1ui,viN,0wi2601) to describe all the edges in the puzzle.
 

 

Output
For 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 two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same.

 

分析:可以拆成几个简单环来做,这样做就不会有问题。

 

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<iostream>
  5 #include<functional>
  6 #include <cassert>
  7 using namespace std;
  8 typedef long long LL;
  9 
 10 const int maxn = 101000;
 11 const int maxm = 510000;
 12 struct node {
 13     int nex;
 14 }a[maxn];
 15 struct edge {
 16     int nex, y;
 17     LL v;
 18 }e[maxm];
 19 int p;
 20 long long stack[maxn];
 21 int top;
 22 int tot;
 23 int f[maxn];
 24 long long num[300010];
 25 void add(int X, int Y, LL V) {
 26     p++;
 27     e[p].nex = a[X].nex;
 28     e[p].v = V;
 29     a[X].nex = p;
 30     e[p].y = Y;
 31 }
 32 bool vis[maxn];
 33 void dfs(int x) {
 34     f[x] = ++top;
 35     vis[x] = true;
 36     for (int t = a[x].nex; t; t = e[t].nex) {
 37         int y = e[t].y;
 38         
 39         if (f[y] == 0) {
 40             stack[y] = stack[x] ^ e[t].v;
 41             dfs(y);
 42         }else {
 43             if (!vis[y]) continue;
 44             num[++tot] = stack[x] ^ e[t].v ^ stack[y];
 45             //cout << x << " " << num[tot] << " " << s << " " << y << endl;
 46         }
 47     }
 48     vis[x] = false;
 49 }
 50 
 51 LL b[100], bN;
 52 void solve() {
 53     bN = 0;
 54 //    swap(num[3], num[2]);
 55     for (int i = 1; i <= tot; ++ i) {
 56 //        printf("%lld\n", num[i]);
 57         for (int j = 0; j < bN; ++ j) {
 58             if ((num[i]^b[j]) < num[i])
 59                 num[i] = num[i] ^ b[j];
 60         }
 61         if (num[i])
 62             b[bN ++] = num[i];
 63     }
 64     
 65     sort(b, b+bN, greater<LL>());
 66 //    for (int i = 0; i < bN; ++ i) printf("%lld ", b[i]);
 67     
 68     LL res = 0;
 69     for (int i = 0; i < bN; ++ i)
 70         if ((res ^ b[i]) > res)
 71             res ^= b[i];
 72     printf("%I64d\n", res);
 73 //    printf("%lld\n", res);
 74 }
 75 
 76 int T, cas = 0, n, m;
 77 int main() {
 78 //    freopen("e.in","r",stdin);
 79     scanf("%d", &T);
 80     while (T--) {
 81         printf("Case #%d: ", ++cas);
 82         scanf("%d%d", &n, &m);
 83         p = 0;
 84         memset(a,0,sizeof(a));
 85         memset(e,0,sizeof(e));
 86         for (int i = 1; i <= m; i++) {
 87             int x, y; LL v;
 88             scanf("%d%d", &x, &y);
 89 //            scanf("%lld", &v);
 90             scanf("%I64d", &v);
 91             add(x, y, v);
 92             add(y, x, v);
 93         }
 94         memset(stack,0,sizeof(stack));
 95         top = 0;
 96         tot = 0;
 97         memset(vis,0,sizeof(vis));
 98         memset(f,0,sizeof(f));
 99         dfs(1);
100         //for (int i = 1; i <= tot; i++) cout << num[i] << endl;
101         sort(num+1, num+1+tot); tot = unique(num+1, num+1+tot) - (num+1);
102 //        for (int i = 1; i <= tot; i++) cout << num[i] << endl;
103         solve();
104     }
105     return 0;
106 }
View Code

 

posted @ 2015-12-21 15:48  yanzx6  阅读(237)  评论(0编辑  收藏  举报