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》——第2章:链表——题目5
2014-03-18 02:32 题目:给定两个由单链表表示的数字,返回它们的和.比如(9->9) + (1->2) = 0->2->1,99 + 21 = 120. 解法:逐 ...
- USACO Section1.2 Dual Palindromes 解题报告
dualpal解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...
- badboy录制提示当前页面的脚本发生错误
利用badboy录制时,发生了错误: 网上查了查,说badboy默认使用IE浏览器,打开Internet选项—>高级,图中的两个选项不要勾选即可 然鹅,然鹅,并没有作用... 请教了好心的同行, ...
- 【志银】MySQL命令总结
===0-MySQL密码设置===0.1-登入和进入MySQL数据库: 0.1.1-登入MySQL数据库:C:\Users\Administrator>mysql -u用户名 -hMySQL服务 ...
- 1099 Build A Binary Search Tree (30 分)(查找二叉树)
还是中序遍历建树 #include<bits/stdc++.h> using namespace std; ; struct node { int data; int L,R; }s[N] ...
- Codeforces Round #328(Div2)
CodeForces 592A 题意:在8*8棋盘里,有黑白棋,F1选手(W棋往上-->最后至目标点:第1行)先走,F2选手(B棋往下-->最后至目标点:第8行)其次.棋子数不一定相等,F ...
- 在python中如何比较两个float类型的数据是否相等
奇怪的现象 前几天跟同事聊起来,在计算机内部float比较是很坑爹的事情.比方说,0.1+0.2得到的结果竟然不是0.3? >>> 0.1+0.2 0.300000000000000 ...
- LeetCode(一)
Q&A ONE Given an array of integers, return indices of the two numbers such that they add up to a ...
- 【距离GDOI:128天】【POJ2778】DNA Sequence(AC自动机+矩阵加速)
已经128天了?怎么觉得上次倒计时150天的日子还很近啊 ....好吧为了把AC自动机搞透我也是蛮拼的..把1030和这道题对比了无数遍...最终结论是...无视时间复杂度,1030可以用这种写法解. ...
- TensorFlow应用实战 | TensorFlow基础知识
挺长的~超出估计值了~预计阅读时间20分钟. 从helloworld开始 mkdir 1.helloworld cd 1.helloworldvim helloworld.py 代码: # -*- c ...