Whether there are multiple trees in the question, and their roots form a ring. Simply put, it is to find whether there is only one ring in an undirected graph and the points are connected.
Method: The number of edges = points is the property of only one ring in the tree. If this condition is met, it cannot be determined because multiple ring fields may meet this condition. Therefore, DFS judges the connectivity.
#include <map>#include <set>#include <list>#include <queue>#include <deque>#include <stack>#include <string>#include <time.h>#include <cstdio>#include <math.h>#include <iomanip>#include <cstdlib>#include <limits.h>#include <string.h>#include <iostream>#include <fstream>#include <algorithm>using namespace std;#define LL long long#define MIN INT_MIN#define MAX INT_MAX#define PI acos(-1.0)#define FRE freopen ("input.txt","r",stdin)#define FF freopen ("output.txt","w",stdout)#define eps 1e-8#define N 105int g[N][N];bool vis[N];int cnt;int n;void dfs(int x) { int i,j; vis[x] = 1; cnt++; for (i = 1; i <= n; i++) { if (!vis[i] && g[x][i]) { dfs(i); } }}int main(){ int m; while(scanf("%d%d",&n,&m) != -1) { int i,j,k; int mm = m; memset(vis,0,sizeof(vis)); while (m--) { int a,b; scanf("%d%d",&a,&b); g[a][b] = g[b][a] = 1; } if (n == mm) { cnt = 0; dfs(1); if(cnt == n) puts("FHTAGN!"); else puts("NO"); } else puts("NO"); } return 0;}