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;
    }
  • 相关阅读:
    使用四元数点乘比较插值是否即将完成
    ShadowGun Demo学习(非技术向)
    测试-关于Unity获取子层级内容的几种接口(Transform FindChild, Component GetComponentInChildren,...)
    Javascript事件模型系列(二)事件的捕获-冒泡机制及事件委托机制
    Javascript事件模型系列(一)事件及事件的三种模型
    在代码中设置IE9的默认文档模式
    如何在博客园的文章/随笔中添加可运行的js代码
    jquery插件:仿百度首页可展开收起的消息提示控件
    有“镜头感”的网页是如何实现的
    HTML5 history API实践
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5879900.html
Copyright © 2011-2022 走看看