浙工大姗姗杯round2 CodeForces 103B Cthulhu

#include 
#include 
#include 
#include 
using namespace std;

const int maxn = 1000 + 5;

int n,m;
int my_index=0;

vectorG[maxn];
int vis[maxn]={0};

void bfs(int u)
{
	queueQ;
	Q.push(u);
	while(!Q.empty())
	{
		int s = Q.front();
		Q.pop();
		if(!vis[s])
			my_index++;
		vis[s]=1;
		//cout<

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.

浙工大姗姗杯round2 CodeForces 103B Cthulhu_第1张图片

Input

The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 1000 ≤ 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.

Sample test(s)
input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
output
FHTAGN!
input
6 5
5 6
4 6
3 1
5 1
1 2
output
NO
Note

Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and22 and 3, ..., v - 1 and vv and 1.

A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).

A rooted tree is a tree where one vertex is selected to be the root.

题意:给一幅图,让你判断这个图是不是怪物。已知怪物由一个环以及连接环上的触手组成。

思路:并查集找环,符合条件的是所有点都连在一个环上。

#include 
#include 
using namespace std;
int pre[1010];
int find(int x)  
{  
    if(x != pre[x])  
        pre[x] = find(pre[x]);  
    return pre[x];
}
int main()
{
    int n, m;
    while(cin >> n >> m){
        for(int i = 1; i <= n; i++)
            pre[i] = i;
        int flag, a, b, num, flag2;
        flag = num = flag2 = 0;
        for(int i = 0; i < m; i++){
            cin >> a >> b;
            if(flag2)
                continue;
            int px = find(a), py = find(b);
            if(px != py){
                num++;
                pre[px] = py;
            }
            else{
                if(!flag){
                    num++;
                    flag = 1;
                }
                else
                    flag2 = 1;
            }
        }
        if(!flag2 && flag && num == n)
            cout << "FHTAGN!\n";
        else
            cout << "NO\n";
    }
    return 0;
}


你可能感兴趣的:(浙工大姗姗杯round2 CodeForces 103B Cthulhu)