当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

探讨gis三维系统在矿山行业中的应用

TCP

Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu

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

广州:金融新活水 文企新机遇

Kali installation configuration

Devops Basics: Jenkins deployment and use (I)

Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week

PLSQL installation under Windows

Selenium+chromedriver cannot open Google browser page
随机推荐
The Falling Leaves
Infinite penetration test
Ffmpeg (I) AV_ register_ all()
After installing NRM, the internal/validators js:124 throw new ERR_ INVALID_ ARG_ TYPE(name, ‘string‘, value)
Idea related issues
Anniversary party
JS rounding tips
Oracle view all tablespaces in the current library
Not so Mobile
Force buckle 1024 video splicing
Force buckle 1884 Egg drop - two eggs
Why MySQL cannot insert Chinese data in CMD
webrtc优势与模块拆分
B_ QuRT_ User_ Guide(27)
B_QuRT_User_Guide(27)
B_ QuRT_ User_ Guide(26)
PLSQL installation under Windows
Oracle view tablespace usage
Loss损失函数
FatMouse and Cheese