题目地址:
https://www.luogu.com.cn/problem/CF103B
题面翻译:
很久很久以前的一天,一位美男子来到海边,海上狂风大作。美男子希望在海中找到美人鱼,但是很不幸他只找到了章鱼怪。
然而,在世界的另一端,人们正在积极的收集怪物的行为信息,以便研制出强大的武器来对付章鱼怪。由于地震的多发,以及恶劣的天气,使得我们的卫星不能很好的定位怪物,从而不能很好的命中目标。第一次射击的分析结果会反映在一张由 n n n个点和 m m m条边组成的无向图上。现在让我们来确定这张图是不是可以被认为是章鱼怪。
为了简单起见,我们假设章鱼怪的形状是这样,他有一个球形的身体,然后有很多触须连接在他的身上。可以表现为一张无向图,在图中可以被认为由三棵或者更多的树(代表触须)组成,这些树的根在图中处在一个环中(这个环代表球形身体)。
题目描述:
…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 n n vertices and m m 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
n
n and the number of edges
m
m
m of the graph (
1
≤
n
≤
100
1\le n\le 100
1≤n≤100 ,
0
≤
m
≤
n
(
n
−
1
)
2
0\le m\le \frac{n(n-1)}{2}
0≤m≤2n(n−1)).
Each of the following m m m lines contains a pair of integers x x x and y y y , that show that an edge exists between vertices x x x and y y y ( 1 ≤ x , y ≤ n , x ≠ y 1\le x,y\le n,x≠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.
提示:
Let us denote as a simple cycle a set of
v
v
v vertices that can be numbered so that the edges will only exist between vertices number
1
1
1 and
2
2
2 ,
2
2
2 and
3
3
3 , …,
v
−
1
v-1
v−1 and
v
v
v ,
v
v
v and
1
1
1 .
A tree is a connected undirected graph consisting of n n n vertices and n − 1 n-1 n−1 edges ( n > 0 n>0 n>0 ).
A rooted tree is a tree where one vertex is selected to be the root.
其实就是问 n n n个顶点 m m m条边的无向图是否只有一个环。如果某个无向图只有一个环,那么我们将这个环上的某条边去掉,整个图依然连通,但是没有环,说明是一棵树,说明只有一个环的图事实上是一棵树加上一条边,所以必须 n = m n=m n=m并且连通,可以用并查集。代码如下:
#include <iostream>
using namespace std;
const int N = 110;
int n, m;
int p[N];
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main() {
scanf("%d%d", &n, &m);
if (n != m) puts("NO");
else {
for (int i = 1; i <= n; i++) p[i] = i;
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
p[find(a)] = find(b);
}
bool flag = true;
for (int i = 2; i <= n; i++)
if (find(i) != find(i - 1)) {
flag = false;
puts("NO");
break;
}
if (flag) puts("FHTAGN!");
}
}
时间复杂度 O ( n log ∗ n + m ) O(n\log^*n+m) O(nlog∗n+m),空间 O ( n ) O(n) O(n)。