CF103B: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 ≤ 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.

Examples
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 and 22 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.

题意:

很久很久以前的一天,一位美男子来到海边,海上狂风大作。美男子希望在海中找到美人鱼,但是很不幸他只找到了章鱼怪。

 

然而,在世界的另一端,人们正在积极的收集怪物的行为信息,以便研制出强大的武器来对付章鱼怪。由于地震的多发,以及恶劣的天气,使得我们的卫星不能很好的定位怪物,从而不能很好的命中目标。第一次射击的分析结果会反映在一张由n个点和m条边组成的无向图上。现在让我们来确定这张图是不是可以被认为是章鱼怪。

 

为了简单起见,我们假设章鱼怪的形状是这样,他有一个球形的身体,然后有很多触须连接在他的身上。可以表现为一张无向图,在图中可以被认为由三棵或者更多的树(代表触须)组成,这些树的根在图中处在一个环中(这个环代表球形身体)。

 

题目保证,在图中没有重复的边,也没有自环。

思路:容易看出仅需要判断图是否只有一个环和所有点连通。

# include <stdio.h>
int pre[1001];

int find(int x)
{
    if(x != pre[x])
        pre[x] = find(pre[x]);
    return pre[x];
}

int main()
{
    int n, m, a, b, flag, fail, road;
    while(~scanf("%d%d",&n,&m))
    {
        flag = road = fail = 0;
        for(int i=1; i<=n; ++i)
            pre[i] = i;
        while(m--)
        {
            scanf("%d%d",&a,&b);
            if(fail)
                continue;
            int px = find(a);
            int py = find(b);
            if(px != py)
            {
                ++road;
                pre[px] = py;
            }
            else
            {
                if(!flag)
                {
                    ++road;
                    flag = 1;
                }
                else
                    fail = 1;
            }
        }
        if(!fail && flag && road == n)
            puts("FHTAGN!");
        else
            puts("NO");
    }
    return 0;
}


转载于:https://www.cnblogs.com/junior19/p/6729920.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值