hihocoder 1176

hihocoder 1176

题意:N,M。分别表示岛屿数量和木桥数量,一笔画

分析:欧拉路问题(给定无孤立结点图G,若存在一条路,经过图中每边一次且仅一次,该条路称为欧拉路)

欧拉路的条件

  1. 一个无向图存在欧拉路当且仅当该图是连通
  2. 且只有2个点的度数是奇数(此时这两个点只能作为欧拉路径的起点和终点

用并查集判断第一个条件

第二个直接用数组存

 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <string>
 6 #include <map>
 7 #include <iomanip>
 8 #include <algorithm>
 9 #include <queue>
10 #include <stack>
11 #include <set>
12 #include <vector>
13 //const int maxn = 1e5+5;
14 #define ll long long
15 #define MAX INT_MAX
16 #define FOR(i,a,b) for( int i = a;i <= b;++i)
17 using namespace std;
18 int fh[11000],degree[11000];
19 int n,m,cnt1,cnt2,a,b;
20 int findhead(int k)  //并查集判断条件一
21 {
22     if(fh[k]==-1)
23         return k;
24      return fh[k]=findhead(fh[k]);
25 }
26 int main()
27 {
28     //    freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
29     //    freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
30     cin>>n>>m;
31     memset(fh,-1,sizeof(fh));
32     for(int i=1;i<=m;++i)
33     {
34         cin>>a>>b;
35         degree[a]++;
36         degree[b]++;
37         a=findhead(a);
38         b=findhead(b);
39 
40         if(a!=b)
41             fh[a]=b;
42     }
43     for(int i=1;i<=n;++i)
44     {
45         if(findhead(i)==i)
46         {
47             cnt1++;
48         }
49         if(degree[i]%2==1)
50         {
51             cnt2++;
52         }
53     }
54     //cout<<cnt1<<"   "<<cnt2<<endl;
55     if(cnt1==1&&(cnt2==0||cnt2==2))  //head点只有一个切 度奇数点为2或者0
56     {
57         cout<<"Full";
58     }
59     else cout<<"Part";
60 
61 
62 }

猜你喜欢

转载自www.cnblogs.com/jrfr/p/10739697.html