题目连接: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
  1. 6 66 36 45 12 51 45 4
Output
  1. FHTAGN!
Input
  1. 6 55 64 63 15 11 2
Output
  1. NO
  2.  
  3. 题目大意:挺有意思的题目,寻找克苏鲁,目前卫星拍到了一个图形,抽象成一个无向图,要求你判断是否是克苏鲁。克苏鲁的特点是类似于章鱼,所以判定是克苏鲁的条件是图由几棵树组成,并且树的根必须连成环,保证不存在重边和自环。
  4.  
  5. 解题思路:刚开始想暴力,dfs找环然后判定环中的每个节点是否练着一棵树,但是这样明显时间复杂度很高,后来想到利用图和树的特性,根据题目要求,这个图必定是一个连通图,一个连通图中有一个环,其余全是树,所以边数一定等于节点数,边数和节点数已经给出,所以只需要dfs判断此图是否连通即可。若连通并且边数等于节点数,那么则一定符合要求。
  6.  
  7. 代码如下:
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n,m;
  6. ][]= {};
  7. ]= {};
  8. ;
  9.  
  10. void dfs(int now)
  11. {
  12. ans++;
  13. vis[now]=;
  14. ; i<=n; i++)
  15. {
  16. if(!vis[i]&&g[now][i])
  17. dfs(i);
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. int u,v;
  24. cin>>n>>m;
  25. ; i<m; i++)
  26. {
  27. scanf("%d%d",&u,&v);
  28. g[u][v]=g[v][u]=;
  29. }
  30. dfs(u);
  31. if(n==ans)
  32. {
  33. if(n==m)
  34. cout<<"FHTAGN!"<<endl;
  35. else
  36. cout<<"NO"<<endl;
  37. }
  38. else
  39. cout<<"NO"<<endl;
  40. }

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. 什么是 IRC?

    IRC是Internet Relay Chat 的英文缩写,中文一般称为互联网中继聊天.它是由芬兰人Jarkko Oikarinen于1988年首创的一种网络聊天协议.经过十年的发展,目前世界上有超过 ...

  2. python - web自动化测试 - 元素操作 - 窗口切换

    # -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: 元素操作-切换.py @ide: PyCharm Community ...

  3. unity值得推荐的网址

    免费字体下载网站:http://www.dafont.com/ 免费声音文件下载网站:http://freesound.org/          http://incompetech.com/mus ...

  4. 孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库

    孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第三天.感觉这个东西学习起来还是那么困 ...

  5. PPC Windows Mobile判断网络(gprs)连接与否代码

    转载 在PPC的开发中有时需要判断网络什么时候连接,什么时候断开,并且做出相应的处理.比如说TimeSyncPPC程序,就需要在网络连接之后得到消息然后去下载时间同步信息,因此就必须要一种方法让Tim ...

  6. 对象内存 (扩展 Data Structure Alignment)

    对于一个class object来说,我们需要多少内存才能表现出来,大致分为3类,这里在前面文章有内存图 (1)非静态数据成员的综合大小,这也符合了c++对象模型的结构 (2)填充字节,就是我们所说的 ...

  7. unity射线碰撞检测+LayerMask的使用

    射线在unity中是个很方便的东西,对对象查找.多用于碰撞检测(如:子弹飞行是否击中目标).角色移动等提供了很大的帮助,在此做个总结与大家分享下 ,若有不足欢迎吐槽 好了,话补多说啦,直接进入主题: ...

  8. 重写Android相机适配不同的设备,对于相机旋转角度问题解决方案

    Android开发中经常需要重写相机,由此会导致一些旋转的情况(不同的设备摄像头角度是不一样的),此处按照解决思路给出解决方案: 情形一:只需要旋转摄像头方向以及最终的照片,注意两者需要保持一致 1. ...

  9. c# 对XML 解析 和 序列化

    /// <summary> /// 解析XML 数据 /// </summary> /// <param name="re"></para ...

  10. SQL Server 重新编译存储过程的方式有三种

    SQL Server 中,强制重新编译存储过程的方式有三种: sp_recompile 系统存储过程强制在下次执行存储过程时对其重新编译.具体方法是:从过程缓存中删除现有计划,强制在下次运行该过程时创 ...