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. 实现Asp.Net Mvc4多级Views目录

    建立自己MyViewEngine类让他继承RazorViewEngine,之后在构造函数里面写入设置视图位置格式代码如下: public class MyViewEngine : RazorViewE ...

  2. 解决安卓TextView异常换行,参差不齐等问题

    参考:http://blog.csdn.net/u012286242/article/details/28429267?utm_source=tuicool&utm_medium=referr ...

  3. MVC公开课 – 1.基础 (2013-3-15广州传智MVC公开课)

      1.MVC设计模式 Model 是指 要处理的业务代码和数据操作代码 View 视图 主要是指的 跟用户打交道 并能够展示数据 Controller 看成是 Model和View的桥梁 优点: 1 ...

  4. 多线线程async与await关键字

    创建线程 //这里面需要注意的是,创建Thread的实例之后,需要手动调用它的Start方法将其启动. //但是对于Task来说,StartNew和Run的同时,既会创建新的线程,并且会立即启动它. ...

  5. siblings 使用

    //$(object).siblings().each(function () { // $(this).find("img").attr("class", & ...

  6. Web框架本质

    Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:utf- ...

  7. VC++6.0使用OpenGL前的配置(必看)

    要在VC++6.0中使用opengl,需要配置一下环境设置. 具体需要两步: 1.加入一个头文件,两个lib文件,两个dll文件,放在合适位置. 2.配置一下vc++6.0的Project Setti ...

  8. hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  9. 攻城狮在路上(叁)Linux(十七)--- linux磁盘与文件管理概述

    一.复习知识点: 1.扇区是最小的物理存储单位,大小为512bytes. 2.扇区组成一个圆,成为柱面,柱面是分区的最小单位. 3.第一个扇区很重要,因为包含了MBR(446字节)和分区表(64字节) ...

  10. golang time and duration

    package mainimport "fmt"import "time"func main() { p := fmt.Println // We'll sta ...