当前位置:网站首页>A - deep sea exploration
A - deep sea exploration
2022-06-28 08:31:00 【Angeliaaa】
A long, long time ago , A beautiful man came to the seaside , The sea was stormy . The beautiful man hopes to find a mermaid in the sea , But unfortunately he only found the octopus .
However , On the other side of the world , People are actively collecting information about monster behavior , In order to develop powerful weapons to deal with the octopus monster . Due to the frequent occurrence of earthquakes , And bad weather , So our satellites can't locate monsters well , So they can't hit the target well . The analysis results of the first shot will be reflected in a picture by n Points and m On an undirected graph composed of edges . Now let's determine whether this picture can be regarded as an octopus monster .
For the sake of simplicity , We assume that the shape of the octopus monster is like this , He has a spherical body , Then there are many tentacles attached to his body . It can be represented as an undirected graph , In the graph, it can be considered as three or more trees ( Representative tentacle ) form , The roots of these trees are in a ring in the graph ( This ring represents a spherical body ).
Title assurance , There are no duplicate edges in the graph , There is no self link .
Input
A single set of test data
The first line gives two numbers ,n Represents the number of points in the graph ,m Represents the number of edges in the graph . (1≤ n≤100,0≤ m≤ n*(n-1)/2 )
Next m Rows give information about edges ,
Each line has two upper numbers x,y (1≤ x,y≤ n,x≠y)
Indication point x Sum point y There are edges between them . At most one edge of each pair of points is connected , Point itself will not have edge to itself .
Output
All in one line , If the given graph is considered to be an octopus, output "FHTAGN!"( No quotes ), Otherwise output "NO"( No quotes ).
Sample Input
6 6 6 3 6 4 5 1 2 5 1 4 5 4
Sample Output
FHTAGN!
The question : Give two numbers n,m, On behalf of n Nodes m side , And then follow m Two numbers per row a,b representative a Follow b Connected to a . Ask if the picture is an octopus , The shape of an octopus is that it has a ring and can form a connected graph . Yes, output FHTAGN!, Otherwise output NO.
Ideas : This problem only needs to satisfy two conditions to form an octopus ,1. Construct connected graph .2. There is only one ring . Using parallel search set to realize , Merge during input , If two nodes have a common ancestor, it means that a ring is formed , Then add one to the number of rings . And judge how many ancestors there are after the merger , If there is a ring and only one ancestor , It can form the shape of an octopus . The code is as follows :
#include<stdio.h>
#include<string.h>
int f[110];
int getf(int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}
int merge(int u,int v)
{
int t1,t2;
t1=getf(u);
t2=getf(v);
if(t1!=t2)
{
f[t2]=t1;
return 0;
}
return 1;
}
int main()
{
int i,x,y,n,m,r,sum;
while(~scanf("%d %d",&n,&m))
{
r=0;
sum=0;
for(i=1; i<=n; i++)
f[i]=i;
for(i=0; i<m; i++)
{
scanf("%d %d",&x,&y);
if(merge(x,y)==1)
r++;
}
for(i=1; i<=n; i++)
{
if(f[i]==i)
sum++;
}
if(sum==1&&r==1)
printf("FHTAGN!\n");
else
printf("NO\n");
}
return 0;
}
边栏推荐
- Quelle est la largeur de bande du serveur de bavardage sonore pour des centaines de millions de personnes en même temps?
- Ffmpeg (I) AV_ register_ all()
- Map. ToCharArray( ),Map. getOrDefault(). Map. charAt()
- 抖音服务器带宽有多大,才能供上亿人同时刷?
- B_QuRT_User_Guide(28)
- In flood fighting and disaster relief, the city donated 100000 yuan of love materials to help Yingde
- Oracle RAC -- understanding of VIP
- 【学习笔记】模拟
- [learning notes] differential constraint
- The preliminary round of the sixth season of 2022 perfect children's model Foshan competition area came to a successful conclusion
猜你喜欢

App automated testing appium tutorial 2 - ADB command

DB

B_ QuRT_ User_ Guide(26)

Usage record of Xintang nuc980: self made development board (based on nuc980dk61yc)

ROS notes (09) - query and setting of parameters

ROS notes (08) - definition and use of service data

【学习笔记】最短路 +生成树

抖音服务器带宽有多大,才能供上亿人同时刷?

微内核Zephyr获众多厂家支持!

Children's unit of 2022 Paris fashion week ended successfully at Wuhan station on June 19
随机推荐
Anniversary party
Resolution of Rac grid failure to start after server restart
PC端隐藏滚动条
B_ QuRT_ User_ Guide(28)
cuda和cudnn和tensorrt的理解
块级元素上下左右居中的两个小技巧
广州:金融新活水 文企新机遇
Sword finger offer 03 Duplicate number in array
App automated testing appium Tutorial Part 1 - advanced supplementary content
ROS notes (08) - definition and use of service data
Idea related issues
webrtc优势与模块拆分
The micro kernel zephyr is supported by many manufacturers!
【学习笔记】拟阵
神殿
Uvcgan: unt vision transformer cycle-consistent Gan for unpropared image-to-image translation
B_QuRT_User_Guide(26)
解决npm ERR! Unexpected end of JSON input while parsing near问题
微内核Zephyr获众多厂家支持!
Redis deployment under Linux & redis startup