题目连接: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 66 36 45 12 51 45 4
Output
FHTAGN!
Input
6 55 64 63 15 11 2
Output
NO

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

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

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

using namespace std;

int n,m;
][]= {};
]= {};
;

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

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

codeforces-103B的更多相关文章

  1. CodeForces - 103B(思维+dfs找环)

    题意 https://vjudge.net/problem/CodeForces-103B 很久很久以前的一天,一位美男子来到海边,海上狂风大作.美男子希望在海中找到美人鱼 ,但是很不幸他只找到了章鱼 ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  10. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. 剑指Offer - 九度1362 - 左旋转字符串(Move!Move!!Move!!!)

    剑指Offer - 九度1362 - 左旋转字符串(Move!Move!!Move!!!)2013-11-23 03:05 题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任 ...

  2. 17、bootStrap组件

    1.bootStrap组件 无数可复用的组件,包括字体图标.下拉菜单.导航.警告框.弹出框等更多功能. 2.字体图标 ①不要和其他图标混合使用 ②只能对内容为空的元素起作用 3.下拉菜单 <di ...

  3. 指定user镜像安装的磁盘

    ironic node-update <node uuid> add properties/root_device='{"name":"/dev/sdb&qu ...

  4. codeblocks17.12 debug 报错:ERROR: You need to specify a debugger program in the debuggers's settings.

    DebugERROR: You need to specify a debugger program in the debuggers's settings.(For MinGW compilers, ...

  5. bitbucket相关操作

    常见命令: git checkout -b develop master 创建Develop分支的命令 git checkout master 切换到Master分支 git merge --no-f ...

  6. jQuery选择器示例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. java安全提交笔记【xmind图片】

  8. 火焰图还有perf

    http://www.brendangregg.com/flamegraphs.html zhangyichun大神的systemtap脚本: https://github.com/openresty ...

  9. Oracle设置用户密码永不过期

    1.查看用户的profile是那个,一般是default: select username, profile from dba_users; 2.查看指定概要文件(如default)的密码有效期设置: ...

  10. 【bzoj3561】DZY Loves Math VI 莫比乌斯反演

    题目描述 给定正整数n,m.求   输入 一行两个整数n,m. 输出 一个整数,为答案模1000000007后的值. 样例输入 5 4 样例输出 424 题解 莫比乌斯反演 (为了方便,以下公式默认$ ...