开始停课---不过下午只写了一道题

cdoj--HDU都有的一道异或高斯消元贪心的题

----Ba Gua Zhen

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 NN vertexes and MM edges. Each edge in the puzzle connected two vertexes which were uiui and vivi with a length of wiwi. 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, TT(1T301≤T≤30). TT test cases follow.

Each test case begins with two integers NN(2N5×1042≤N≤5×104) and MM(1M1051≤M≤105) in one line. Then MM lines follow. Each line contains three integers uiui, vivi andwiwi(1ui,viN1≤ui,vi≤N, 0wi26010≤wi≤260−1) to describe all the edges in the puzzle.

Output

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yy is the maximum XOR sum of one circuit path in the puzzle.

注意输出的Case.....挂了好久

#include<iostream>
#include<cstdio>
#include<cstring>
#define rint register int
#define lld long long
using namespace std;
#define maxn 60000
#define maxm 300000
lld n,m;
struct edge{
    lld np;
    lld next;
    lld val;
    bool mark;
}e[maxm];
lld cnt = 1;
lld head[maxn];
lld vaf[maxn];
void add(lld x,lld y,lld va){
    e[++cnt] = (edge){y,head[x],va};
    head[x] = cnt;
    e[++cnt] = (edge){x,head[y],va};
    head[y] = cnt; 
}
bool vis[maxn];
void dfs(lld x){
    for(rint ne = head[x];ne;ne = e[ne].next){
        if(!vis[e[ne].np]){
            vis[e[ne].np] = true;
            vaf[e[ne].np] = vaf[x] ^ e[ne].val;
            e[ne].mark  =1;
            e[ne^1].mark = 1;
            dfs(e[ne].np);
        }
    }
}
lld cntt = 0;
lld haha[maxm];
void chu(){
    for(rint i =1;i<=n;i++){
        for(rint ne = head[i];ne;ne = e[ne].next){
            if(!e[ne].mark){
                lld now = vaf[i] ^ vaf[e[ne].np] ^ e[ne].val;
                haha[cntt++] = now;
                e[ne].mark = 1;
                e[ne^1].mark = 1;    
            }
        }
    }
}
lld anss;
int xorguass(int n,lld* val){
    int row = 0;
    for(int i = 60;i >= 0;-- i){
        int j;
        for(j = row;j < n;++ j){
            if(val[j] & (1LL<<i))   break;
        }
        if(j != n){
            swap(val[j],val[row]);
            for(j = 0;j < n;++ j){
                if(j == row)    continue;
                if(val[j] & (1LL<<i))   val[j] ^= val[row];
            }
            ++ row;
        }
    }
    return row;
}
lld T;
lld a,b,c;
int main(){
    scanf("%lld",&T);
    int cas = 0;
    while(T--){
        cas++;
        scanf("%lld %lld",&n,&m);
        vaf[1] = 0;
        anss = 0;
        memset(e,0,sizeof(e));
        memset(head,0,sizeof(head));
        memset(haha,0,sizeof(haha));
        memset(vis,0,sizeof(vis));
        memset(vaf,0,sizeof(vaf));
        cntt = 0;
        cnt = 1;
        while(m--){
            scanf("%lld %lld %lld",&a,&b,&c);
            add(a,b,c);
        }
        vis[1] = 1;
        dfs(1);
        chu();
        int haha1 = xorguass(cntt,haha);
        for(rint i =0 ;i<cntt;i++){
            anss ^= haha[i];
        }
        printf("Case #%d: %lld\n",cas,anss);
    }
}

 

posted @ 2016-03-22 21:46  cx_oier  阅读(206)  评论(0编辑  收藏  举报