牛客小白月赛2 D题虚虚实实

题目链接:https://www.nowcoder.com/acm/contest/86/D

解题思路:这题目就是判断是否存在欧拉路径。由无向图存在欧拉路径的充分必要条件可知先判断是否联通,再判断是否有0个或者2个奇数度数点

代码:

 1 #include<iostream>
 2 #include<string.h>
 3 using namespace std;
 4 int a[35];
 5 int fa[35];
 6 int Find(int x){
 7     if(fa[x]==x) return x;
 8     else{
 9         fa[x]=Find(fa[x]);
10         return fa[x];
11     }
12 }
13 void init(int x,int y){
14     fa[Find(x)]=Find(y);
15     return ;
16 }
17 int main(){
18     int T;
19     cin>>T;
20     while(T--){
21         int n,m;
22         cin>>n>>m;
23         for(int i=0;i<34;i++){
24             fa[i]=i;
25         }
26         memset(a,0,sizeof(a));
27         for(int i=0;i<m;i++){
28             int x,y;
29             cin>>x>>y;
30             if(x!=y){
31                 if(Find(x)!=Find(y)){
32                     init(x,y);
33                 }
34                 a[x]++;
35                 a[y]++;
36             }
37         }
38         int sum=0;
39         int flag=1;
40         int f=Find(1);
41         for(int i=1;i<=n;i++){
42             if(a[i]%2==1){
43                 sum++;
44             }
45             if(Find(i)!=f){
46                 flag=0;
47                 break;
48             }
49         }
50         if((sum==0||sum==2)&&flag==1){
51             cout<<"Zhen"<<endl;
52         }else{
53             cout<<"Xun"<<endl;
54         }
55     }
56     return 0;
57 }
原文地址:https://www.cnblogs.com/ISGuXing/p/8908035.html