codeforce 103B Cthulhu

B. Cthulhu
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

...Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu...

Whereas on the other end of the world Pentagon is actively collecting information trying to predict the monster's behavior and preparing the secret super weapon. Due to high seismic activity and poor weather conditions the satellites haven't yet been able to make clear shots of the monster. The analysis of the first shot resulted in an undirected graph with n vertices and m edges. Now the world's best minds are about to determine whether this graph can be regarded as Cthulhu or not.

To add simplicity, let's suppose that Cthulhu looks from the space like some spherical body with tentacles attached to it. Formally, we shall regard as Cthulhu such an undirected graph that can be represented as a set of three or more rooted trees, whose roots are connected by a simple cycle.

It is guaranteed that the graph contains no multiple edges and self-loops.

Input

The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ ).

Each of the following m lines contains a pair of integers x and y, that show that an edge exists between vertices x and y(1 ≤ x, y ≤ n, x ≠ y). For each pair of vertices there will be at most one edge between them, no edge connects a vertex to itself.

Output

Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.

题意:所有给定的点和边形成一个连通图,至少存在三个有根树,  且树的根形成一个环;  

题解:  

  1. 因为没有重边和自环,因此只要形成一个环就至少有三个点,而三个点就可以  
  2. 分割成三棵有根树。因此只要满足图是联通的且存在环就满足题意。  
  3. 可以用并查集检查是否联通,如果联通且存在环则必定有点数等于边数。  
 1 #include<bits/stdc++.h>  
 2 using namespace std;  
 3 int par[102];  
 4 int find(int x)  
 5 {  
 6     if(par[x]==x)return x;  
 7     return par[x]=find(par[x]);  
 8 }  
 9 void unite(int x,int y)  
10 {  
11     x=find(x);y=find(y);  
12     if(x!=y)par[x]=y;  
13 }  
14 int main()  
15 {  
16     int n,m;scanf("%d%d",&n,&m);  
17     for(int i=0;i<101;i++)par[i]=i;  
18     for(int i=0;i<m;i++)  
19     {  
20         int x,y;scanf("%d%d",&x,&y);  
21         unite(x,y);  
22     }  
23     bool bb=1;  
24     for(int i=2;i<=n;i++)  
25         if(find(1)!=find(i))bb=0;  
26     if(n!=m)bb=0;  
27     if(bb)printf("FHTAGN!
");  
28     else printf("NO
");  
29     return 0;  
30 }  
原文地址:https://www.cnblogs.com/caiyishuai/p/13271066.html