【刷题】HDU 5883 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 \(P_0 \rightarrow P_1 \rightarrow ... \rightarrow P_t\) , the lucky number of this trip would be \(a_{P_0} \quad XOR \quad a_{P_1} \quad XOR \quad... \quad XOR \quad a_{P_t}\) . 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

Description(CHN)

给你一个无向图,每个点有权值,你要从某一个点出发,使得一笔画经过所有的路,且使得经过的节点的权值XOR运算最大

Solution

对于一个图,如果奇度数点数不为0也不为2,那么无解
如果为2,则欧拉路径固定,每个点经过的次数为它的度数/2,直接算就可以了
如果为0,即存在欧拉回路,这种情况相较于上一种情况就是起点多经过了一次,因为要回到起点,所以枚举起点,chkmax就好了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=100000+10;
int T,n,m,val[MAXN],d[MAXN],sum[2],ans,now;
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
int main()
{
    read(T);
    while(T--)
    {
        read(n);read(m);
        for(register int i=1;i<=n;++i)read(val[i]),d[i]=0;
        for(register int i=1;i<=m;++i)
        {
            int u,v;read(u);read(v);
            d[u]++;d[v]++;
        }
        sum[0]=sum[1]=0;
        for(register int i=1;i<=n;++i)sum[d[i]&1]++;
        if(sum[1]!=0&&sum[1]!=2)
        {
            puts("Impossible");
            continue;
        }
        ans=now=0;
        for(register int i=1;i<=n;++i)
            if((d[i]>>1)&1)now^=val[i];
        if(sum[1]==2)
        {
            for(register int i=1;i<=n;++i)
                if(d[i]&1)now^=val[i];
            ans=now;
        }
        else
            for(register int i=1;i<=n;++i)chkmax(ans,now^val[i]);
        write(ans,'\n');
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hongyj/p/9460476.html
今日推荐