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(1≤T≤30). T test cases follow.

Each test case begins with two integers N(2≤N≤5×104) and M(1≤M≤105) in one line. Then M lines follow. Each line contains three integers ui, viand wi(1≤ui,vi≤N,0≤wi≤260−1) 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.

 

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

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<functional>
#include <cassert>
using namespace std;
typedef long long LL; const int maxn = ;
const int maxm = ;
struct node {
int nex;
}a[maxn];
struct edge {
int nex, y;
LL v;
}e[maxm];
int p;
long long stack[maxn];
int top;
int tot;
int f[maxn];
long long num[];
void add(int X, int Y, LL V) {
p++;
e[p].nex = a[X].nex;
e[p].v = V;
a[X].nex = p;
e[p].y = Y;
}
bool vis[maxn];
void dfs(int x) {
f[x] = ++top;
vis[x] = true;
for (int t = a[x].nex; t; t = e[t].nex) {
int y = e[t].y; if (f[y] == ) {
stack[y] = stack[x] ^ e[t].v;
dfs(y);
}else {
if (!vis[y]) continue;
num[++tot] = stack[x] ^ e[t].v ^ stack[y];
//cout << x << " " << num[tot] << " " << s << " " << y << endl;
}
}
vis[x] = false;
} LL b[], bN;
void solve() {
bN = ;
// swap(num[3], num[2]);
for (int i = ; i <= tot; ++ i) {
// printf("%lld\n", num[i]);
for (int j = ; j < bN; ++ j) {
if ((num[i]^b[j]) < num[i])
num[i] = num[i] ^ b[j];
}
if (num[i])
b[bN ++] = num[i];
} sort(b, b+bN, greater<LL>());
// for (int i = 0; i < bN; ++ i) printf("%lld ", b[i]); LL res = ;
for (int i = ; i < bN; ++ i)
if ((res ^ b[i]) > res)
res ^= b[i];
printf("%I64d\n", res);
// printf("%lld\n", res);
} int T, cas = , n, m;
int main() {
// freopen("e.in","r",stdin);
scanf("%d", &T);
while (T--) {
printf("Case #%d: ", ++cas);
scanf("%d%d", &n, &m);
p = ;
memset(a,,sizeof(a));
memset(e,,sizeof(e));
for (int i = ; i <= m; i++) {
int x, y; LL v;
scanf("%d%d", &x, &y);
// scanf("%lld", &v);
scanf("%I64d", &v);
add(x, y, v);
add(y, x, v);
}
memset(stack,,sizeof(stack));
top = ;
tot = ;
memset(vis,,sizeof(vis));
memset(f,,sizeof(f));
dfs();
//for (int i = 1; i <= tot; i++) cout << num[i] << endl;
sort(num+, num++tot); tot = unique(num+, num++tot) - (num+);
// for (int i = 1; i <= tot; i++) cout << num[i] << endl;
solve();
}
return ;
}

The 2015 China Collegiate Programming Contest E. Ba Gua Zhen hdu 5544的更多相关文章

  1. The 2015 China Collegiate Programming Contest L. Huatuo's Medicine hdu 5551

    Huatuo's Medicine Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  2. The 2015 China Collegiate Programming Contest D.Pick The Sticks hdu 5543

    Pick The Sticks Time Limit: 15000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  3. The 2015 China Collegiate Programming Contest A. Secrete Master Plan hdu5540

    Secrete Master Plan Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Othe ...

  4. The 2015 China Collegiate Programming Contest Game Rooms

    Game Rooms Time Limit: 4000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  5. The 2015 China Collegiate Programming Contest C. The Battle of Chibi hdu 5542

    The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Othe ...

  6. The 2015 China Collegiate Programming Contest K Game Rooms hdu 5550

    Game Rooms Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  7. The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547

    Sudoku Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  8. The 2015 China Collegiate Programming Contest G. Ancient Go hdu 5546

    Ancient Go Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  9. The 2015 China Collegiate Programming Contest -ccpc-c题-The Battle of Chibi(hdu5542)(树状数组,离散化)

    当时比赛时超时了,那时没学过树状数组,也不知道啥叫离散化(貌似好像现在也不懂).百度百科--离散化,把无限空间中无限的个体映射到有限的空间中去,以此提高算法的时空效率. 这道题是dp题,离散化和树状数 ...

随机推荐

  1. 词频统计_输入到文件_update

    /* 输入文件见337.in.txt 输出文件见338.out.txt */ #include <iostream> #include <cctype> #include &l ...

  2. 【转】深入Windows内核——C++中的消息机制

    上节讲了消息的相关概念,本文将进一步聊聊C++中的消息机制. 从简单例子探析核心原理 在讲之前,我们先看一个简单例子:创建一个窗口和两个按钮,用来控制窗口的背景颜色.其效果 图1.效果图  Win32 ...

  3. C 工厂模式 还有其他的模式

    http://blog.csdn.net/feixiaoxing/article/details/7081243

  4. 浅析 - Storyboard / Xib

    大家都知道纯代码写应用的成本是很高的,特别是涉及到UI界面的实现,相当耗费时间.之前自己写应用时有了解过Storyboard,也简单使用过,但随着最近深入了解它之后,发现自己低估了它的作用和影响力,因 ...

  5. LR性能指标分析

    Memory: ·Available Mbytes 简述:可用物理内存数.如果Available Mbytes的值很小(4 MB或更小),则说明计算机上总的内存可能不足,或某程序没有释放内存. 参考值 ...

  6. Dapper.NET 使用简单举例

    概述 Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的.也就是说实体类都要自己写.它没有复杂的配置文件,一个单文 ...

  7. 常用的Java代码汇总

    1. 字符串有整型的相互转换           Java   1 2 <strong>Stringa=String.valueOf(2);   //integer to numeric ...

  8. Linux下修改默认字符集--->解决Linux下Java程序种中文文件夹file.isDirectory()判断失败的问题

    一.问题描述: 一个项目中为了生成树状目录,调用了file.listFiles()方法,然后利用file.isDirectory()方法判断是否为目录,该程序在windows下运行无问题,在Linux ...

  9. proxifier 代理bluestack

    proxycap 可以很方便的代理bluestack, 但是proxycap 的破解版现在越来越不好用了,而且不小心还会中个病毒,这个时候免费的proxifier就显得更加的可爱了. 但是有个问题,就 ...

  10. hdu 5437 优先队列+模拟 **

    比赛的时候虽然考虑到没门的情况,但是写了几组都能过,就没想了,23333,差一行代码就能A,遗憾~~ #include<cstdio> #include<iostream> # ...