hdu5544 Ba Gua Zhen(高斯消元)

思路:首先DFS把图上的所有环找出来,然后就是K个数里面任意选数使得异或和最大,高斯消元就好了

异或方程的高斯消元见 点击打开链接


#include<bits/stdc++.h>
using namespace std;
const int maxn = 5*1e4+7;
const int maxm = 1e5+7;
#define LL long long
int dyn[maxn];
vector<pair<int,LL> >e[maxn];
LL f[maxn];
int tim = 0;
int cnt = 0;
LL cir[maxm<<1];

void dfs(int u,int fa)
{
	dyn[u]=1;
	for(int i = 0;i<e[u].size();i++)
	{
		int v = e[u][i].first;
		LL w = e[u][i].second;
		if(v==fa)
			continue;
		if(!dyn[v])
		{
			f[v]=f[u]^w;
			dfs(v,u);
		}
		else
		{
			cir[cnt++]=f[u]^f[v]^w;
		}
		//if(dyn[v]==0)
		//{
		//	f[v]=f[u]^w;
		//	dfs(v);
		//}
		//else if (dyn[v]>dyn[u])
		//{
		//	LL tmp = f[v]^f[u]^w;
		//	if(tmp)
		//	  cir[cnt++]=tmp;
		//}
	}
}
int main()
{
    int T,cas=1;
	scanf("%d",&T);
	while(T--)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i = 0;i<=n;i++)
			e[i].clear();
		memset(cir,0,sizeof(cir));
		memset(f,0,sizeof(f));
		memset(dyn,0,sizeof(dyn));
		for(int i = 1;i<=m;i++)
		{
			int u,v;
			LL w;
			scanf("%d%d%lld",&u,&v,&w);
			e[u].push_back(make_pair(v,w));
			e[v].push_back(make_pair(u,w));
		}
		//f[1]=0;
		tim=0;
		cnt = 0;
		dfs(1,-1);
		LL ans = 0;
		int row = 0;
        for(int i = 60;i>=0;i--)
		{
            LL tmp = (1LL)<<i;
			int j;
            for(j = row;j<cnt;j++)
				if(cir[j]&tmp)
				   break;
			if(j==cnt)
				continue;
            if(j!=row)
			{
				swap(cir[j],cir[row]);
			}
			for(int k = row+1;k<cnt;k++)
			{
				if(cir[k]&tmp)
					cir[k]^=cir[row];
			}
			row++;
		}
		for(int i = 0;i<row;i++)
			ans = max(ans^cir[i],ans);
		printf("Case #%d: ",cas++);
		printf("%lld\n",ans);
	}
}



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   vertexes and  edges. Each edge in the puzzle connected two vertexes which were   and   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  XOR circuit path in this puzzle to help him hack the puzzle?
 

Input

The first line of the input gives the number of test cases,   test cases follow. 

Each test case begins with two integers   and   in one line. Then   lines follow. Each line contains three integers   and   to describe all the edges in the puzzle.
 

Output

For each test case, output one line containing  Case #x: y, where   is the test case number (starting from 1) and   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. 
         
 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值