HDOJ5883(歐拉路)

The Best Path

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


this

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 P0P1...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 (N100000) and M (M500000), as described above. The i-th line of the next N lines contains an integer ai(i,0ai10000) 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
 
思路:判斷圖中存在歐拉路/歐拉回路的條件:①圖連通。②圖中結點的度數爲奇數的個數爲0/2。題意要求最大值。由於當圖中存在歐拉回路時,起點要異或兩次,以不一樣的結點爲起點所獲得的異或和可能不一樣。因此當圖中存在歐拉回路時,依次遍歷每一個結點做爲起點,求最大值便可。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 100005;
struct Edge{
    int u, v;
    bool tag;
    int getTo(int u)
    {
        if(this->u == u)    return v;
        else    return this->u;
    }
}es[500005];
int n, m, val[MAXN],deg[MAXN], res;
vector<int> arc[MAXN];
void dfs(int u)
{
    for(int i = 0, size = arc[u].size(); i < size; i++)
    {
        int id = arc[u][i];
        if(!es[id].tag)
        {
            es[id].tag = true;
            int to = es[id].getTo(u);
            dfs(to);
        }
    }
    res ^= val[u];
}

int par[MAXN];
void prep()
{
    for(int i = 0; i < MAXN; i++)
    {
        par[i] = i;
    }
}
int fnd(int x)
{
    if(x == par[x])
    {
        return x;
    }
    return par[x] = fnd(par[x]);
}
void unite(int fa, int son)
{
    int a = fnd(fa);
    int b = fnd(son);
    par[b] = a;
}
int main()
{
//    freopen("input.in", "r", stdin);
    int T;
    scanf("%d", &T);
    while(T--)
    {
        prep();
        res = 0;
        memset(deg, 0, sizeof(deg));
        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n; i++)
        {
            arc[i].clear();
            scanf("%d", &val[i]);
        }
        for(int i = 0; i < m; i++)
        {
            int u, v;
            scanf("%d %d", &u, &v);
            es[i].u = u;
            es[i].v = v;
            es[i].tag = false;
            arc[u].push_back(i);
            arc[v].push_back(i);
            deg[u]++;
            deg[v]++;
            unite(u, v);
        }
        int start = 1;
        int cnt = 0;
        for(int i = 1; i <= n; i++)
        {
            if(deg[i] & 1)
            {
                start = i;
                cnt++;
            }
        }
        int rt = -1, sum = 0;
        for(int i = 1; i <= n; i++)
        {
            int fa = fnd(i);
            if(fa != rt)
            {
                rt = fa;
                sum++;
            }
        }
        if((cnt == 0 || cnt == 2) && sum == 1)
        {
            dfs(start);
            if(cnt == 2)
            {
                printf("%d\n", res);
            }
            else
            {
                int mx  = -1;
                for(int i = 1; i <= n; i++)
                {
                    mx = max(mx, res ^ val[i]);
                }
                printf("%d\n", mx);
            }
        }
        else
        {
            printf("Impossible\n");
        }
    }
    return 0;
}
相關文章
相關標籤/搜索