HDU 5883 The Best Path 欧拉路径/回路

Description

Alice is planning her travel route in a beautiful valley. In this valley, there are NN lakes, and MM rivers linking these lakes. Alice wants to start her trip from one lake, and enjoys the landscape by boat. That means she need to set up a path which go through every river exactly once. In addition, Alice has a specific number (a1,a2,…,ana1,a2,…,an) for each lake. If the path she finds is P0→P1→…→PtP0→P1→…→Pt, the lucky number of this trip would be aP0XORaP1XOR…XORaPtaP0XORaP1XOR…XORaPt. She want to make this number as large as possible. Can you help her?

Input

The first line of input contains an integer tt, the number of test cases. tt test cases follow.

For each test case, in the first line there are two positive integers N (N≤100000)N (N≤100000) and M (M≤500000)M (M≤500000), as described above. The ii-th line of the next NN lines contains an integer ai(∀i,0≤ai≤10000)ai(∀i,0≤ai≤10000) representing the number of the ii-th lake.

The ii-th line of the next MM lines contains two integers uiui and vivi representing the ii-th river between the uiui-th lake and vivi-th lake. It is possible that ui=viui=vi.

Output

For each test cases, output the largest lucky number. If it dose not have any path, output “Impossible”.

Sample Input

2
3 2
3
4
5
1 2
2 3
4 3
1
2
3
4
1 2
2 3
2 4

Sample Output

2
Impossible

题意:
给出n个点,m条无向边,每个点有权值。问是否可以经过每条边恰好一次,若不可以输出”Impossible”,否则求出所有可行的路径中,经过节点的最大异或和。

题解:
首先就是无向图判断是否存在欧拉路径/回路,利用点的度数来判断。
对于欧拉路径,如果一个点的度数为3、4、7、8、11、12(4k/4k+3,其中k为奇数)…等等,则这个节点必经过偶数次,而如果是1、 2、5、 6、 9、 10…(4k+1,4k+2)等等,则经过奇数次。更一般的,记i节点的度数为in[i],如果(in[i]+1)/2是偶数,则这个节点对答案没有贡献,反之。所以对于欧拉路径,最大的异或和是一个确定值。
对于欧拉回路。起点会在之前的基础上多经过一次,所以可以枚举起点的位置,找到最大的异或和。

为什么pos的初始值会莫名其妙变成-1!!!

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

inline 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*10+ch-'0';ch=getchar();}
    return x*f;
}

const int N = 100000 + 10;
const int M = 500000 + 10;

int n,m;
int a[N],in[N];

struct node{
    int pre,v;
}e[M<<1];

int num=0,head[N];
void addedge(int from,int to){
    e[++num].pre=head[from],head[from]=num;
    e[num].v=to;
}

bool vis[N];
void dfs(int u){
    vis[u]=1;
    for(int i=head[u];i;i=e[i].pre){
        int v=e[i].v;
        if(vis[v]) continue;
        dfs(v);
    }
}

inline int Max(int a,int b) {return a>b?a:b;}

int t,ans=0;

#define ms(x,y) memset(x,y,sizeof(x))
void update(){
    ans=0;ms(vis,0);ms(head,0);ms(in,0);
    num=0;
}

int main(){
    t=read();
    while(t--){
       update();
       n=read(),m=read();
       for(register int i=1;i<=n;++i) a[i]=read();
       for(register int i=1;i<=m;++i){
          int u=read(),v=read();
          addedge(u,v);addedge(v,u);
          ++in[u],++in[v];
       }
       dfs(1);
       int sum=0,pos=0;//为什么pos不附初始值的话就默认为-1!!! 
       for(int i=1;i<=n;i++){
          if(!vis[i]) {pos=-1;break;}
          if(in[i]&1) ++sum;
       }
       if(pos==-1||(sum!=0&&sum!=2)) {printf("Impossible\n");continue;}
       for(int i=1;i<=n;++i){
            if(((in[i]+1>>1)&1)) ans^=a[i];
       }      
       if(sum==0){
          int x=ans;ans^=a[1];
          for(int i=2;i<=n;++i) ans=Max(ans,x^a[i]);
       }
       printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值