题目连接: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. DOS程序员手册(十三)

    744页 在DPMI 1.0下,系统会修改并重新装载所有含选择符的段寄存器,并且将所有 含有要释放的选择符的寄存器清空为0. 客户程序绝不能修改或释放该功能分配的任何描述符.Int 31h.功能010 ...

  2. Lua2

    1. 迭代器与Closure 在Lua中,迭代器通常为函数,每调用一次函数,即返回集合中的“下一个”元素.每个迭代器都需要在每次成功调用之间保持一些状态,这样才能知道它所在的位置和下一次遍历时的位置. ...

  3. linux shell 总结 (整理)

    ls /usr/bin/ info #路径操作 dirname basename #“”和‘’与 ` ` 在shell变量中的区别 “ ” 允许通过$符引用其他变量 ‘’禁止引用其他变量符,视为普通字 ...

  4. VS调试时不捕捉Exception

    在方法上添加 [DebuggerHidden] 这样,发生错误的时候,VS就不会捕捉到这个错误,否则,会在错误的地方中断. [DebuggerHidden] public void DeleteAll ...

  5. 使用hadoop统计多个文本中每个单词数目

    程序源码 import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Con ...

  6. 【bzoj1875】[SDOI2009]HH去散步 矩阵乘法

    题目描述 一张N个点M条边的无向图,从A走到B,要求:每一次不能立刻沿着上一次的边的反方向返回.求方案数. 输入 第一行:五个整数N,M,t,A,B. N表示学校里的路口的个数 M表示学校里的路的条数 ...

  7. Codeforces 846D Monitor(简单二分+二维BIT)

    D. Monitor time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  8. 【CF #313】

    B题为啥交换一下搜索顺序就会TLE啊QAQ C题原来要预处理乘法逆元才能过啊QAQ 我沙茶啊我QAQ[还是太弱 嗯A题就是道水题 B题就是字符串Hash+暴力搜 C题就是组合数+容斥原理

  9. poj 1764 Dice Contest

    题目戳这里. 首先我要吐槽这个题目描述不清.\(2\)对着选手,那选手朝那边?看完别人写的程序后我才知道选手对着目标所在的方向(或左或右). 然后这道题还是不错的,因为他交给我矩阵乘法不只有常规意义下 ...

  10. H-1-B签证简介

    H 1-B签证简介 H 1-B签证是美国雇主为外籍高级技术人员申请的一种工作签证,有效期为3年,可在到期后再延长6年.每年新的配额是6.5万人,另外还有2万个名额是留给在美国获得高级学位(硕士以上学位 ...