zoukankan      html  css  js  c++  java
  • 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


    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
    ", res);
                }
                else
                {
                    int mx  = -1;
                    for(int i = 1; i <= n; i++)
                    {
                        mx = max(mx, res ^ val[i]);
                    }
                    printf("%d
    ", mx);
                }
            }
            else
            {
                printf("Impossible
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    Android应用程序组件Content Provider简要介绍和学习计划
    本人其它博客
    Android应用程序组件Content Provider的启动过程源代码分析
    Android应用程序组件Content Provider应用实例
    Team Foundation Server 2010 Performance Tuning – Lessons learned
    Katapult:KDE 桌面辅佐序次
    Informix IDS 11系统打点(918考试)认证指南,第2局部系统活动监督(3)
    Amarok 1.4.6
    Informix IDS 11琐细管理(918测验)认证指南,第1部分IDS安设和设置(1)
    KTorrent 2.2 公布
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5879900.html
Copyright © 2011-2022 走看看