codeforces-103B

题目连接:http://codeforces.com/contest/103/problem/B

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.

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

题目大意:
挺有意思的题目,寻找克苏鲁,
目前卫星拍到了一个图形,抽象成一个无向图,要求你判断是否是克苏鲁。
克苏鲁的特点是类似于章鱼,
所以判定是克苏鲁的条件是图由几棵树组成,并且树的根必须连成环,保证不存在重边和自环。

解题思路:
刚开始想暴力,dfs找环然后判定环中的每个节点是否练着一棵树,但是这样明显时间复杂度很高,
后来想到利用图和树的特性,
根据题目要求,这个图必定是一个连通图,一个连通图中有一个环,其余全是树,所以边数一定等于节点数,边数和节点数已经给出,
所以只需要dfs判断此图是否连通即可。
若连通并且边数等于节点数,那么则一定符合要求。

代码如下:
#include<bits/stdc++.h>

using namespace std;

int n,m;
int g[105][105]= {0};
bool vis[105]= {0};
int ans=0;

void dfs(int now)
{
    ans++;
    vis[now]=1;
    for(int i=1; i<=n; i++)
    {
        if(!vis[i]&&g[now][i])
                dfs(i);
    }
}

int main()
{
    int u,v;
    cin>>n>>m;
    for(int i=0; i<m; i++)
    {
        scanf("%d%d",&u,&v);
        g[u][v]=g[v][u]=1;
    }
    dfs(u);
    if(n==ans)
    {
        if(n==m)
            cout<<"FHTAGN!"<<endl;
        else
            cout<<"NO"<<endl;
    }
    else
        cout<<"NO"<<endl;
}

 

转载于:https://www.cnblogs.com/cryingrain/p/7694098.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值