zoukankan      html  css  js  c++  java
  • hdu5883【欧拉通路】

    题意:n个点m条无向边的图,找一个欧拉通路/回路,下标是p1,p2,p3…pt,然后使得ap1XORap2XOR…XORapt这个值最大。
    思路:
    首先要判断一下这个图是不是联通的,用并查集就好了,然后有个注意点就是可能是单个独立点;
    然后再判断是不是欧拉通路,不是也不行;
    最后计算,最后如果是欧拉回路还要找一个最大起点(终点)。

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N=1e5+10;
    int n;
    
    int pre[N];
    int a[N];
    int in[N];
    
    int Find(int x)
    {
        int r=x;
        while(pre[r]!=r)
            r=pre[r];
        int i=x,j;
        while(pre[i]!=r)
        {
            j=pre[i];
            pre[i]=r;
            i=j;
        }
        return r;
    }
    
    void Union(int x,int y)
    {
        int xx=Find(x);
        int yy=Find(y);
        if(xx!=yy)
            pre[xx]=yy;
    }
    
    void init()
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            pre[i]=i;
            in[i]=0;
        }
    }
    
    int main()
    {
        int T,m,x,y;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            init();
            for(int i=0;i<m;i++)
            {
                scanf("%d%d",&x,&y);
                in[x]++;
                in[y]++;
                Union(x,y);
            }
            int flag=0;
            for(int i=1;i<=n;i++)
            {
                if(pre[i]==i&&in[i])
                    flag++;
                if(flag==2)
                    break;
            }
            if(flag==2)
            {
                puts("Impossible");
                continue;
            }
            int res,num=0;
            for(int i=1;i<=n;i++)
            {
                if(in[i]&1)
                    num++;
            }
            if(!(!num||num==2))
            {
                puts("Impossible");
                    continue;
            }
            int ans=0;
            for(int i=1;i<=n;i++)
            {
                res=in[i];
                if(res%2)
                {
                    res/=2;
                    if((res+1)%2)
                        ans^=a[i];
                }
                else
                {
                    res/=2;
                    if(res%2)
                        ans^=a[i];
                }
            }
            //printf("%d
    ",ans);
            if(!num)
            {
                res=ans;
                for(int i=1;i<=n;i++)
                {
                    res=max(res,ans^a[i]);
                }
                printf("%d
    ",res);
            }
            else
                printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    线程锁lock&rlock
    threading.local
    threading Event
    python中的eval 和 exec 和 execfile
    cloud-init 常见问题
    systemd
    cloud-init 的命令行
    原生js实现Promise
    js 指定位置插入html标签(可编辑div)
    js 实现复制粘贴文本过滤(保留文字和图片)
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934783.html
Copyright © 2011-2022 走看看