hdu 5883 The Best Path【欧拉路经/欧拉回路】

本文探讨了在一个给定点和边构成的图中寻找一条特殊路径的问题,该路径需经过每条边恰好一次,并使得路径上的点权值通过异或运算达到最大值。文章提供了问题的背景描述、解决思路及AC代码。

The Best Path

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 547    Accepted Submission(s): 222

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

Source

2016 ACM/ICPC Asia Regional Qingdao Online

 

题目大意:

 给你n个点,m条边,让你找一条路径,使得经过所有边。每个点都有对应的点权值,求这样一条满足的路径,使得其点权值的亦或值最大。


思路:


1、对应无向图的欧拉回路,其包含0个奇数度的点,对应无向图的欧拉路径,其包含两个奇数度的点。


2、那么我们在输入边的时候统计每个点的度数,那么对应这条满足欧拉路经/欧拉回路的路径的每个点贡献出的价值为():(degree【i】+1)/2)%2;


3、如果单单是一条欧拉路经的话,从1号点一直亦或到最后一个点即可,如果这是一条欧拉回路的话,我们还要枚举出一个起点(起点需要经过两次),用刚才亦或完的ans,再亦或一次起点,维护最大值即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[100500];
int degree[100500];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        memset(degree,0,sizeof(degree));
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        if(m==0)
        {
            printf("Impossible\n");
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            degree[x]++;
            degree[y]++;
        }
        int ji=0;
        for(int i=1;i<=n;i++)
        {
            if(degree[i]%2==1)ji++;
        }
        if(ji==0||ji==2)
        {
            int ans=0;
            for(int i=1;i<=n;i++)
            {
                if(((degree[i]+1)/2)%2==1)
                {
                    ans^=a[i];
                }
            }
            if(ji==0)
            {
                int tmp=ans;
                for(int i=1;i<=n;i++)
                {
                    ans=max(ans,ans^a[i]);
                }
            }
            printf("%d\n",ans);
        }
        else printf("Impossible\n");
    }
}






评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值