The Best Path(欧拉路)

Problem Description

Alice is planning her travel route in a beautiful valley. In this valley, there are N lakes, and M 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,…,an) for each lake. If the path she finds is P0→P1→…→Pt, the lucky number of this trip would be aP0XORaP1XOR…XORaPt. She want to make this number as large as possible. Can you help her?

Input

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

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

The i-th line of the next M lines contains two integers ui and vi representing the i-th river between the ui-th lake and vi-th lake. It is possible that ui=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

题意:

简单的求个欧拉路,给每个店付一个权值,求权值最大的路径。一个点经过的次数是(该点的度数+1)/2但是要注意如果是欧拉回路,起点要多走一次所以多异或一次

提一点欧拉路有关的小知识:
如果欧拉路或者欧拉回路存在那么该图肯定是连通图(可用并查集来判断)
无向连通图中,如果图中的点都是偶点那么存在欧拉回路,如果有两个奇点那么一个是起点一个是终点,如果大于两个奇点就不存在欧拉路。
有向连通图中,可以将点的出度记为1,入度记为-1,那么这个点上所有的出度和入度之和就是它的度数。若存在欧拉路那么有一个点度数是1,一个点是-1,其他的点是0。1是起点,-1是终点。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll t,n,m;
const ll N=100005;
ll val[N],deg[N];
ll fa[N];
inline ll read()
{
    ll xx=0,ff=1;
    char ch;
    ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-') ff=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        xx=(xx<<3)+(xx<<1)+ch-'0';
        ch=getchar();
    }
    return xx*ff;
}
ll gen(ll x){
    if(x==fa[x]) return x;
    return fa[x]=gen(fa[x]);
}

int main(){
    t=read();
    while(t--){
        ll res=0;
        n=read(),m=read();

        for(ll i=1;i<=n;i++){
            val[i]=read();
            fa[i]=i;
        }

        memset(deg,0,sizeof deg);
        ll u,v,fu,fv;
        for(ll i=0;i<m;i++){
            u=read(),v=read();
            deg[u]++,deg[v]++;
            fu=gen(u),fv=gen(v);
            if(fu!=fv){
                fa[fu]=fv;
            }
        }

        ll cnt=0,flag=0;
        for(ll i=1;i<=n;i++){
            if(deg[i]%2==1)cnt++;
            if(i==fa[i]) flag++;

            ll len=(deg[i]+1)/2;
            if(len%2){
                res=res^val[i];
            }
        }
        if(flag>1||cnt>2){
            printf("Impossible\n");
        }
        else{
            if(cnt==0){
                ll ma=res;
                for(ll i=1;i<=n;i++){
                    res=max(ma^val[i],res);
                }
            }
            printf("%lld\n",res);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值