HDU-5883-The Best Path(欧拉路径+异或)

欧拉路径的定义
题目链接

俗称“一笔画”,这题就考欧拉路径的基础概念。

题意:给定一个无向图,关于欧拉路的问题。这里我们要求的是这个路径的结点异或在一起的最大值。
先判断是否构成欧拉路径。
再根据异或的偶数次相消,奇数次原数的规律,找每个结点(入度+出度)度为奇数次的,异或一下。
如果是欧拉回路,起点与终点未知但相同,所以起点要多异或一次。在上面的基础上,遍历异或取最大即可。

#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e5+7;
const int mod=1e9+7;
int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+ch-'0';
        ch=getchar();
    }
    return x*f;
}

int T,n,m,a[maxn],d[maxn];
int main(){
	T=read();
	while(T--){
		memset(d,0,sizeof(d));
		n=read();	m=read();
		for(int i=1;i<=n;i++)	a[i]=read();
		while(m--){
			int x,y;
			x=read();	y=read();
			d[x]++;	d[y]++;
		}
		//判断是否存在欧拉通/回路 
		int cnt=0;
		for(int i=1;i<=n;i++){
			if(d[i]&1)	cnt++;
		}
		if(cnt&&cnt!=2){
			printf("Impossible\n");
			continue;
		}
		
		int ans=0,temp=0;
		for(int i=1;i<=n;i++) {
			d[i]=(d[i]+1)>>1;	//向上取整 
			if(d[i]&1)	ans^=a[i];
		}
		//欧拉回路时,起点与终点未知且相等,要多取一次起点 
		if(!cnt){
			for(int i=1;i<=n;i++)	temp=max(temp,ans^a[i]);
			ans=temp;
		}
		printf("%d\n",ans);
	}
}

/*
1
6 10
1 2 4 8 16 32
1 6
1 5
1 4
1 2
2 5
2 4
2 3
3 4
4 5
5 6

52

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值