codeforces-103B
题目连接:http://codeforces.com/contest/103/problem/B
2 seconds
256 megabytes
standard input
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.
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.
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
6 66 36 45 12 51 45 4
FHTAGN!
6 55 64 63 15 11 2
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的更多相关文章
- CodeForces - 103B(思维+dfs找环)
题意 https://vjudge.net/problem/CodeForces-103B 很久很久以前的一天,一位美男子来到海边,海上狂风大作.美男子希望在海中找到美人鱼 ,但是很不幸他只找到了章鱼 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- 《Cracking the Coding Interview》——第16章:线程与锁——题目2
2014-04-27 19:14 题目:如何测量上下文切换的时间? 解法:首先,上下文切换是什么,一搜就知道.对于这么一个极短的时间,要测量的话,可以通过放大N倍的方法.比如:有A和B两件事,并且经常 ...
- 安装LoadRunner11报缺少vc2005_sp1_with_atl_fix_redist的错误
找到安装程序自带的lrunner\Chs\prerequisites\vc2005_sp1_redist,双击运行vcredist_x86.exe,再重新安装LoadRunner即可成功. 参考资料: ...
- IOS测试,打不开要测试的APP怎么办?设置信任
步骤:设置-->通用-->设备管理-->企业级应用-->信任 具体教程:http://jingyan.baidu.com/article/ab69b27085ab002ca71 ...
- 关于MySQL查询优化 の 30条忠告
撸自:http://www.jincon.com/archives/120/ 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避 ...
- [OpenCV]Mat类详解
http://blog.csdn.net/yang_xian521/article/details/7107786 Preface Mat:Matrix Mat类可以被看做是opencv中C++版本的 ...
- XGBoost——机器学习--周振洋
XGBoost——机器学习(理论+图解+安装方法+python代码) 目录 一.集成算法思想 二.XGBoost基本思想 三.MacOS安装XGBoost 四.用python实现XGBoost算法 在 ...
- SpringMVC<一> 基本结构与配置
刚刚踏入SpringMVC的学习,有一定Strust2的使用经验,边看书看博客,边总结,如有不对的地方还希望各位大佬多多指正. Spring 响应过程与结构 (1)用户在客户端发送一个HTTP请求,W ...
- SQL 唯一标识 写法
创建唯一标识的方法~16位唯一标识 SELECT LTRIM(STR(CONVERT(varchar(100), GETDATE(), 112)))+right(cast(power(10,6) as ...
- GDI+ 双缓存 和 刷新桌面(F5)
GDI+双缓存 POINT currentPoint; GetCursorPos(¤tPoint); HWND hWnd = ::GetDesktopWindow(); int n ...
- template.js 模板引擎
例子: html代码: <div id="box"></div> css代码: table{ border-collapse: collapse; text ...