zoukankan      html  css  js  c++  java
  • HDU 5883 The Best Path

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define rep(i,a,b) for(int i=a;i<=b;++i)
     4 #define ms(arr,a) memset(arr,a,sizeof arr)
     5 const int maxn=1e5+5;
     6 int w[maxn],a[maxn];
     7 int fa[maxn];
     8 int root(int x)
     9 {
    10     int r=x;
    11     while(r!=fa[r])r=fa[r];
    12     while(x!=r)
    13     {
    14         int tmp=fa[x];
    15         fa[x]=r;
    16         x=tmp;
    17     }
    18     return r;
    19 }
    20 int main()
    21 {
    22     //freopen("Input.txt","r",stdin);
    23     //freopen("Output.txt","w",stdout);
    24     int T;scanf("%d",&T);
    25     while(T--)
    26     {
    27         ms(a,0);
    28         int n,m;scanf("%d%d",&n,&m);
    29         rep(i,1,n)fa[i]=i;
    30         rep(i,1,n)scanf("%d",w+i);
    31         rep(i,1,m)
    32         {
    33             int u,v;scanf("%d%d",&u,&v);
    34             a[u]++;
    35             a[v]++;
    36             if(root(u)!=root(v))fa[u]=v;
    37         }
    38         int sum_graph=0;
    39         rep(i,1,n)
    40         {
    41             if(fa[i]==i)sum_graph++;
    42         }
    43         if(sum_graph!=1){printf("Impossible
    ");continue;}
    44         int odd=0;
    45         rep(i,1,n)if(a[i]&1)odd++;
    46         if(odd>2){printf("Impossible
    ");continue;}
    47         int ans=0;
    48         if(odd==2)
    49         {
    50             rep(i,1,n)
    51             {
    52                 int tp=(a[i]+1)/2;
    53                 if(tp&1)ans^=w[i];
    54             }
    55         }
    56         if(odd==0)
    57         {
    58             rep(i,1,n)
    59             {
    60                 int tp=a[i]/2;
    61                 if(tp&1)ans^=w[i];
    62             }
    63             int Max=ans;
    64             rep(i,1,n)
    65             {
    66                 Max=max(Max,ans^w[i]);
    67             }
    68             ans=Max;
    69         }
    70         printf("%d
    ",ans);
    71     }
    72     //freopen("CON","w",stdout);
    73     //system("start Output.txt");
    74 }
  • 相关阅读:
    C语言版本:单链表的实现(优化版本)
    C语言版本:单链表的实现
    C语言版本:顺序表的实现
    C++:多态浅析
    C++:同名隐藏和赋值兼容规则
    C++:钻石继承与虚继承
    C++:派生类的构造函数和析构函数的调用顺序
    Docker安装和使用
    Node10.15.0的安装
    碎碎叨叨
  • 原文地址:https://www.cnblogs.com/maoruimas/p/9511133.html
Copyright © 2011-2022 走看看