output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int f[103];
int find(int x) {
if (x == f[x]) return x;
return f[x] = find(f[x]);
}
void up(int x, int y) {
int fx = find(x);
int fy = find(y);
f[fx] = fy;
}
int main() {
while (~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; i++) f[i] = i;
int x, y;
for (int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
if (find(x) != find(y)) up(x, y);
}
int flag = 0;
if (n != m) flag = 1;
for (int i = 2; i <= n; i++)
if (find(1) != find(i)) {
flag = 1;
break;
}
if (flag)
printf("NO\n");
else
printf("FHTAGN!\n");
}
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
int f[103];
int find(int x) {
if (x == f[x]) return x;
return f[x] = find(f[x]);
}
void up(int x, int y) {
int fx = find(x);
int fy = find(y);
f[fx] = fy;
}
int main() {
while (~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; i++) f[i] = i;
int x, y;
for (int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
if (find(x) != find(y)) up(x, y);
}
int flag = 0;
if (n != m) flag = 1;
for (int i = 2; i <= n; i++)
if (find(1) != find(i)) {
flag = 1;
break;
}
if (flag)
printf("NO\n");
else
printf("FHTAGN!\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const long double pie = 3.1415926535897932384626;
const long long mod = 1e9 + 7;
void bad() {
cout << "NO";
cout << "\n";
;
exit(0);
}
const int N = 101;
vector<int> g[N];
int n, m;
void read() {
cin >> n >> m;
if (n != m) bad();
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
}
bool vis[N];
void dfs(int node) {
vis[node] = 1;
for (auto &i : g[node]) {
if (vis[i]) continue;
;
dfs(i);
}
}
void solve(int test_case) {
read();
dfs(1);
for (int i = 1; i <= n; i++) {
if (vis[i] == false) {
bad();
}
}
cout << "FHTAGN!";
cout << "\n";
;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
for (int i = 1; i <= t; i++) {
solve(i);
}
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const long double pie = 3.1415926535897932384626;
const long long mod = 1e9 + 7;
void bad() {
cout << "NO";
cout << "\n";
;
exit(0);
}
const int N = 101;
vector<int> g[N];
int n, m;
void read() {
cin >> n >> m;
if (n != m) bad();
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
}
bool vis[N];
void dfs(int node) {
vis[node] = 1;
for (auto &i : g[node]) {
if (vis[i]) continue;
;
dfs(i);
}
}
void solve(int test_case) {
read();
dfs(1);
for (int i = 1; i <= n; i++) {
if (vis[i] == false) {
bad();
}
}
cout << "FHTAGN!";
cout << "\n";
;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
for (int i = 1; i <= t; i++) {
solve(i);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300010;
int n, m, fa[105];
int g[105][105], deg[105];
queue<int> q;
int fnd(int x) { return fa[x] == x ? x : fa[x] = fnd(fa[x]); }
int main() {
scanf("%d%d", &n, &m);
if (m != n) {
printf("NO");
return 0;
}
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[u][v] = g[v][u] = 1;
deg[u]++;
deg[v]++;
fa[fnd(u)] = fnd(v);
}
for (int i = 2; i <= n; i++)
if (fnd(i) != fnd(1)) {
printf("NO");
return 0;
}
for (int i = 1; i <= n; i++)
if (deg[i] == 1) q.push(i);
while (q.size()) {
int x = q.front();
q.pop();
for (int y = 1; y <= n; y++)
if (g[x][y]) {
if (--deg[y] == 1) q.push(y);
}
}
int cnt = 0;
for (int i = 1; i <= n; i++)
if (deg[i] > 1) cnt++;
if (cnt < 3)
printf("NO");
else
printf("FHTAGN!");
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300010;
int n, m, fa[105];
int g[105][105], deg[105];
queue<int> q;
int fnd(int x) { return fa[x] == x ? x : fa[x] = fnd(fa[x]); }
int main() {
scanf("%d%d", &n, &m);
if (m != n) {
printf("NO");
return 0;
}
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[u][v] = g[v][u] = 1;
deg[u]++;
deg[v]++;
fa[fnd(u)] = fnd(v);
}
for (int i = 2; i <= n; i++)
if (fnd(i) != fnd(1)) {
printf("NO");
return 0;
}
for (int i = 1; i <= n; i++)
if (deg[i] == 1) q.push(i);
while (q.size()) {
int x = q.front();
q.pop();
for (int y = 1; y <= n; y++)
if (g[x][y]) {
if (--deg[y] == 1) q.push(y);
}
}
int cnt = 0;
for (int i = 1; i <= n; i++)
if (deg[i] > 1) cnt++;
if (cnt < 3)
printf("NO");
else
printf("FHTAGN!");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[101];
bool vis[101];
void dfs(int u) {
vis[u] = true;
for (auto v : adj[u]) {
if (!vis[v]) dfs(v);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
int M = m;
while (M--) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (n != m)
cout << "NO";
else {
dfs(1);
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
}
}
| ### Prompt
Create a solution in CPP for the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[101];
bool vis[101];
void dfs(int u) {
vis[u] = true;
for (auto v : adj[u]) {
if (!vis[v]) dfs(v);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
int M = m;
while (M--) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (n != m)
cout << "NO";
else {
dfs(1);
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.1415926535897923846;
const long long int MOD = 1000000007;
const long long int N = 998244353;
long long int power(long long int x, long long int n) {
long long int res = 1;
while (n > 0) {
if (n & 1) res = res * x % MOD;
x = x * x % MOD;
n >>= 1;
}
return res;
}
long long int modinverse(long long int a) { return power(a, MOD - 2); }
vector<vector<long long int> > adj;
vector<long long int> vis, par;
bool hm = false;
void dfs(long long int node, long long int parent) {
vis[node] = 1;
for (auto x : adj[node]) {
if (x == parent) continue;
if (vis[x] == 0) {
dfs(x, node);
}
}
}
void solve() {
long long int n, m;
cin >> n >> m;
vis.resize(n, 0);
par.resize(n, -1);
adj.resize(n);
for (long long int i = 0; i < m; i++) {
long long int x, y;
cin >> x >> y;
--x, --y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(0, -1);
for (long long int i = 0; i < n; i++) {
if (vis[i] == 0) {
cout << "NO\n";
return;
}
}
if (m == n)
cout << "FHTAGN!\n";
else
cout << "NO\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int test = 1;
while (test--) {
solve();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
}
| ### Prompt
In CPP, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.1415926535897923846;
const long long int MOD = 1000000007;
const long long int N = 998244353;
long long int power(long long int x, long long int n) {
long long int res = 1;
while (n > 0) {
if (n & 1) res = res * x % MOD;
x = x * x % MOD;
n >>= 1;
}
return res;
}
long long int modinverse(long long int a) { return power(a, MOD - 2); }
vector<vector<long long int> > adj;
vector<long long int> vis, par;
bool hm = false;
void dfs(long long int node, long long int parent) {
vis[node] = 1;
for (auto x : adj[node]) {
if (x == parent) continue;
if (vis[x] == 0) {
dfs(x, node);
}
}
}
void solve() {
long long int n, m;
cin >> n >> m;
vis.resize(n, 0);
par.resize(n, -1);
adj.resize(n);
for (long long int i = 0; i < m; i++) {
long long int x, y;
cin >> x >> y;
--x, --y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(0, -1);
for (long long int i = 0; i < n; i++) {
if (vis[i] == 0) {
cout << "NO\n";
return;
}
}
if (m == n)
cout << "FHTAGN!\n";
else
cout << "NO\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int test = 1;
while (test--) {
solve();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
}
``` |
#include <bits/stdc++.h>
const int maxn = 105;
int n, m, fa[maxn], cnt;
bool flag;
inline int find(int x) {
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
int fx = find(u), fy = find(v);
if (fx != fy) {
fa[fy] = fx;
cnt++;
} else {
if (flag) {
printf("NO\n");
return 0;
}
flag = 1;
cnt++;
}
}
if (flag && cnt == n) {
printf("FHTAGN!");
} else
printf("NO\n");
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
const int maxn = 105;
int n, m, fa[maxn], cnt;
bool flag;
inline int find(int x) {
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
int fx = find(u), fy = find(v);
if (fx != fy) {
fa[fy] = fx;
cnt++;
} else {
if (flag) {
printf("NO\n");
return 0;
}
flag = 1;
cnt++;
}
}
if (flag && cnt == n) {
printf("FHTAGN!");
} else
printf("NO\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 500000;
vector<long long int> g[N];
long long int color[N];
void dfs(long long int k, long long int par, long long int &cyclenumber) {
if (color[k] == 2)
return;
else if (color[k] == 1) {
cyclenumber++;
return;
}
color[k] = 1;
for (auto i : g[k]) {
if (i != par) dfs(i, k, cyclenumber);
}
color[k] = 2;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n, m;
cin >> n >> m;
long long int i;
for (i = 0; i < m; i++) {
long long int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
long long int cyclenumber = 0;
for (long long int i = 0; i < n; i++) color[i] = 0;
long long int connected = 0;
for (i = 1; i <= n; i++)
if (color[i] == 0) {
connected++;
dfs(i, 0, cyclenumber);
}
if (cyclenumber == 1 && connected == 1)
cout << "FHTAGN!";
else
cout << "NO";
}
| ### Prompt
Please create a solution in CPP to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int N = 500000;
vector<long long int> g[N];
long long int color[N];
void dfs(long long int k, long long int par, long long int &cyclenumber) {
if (color[k] == 2)
return;
else if (color[k] == 1) {
cyclenumber++;
return;
}
color[k] = 1;
for (auto i : g[k]) {
if (i != par) dfs(i, k, cyclenumber);
}
color[k] = 2;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n, m;
cin >> n >> m;
long long int i;
for (i = 0; i < m; i++) {
long long int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
long long int cyclenumber = 0;
for (long long int i = 0; i < n; i++) color[i] = 0;
long long int connected = 0;
for (i = 1; i <= n; i++)
if (color[i] == 0) {
connected++;
dfs(i, 0, cyclenumber);
}
if (cyclenumber == 1 && connected == 1)
cout << "FHTAGN!";
else
cout << "NO";
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
bool vis[101];
vector<int> edges[101];
void dfs(int u) {
vis[u] = 1;
for (int i = 0; i < edges[u].size(); ++i)
if (!vis[edges[u][i]]) dfs(edges[u][i]);
}
bool spojny() {
dfs(1);
for (int i = 1; i <= n; ++i)
if (!vis[i]) return 0;
return 1;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
scanf("%d%d", &a, &b);
edges[a].push_back(b);
edges[b].push_back(a);
}
if (n > 2 && spojny() && n == m)
printf("FHTAGN!\n");
else
printf("NO\n");
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
bool vis[101];
vector<int> edges[101];
void dfs(int u) {
vis[u] = 1;
for (int i = 0; i < edges[u].size(); ++i)
if (!vis[edges[u][i]]) dfs(edges[u][i]);
}
bool spojny() {
dfs(1);
for (int i = 1; i <= n; ++i)
if (!vis[i]) return 0;
return 1;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
scanf("%d%d", &a, &b);
edges[a].push_back(b);
edges[b].push_back(a);
}
if (n > 2 && spojny() && n == m)
printf("FHTAGN!\n");
else
printf("NO\n");
}
``` |
#include <bits/stdc++.h>
int fa[1010];
int find(int x) {
if (x != fa[x]) fa[x] = find(fa[x]);
return fa[x];
}
int main() {
int n, m;
while (~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; i++) fa[i] = i;
int flag, a, b, num, flag2;
flag = num = flag2 = 0;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
if (flag2) continue;
int px = find(a), py = find(b);
if (px != py) {
num++;
fa[px] = py;
} else {
if (!flag) {
num++;
flag = 1;
} else
flag2 = 1;
}
}
if (!flag2 && flag && num == n)
printf("FHTAGN!\n");
else
printf("NO\n");
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
int fa[1010];
int find(int x) {
if (x != fa[x]) fa[x] = find(fa[x]);
return fa[x];
}
int main() {
int n, m;
while (~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; i++) fa[i] = i;
int flag, a, b, num, flag2;
flag = num = flag2 = 0;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
if (flag2) continue;
int px = find(a), py = find(b);
if (px != py) {
num++;
fa[px] = py;
} else {
if (!flag) {
num++;
flag = 1;
} else
flag2 = 1;
}
}
if (!flag2 && flag && num == n)
printf("FHTAGN!\n");
else
printf("NO\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[100][100];
int col[100];
int cycles;
void dfs(int p, int u) {
col[u] = 1;
for (int i = 0; i < n; i++)
if (a[u][i] && i != p) {
if (col[i] == 1) cycles++;
if (col[i] == 0) dfs(u, i);
}
col[u] = 2;
}
int main() {
cin >> n >> m;
int x, y;
for (int i = 0; i < m; i++) {
cin >> x >> y;
x--;
y--;
a[x][y] = 1;
a[y][x] = 1;
}
dfs(-1, 0);
for (int i = 0; i < n; i++)
if (col[i] == 0) cycles = 0;
if (cycles == 1)
cout << "FHTAGN!";
else
cout << "NO";
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, a[100][100];
int col[100];
int cycles;
void dfs(int p, int u) {
col[u] = 1;
for (int i = 0; i < n; i++)
if (a[u][i] && i != p) {
if (col[i] == 1) cycles++;
if (col[i] == 0) dfs(u, i);
}
col[u] = 2;
}
int main() {
cin >> n >> m;
int x, y;
for (int i = 0; i < m; i++) {
cin >> x >> y;
x--;
y--;
a[x][y] = 1;
a[y][x] = 1;
}
dfs(-1, 0);
for (int i = 0; i < n; i++)
if (col[i] == 0) cycles = 0;
if (cycles == 1)
cout << "FHTAGN!";
else
cout << "NO";
}
``` |
#include <bits/stdc++.h>
using namespace std;
int m, n, s;
bool vis[102];
vector<int> q[102];
void dfs(int a) {
vis[a] = true;
s++;
for (int i = 0; i < q[a].size(); i++)
if (vis[q[a][i]] == false) dfs(q[a][i]);
}
int main() {
cin >> n >> m;
int r1, r2;
for (int i = 0; i < m; i++) {
cin >> r1 >> r2;
q[r1].push_back(r2);
q[r2].push_back(r1);
}
if (n != m) {
cout << "NO" << endl;
return 0;
}
dfs(1);
if (s == n)
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int m, n, s;
bool vis[102];
vector<int> q[102];
void dfs(int a) {
vis[a] = true;
s++;
for (int i = 0; i < q[a].size(); i++)
if (vis[q[a][i]] == false) dfs(q[a][i]);
}
int main() {
cin >> n >> m;
int r1, r2;
for (int i = 0; i < m; i++) {
cin >> r1 >> r2;
q[r1].push_back(r2);
q[r2].push_back(r1);
}
if (n != m) {
cout << "NO" << endl;
return 0;
}
dfs(1);
if (s == n)
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
vector<int> order, m, cc;
int cycle = 0, cnt = 0;
int cycle_check(int here) {
int ret = order[here];
m.push_back(here);
for (int there : adj[here]) {
if (order[there] == -1) {
order[there] = order[here] + 1;
ret = min(ret, cycle_check(there));
} else if (order[there] < order[here] - 1) {
cycle++;
ret = min(ret, order[there]);
}
}
if (ret == order[here]) {
cc.push_back(here);
while (m.back() != here) {
cc.push_back(m.back());
m.pop_back();
}
m.pop_back();
if (cc.size() == 1) {
cc.clear();
}
}
return ret;
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
adj = vector<vector<int> >(n + 1);
order = vector<int>(n + 1, -1);
while (m--) {
int u, v;
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
int n_component = 0;
for (int i = 1; i <= n; i++) {
if (order[i] == -1) {
n_component++;
order[i] = 0;
cycle_check(1);
}
}
if (cycle == 1 && cc.size() >= 3 && n_component == 1) {
printf("FHTAGN!\n");
} else {
printf("NO\n");
}
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
vector<int> order, m, cc;
int cycle = 0, cnt = 0;
int cycle_check(int here) {
int ret = order[here];
m.push_back(here);
for (int there : adj[here]) {
if (order[there] == -1) {
order[there] = order[here] + 1;
ret = min(ret, cycle_check(there));
} else if (order[there] < order[here] - 1) {
cycle++;
ret = min(ret, order[there]);
}
}
if (ret == order[here]) {
cc.push_back(here);
while (m.back() != here) {
cc.push_back(m.back());
m.pop_back();
}
m.pop_back();
if (cc.size() == 1) {
cc.clear();
}
}
return ret;
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
adj = vector<vector<int> >(n + 1);
order = vector<int>(n + 1, -1);
while (m--) {
int u, v;
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
int n_component = 0;
for (int i = 1; i <= n; i++) {
if (order[i] == -1) {
n_component++;
order[i] = 0;
cycle_check(1);
}
}
if (cycle == 1 && cc.size() >= 3 && n_component == 1) {
printf("FHTAGN!\n");
} else {
printf("NO\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 444;
vector<int> graph[MAX_N];
bool was[MAX_N];
void dfs(int u) {
was[u] = true;
for (int i = 0; i < graph[u].size(); i++) {
int to = graph[u][i];
if (!was[to]) dfs(to);
}
}
int main() {
ios_base::sync_with_stdio(false);
int n, m;
int u, v;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (!was[i]) {
cout << "NO";
return 0;
}
}
if (n != m)
cout << "NO";
else
cout << "FHTAGN!";
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 444;
vector<int> graph[MAX_N];
bool was[MAX_N];
void dfs(int u) {
was[u] = true;
for (int i = 0; i < graph[u].size(); i++) {
int to = graph[u][i];
if (!was[to]) dfs(to);
}
}
int main() {
ios_base::sync_with_stdio(false);
int n, m;
int u, v;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (!was[i]) {
cout << "NO";
return 0;
}
}
if (n != m)
cout << "NO";
else
cout << "FHTAGN!";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long parent[10001];
long long cnt = 0, cnt1 = 0;
void make_set(int v) { parent[v] = v; }
int find_set(int v) {
if (v == parent[v]) return v;
return find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a == b) cnt++;
if (a != b) parent[b] = a;
}
int main() {
int n, m;
cin >> n >> m;
for (int k = 1; k <= n; k++) make_set(k);
for (int k = 1; k <= m; k++) {
int l, t;
cin >> l >> t;
union_sets(l, t);
}
for (int k = 1; k <= n; k++)
if (parent[k] == k) cnt1++;
if (cnt == 1 && cnt1 == 1)
cout << "FHTAGN!";
else
cout << "NO";
}
| ### Prompt
Generate a Cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long parent[10001];
long long cnt = 0, cnt1 = 0;
void make_set(int v) { parent[v] = v; }
int find_set(int v) {
if (v == parent[v]) return v;
return find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a == b) cnt++;
if (a != b) parent[b] = a;
}
int main() {
int n, m;
cin >> n >> m;
for (int k = 1; k <= n; k++) make_set(k);
for (int k = 1; k <= m; k++) {
int l, t;
cin >> l >> t;
union_sets(l, t);
}
for (int k = 1; k <= n; k++)
if (parent[k] == k) cnt1++;
if (cnt == 1 && cnt1 == 1)
cout << "FHTAGN!";
else
cout << "NO";
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool vis[105];
vector<int> g[105];
int dfs(int u) {
vis[u] = true;
for (int i = 0; i < g[u].size(); i++) {
int to = g[u][i];
if (!vis[to]) dfs(to);
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
if (n < 3)
cout << "NO" << endl;
else {
if (n != m)
cout << "NO" << endl;
else {
bool valid = true;
dfs(0);
for (int i = 0; i < n; i++)
if (!vis[i]) valid = false;
if (!valid)
cout << "NO" << endl;
else
cout << "FHTAGN!" << endl;
}
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool vis[105];
vector<int> g[105];
int dfs(int u) {
vis[u] = true;
for (int i = 0; i < g[u].size(); i++) {
int to = g[u][i];
if (!vis[to]) dfs(to);
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
if (n < 3)
cout << "NO" << endl;
else {
if (n != m)
cout << "NO" << endl;
else {
bool valid = true;
dfs(0);
for (int i = 0; i < n; i++)
if (!vis[i]) valid = false;
if (!valid)
cout << "NO" << endl;
else
cout << "FHTAGN!" << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adj(105);
vector<int> vis(105, 0);
int vert;
void dfs(int src) {
vis[src] = 1;
vert++;
for (auto i : adj[src]) {
if (!vis[i]) dfs(i);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
long long int x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1);
if (vert == n && n == m)
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
}
| ### Prompt
Generate a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adj(105);
vector<int> vis(105, 0);
int vert;
void dfs(int src) {
vis[src] = 1;
vert++;
for (auto i : adj[src]) {
if (!vis[i]) dfs(i);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
long long int x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1);
if (vert == n && n == m)
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m;
void dfs(vector<int> tab[], int a, bool vis[]) {
if (!vis[a]) vis[a] = true;
for (int i = 0; i < tab[a].size(); ++i) {
if (!vis[tab[a][i]]) {
dfs(tab, tab[a][i], vis);
}
}
}
int main() {
int x, y;
cin >> n >> m;
vector<int> tab[n];
bool vis[n];
for (int i = 0; i < n; ++i) {
vis[i] = false;
}
int c = m;
while (c--) {
cin >> x >> y;
x--;
y--;
tab[x].push_back(y);
tab[y].push_back(x);
}
dfs(tab, 0, vis);
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
cout << "NO" << endl;
return 0;
}
}
if (n == m)
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
void dfs(vector<int> tab[], int a, bool vis[]) {
if (!vis[a]) vis[a] = true;
for (int i = 0; i < tab[a].size(); ++i) {
if (!vis[tab[a][i]]) {
dfs(tab, tab[a][i], vis);
}
}
}
int main() {
int x, y;
cin >> n >> m;
vector<int> tab[n];
bool vis[n];
for (int i = 0; i < n; ++i) {
vis[i] = false;
}
int c = m;
while (c--) {
cin >> x >> y;
x--;
y--;
tab[x].push_back(y);
tab[y].push_back(x);
}
dfs(tab, 0, vis);
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
cout << "NO" << endl;
return 0;
}
}
if (n == m)
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class U, class V>
ostream& operator<<(ostream& os, const pair<U, V>& p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <class U, class V>
istream& operator>>(istream& in, pair<U, V>& p) {
in >> p.first >> p.second;
return in;
}
long long n, f, s;
vector<vector<long long>> al;
void dfsvis(long long u, bool* mark) {
mark[u] = 1;
for (long long v : al[u]) {
if (!mark[v]) {
dfsvis(v, mark);
}
}
}
bool iscon() {
bool mark[n];
memset(mark, 0, sizeof(mark));
;
dfsvis(0, mark);
for (long long i = 0; i < n; i++) {
if (!mark[i]) {
return 0;
}
}
return 1;
}
int main() {
ios::sync_with_stdio(false);
long long m;
cin >> n >> m;
al = vector<vector<long long>>(n);
for (long long i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
al[u - 1].push_back(v - 1), al[v - 1].push_back(u - 1);
}
if (!iscon()) {
cout << "NO";
return 0;
}
if (m - (n - 1) == 1) {
cout << "FHTAGN!";
} else {
cout << "NO";
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class U, class V>
ostream& operator<<(ostream& os, const pair<U, V>& p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <class U, class V>
istream& operator>>(istream& in, pair<U, V>& p) {
in >> p.first >> p.second;
return in;
}
long long n, f, s;
vector<vector<long long>> al;
void dfsvis(long long u, bool* mark) {
mark[u] = 1;
for (long long v : al[u]) {
if (!mark[v]) {
dfsvis(v, mark);
}
}
}
bool iscon() {
bool mark[n];
memset(mark, 0, sizeof(mark));
;
dfsvis(0, mark);
for (long long i = 0; i < n; i++) {
if (!mark[i]) {
return 0;
}
}
return 1;
}
int main() {
ios::sync_with_stdio(false);
long long m;
cin >> n >> m;
al = vector<vector<long long>>(n);
for (long long i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
al[u - 1].push_back(v - 1), al[v - 1].push_back(u - 1);
}
if (!iscon()) {
cout << "NO";
return 0;
}
if (m - (n - 1) == 1) {
cout << "FHTAGN!";
} else {
cout << "NO";
}
return 0;
}
``` |
#include <bits/stdc++.h>
const int magicconst = 73743071;
using namespace std;
bool b[105];
bool c[105][105];
int n, m;
void dfs(int v) {
if (b[v]) return;
b[v] = 1;
for (int i = 0; i < (int)(n); ++i)
if (c[v][i]) dfs(i);
}
int main() {
scanf("%d%d", &n, &m);
if (n != m) {
printf("NO");
return 0;
}
memset(c, 0, sizeof c);
for (int i = 0; i < (int)(m); ++i) {
int q, w;
scanf("%d%d", &q, &w);
c[q - 1][w - 1] = c[w - 1][q - 1] = 1;
}
memset(b, 0, sizeof b);
dfs(0);
for (int i = 0; i < (int)(n); ++i) {
if (!b[i]) {
printf("NO");
return 0;
}
}
printf("FHTAGN!");
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
const int magicconst = 73743071;
using namespace std;
bool b[105];
bool c[105][105];
int n, m;
void dfs(int v) {
if (b[v]) return;
b[v] = 1;
for (int i = 0; i < (int)(n); ++i)
if (c[v][i]) dfs(i);
}
int main() {
scanf("%d%d", &n, &m);
if (n != m) {
printf("NO");
return 0;
}
memset(c, 0, sizeof c);
for (int i = 0; i < (int)(m); ++i) {
int q, w;
scanf("%d%d", &q, &w);
c[q - 1][w - 1] = c[w - 1][q - 1] = 1;
}
memset(b, 0, sizeof b);
dfs(0);
for (int i = 0; i < (int)(n); ++i) {
if (!b[i]) {
printf("NO");
return 0;
}
}
printf("FHTAGN!");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<long long> adj[500];
long long vis[500];
long long ans;
void dfs(long long fr, long long m) {
vis[fr] = 1;
for (auto it : adj[fr]) {
if (it != m) {
if (vis[it])
ans++;
else
dfs(it, fr);
}
}
}
int main() {
long long n, m;
scanf("%lld%lld", &n, &m);
long long i;
long long x, y, w;
long long fr;
for (i = 0; i < m; i++) {
scanf("%lld%lld", &x, &y);
x--;
y--;
adj[x].push_back(y);
adj[y].push_back(x);
if (!i) fr = x;
}
dfs(1, -1);
for (i = 0; i < n; i++) {
if (!vis[i]) {
cout << "NO\n";
return 0;
}
}
if (ans == 2)
cout << "FHTAGN!\n";
else
cout << "NO\n";
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<long long> adj[500];
long long vis[500];
long long ans;
void dfs(long long fr, long long m) {
vis[fr] = 1;
for (auto it : adj[fr]) {
if (it != m) {
if (vis[it])
ans++;
else
dfs(it, fr);
}
}
}
int main() {
long long n, m;
scanf("%lld%lld", &n, &m);
long long i;
long long x, y, w;
long long fr;
for (i = 0; i < m; i++) {
scanf("%lld%lld", &x, &y);
x--;
y--;
adj[x].push_back(y);
adj[y].push_back(x);
if (!i) fr = x;
}
dfs(1, -1);
for (i = 0; i < n; i++) {
if (!vis[i]) {
cout << "NO\n";
return 0;
}
}
if (ans == 2)
cout << "FHTAGN!\n";
else
cout << "NO\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool p[128][128], q[128][128];
int cnt[128], n;
bool mark[128];
void dfs(int u) {
if (mark[u]) return;
mark[u] = true;
for (int v = 1; v <= n; ++v)
if (p[u][v]) dfs(v);
}
bool gao() {
int m, a, b, i, j, t, f;
scanf("%d%d", &n, &m);
while (m--) {
scanf("%d%d", &a, &b);
q[a][b] = q[b][a] = p[a][b] = p[b][a] = true;
++cnt[a];
++cnt[b];
}
bool flag;
dfs(1);
for (i = 1; i <= n; ++i)
if (!mark[i]) return false;
t = n;
do {
flag = false;
for (i = 1; i <= n; ++i)
if (cnt[i] == 1) {
cnt[i] = -1;
--t;
flag = true;
for (j = 1; j <= n; ++j)
if (q[i][j]) {
q[j][i] = q[i][j] = false;
--cnt[j];
}
}
} while (flag);
if (t < 3) return false;
for (i = 1; i <= n; ++i)
if (cnt[i] != -1 && cnt[i] != 2) return false;
return true;
}
int main() {
puts(gao() ? "FHTAGN!" : "NO");
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool p[128][128], q[128][128];
int cnt[128], n;
bool mark[128];
void dfs(int u) {
if (mark[u]) return;
mark[u] = true;
for (int v = 1; v <= n; ++v)
if (p[u][v]) dfs(v);
}
bool gao() {
int m, a, b, i, j, t, f;
scanf("%d%d", &n, &m);
while (m--) {
scanf("%d%d", &a, &b);
q[a][b] = q[b][a] = p[a][b] = p[b][a] = true;
++cnt[a];
++cnt[b];
}
bool flag;
dfs(1);
for (i = 1; i <= n; ++i)
if (!mark[i]) return false;
t = n;
do {
flag = false;
for (i = 1; i <= n; ++i)
if (cnt[i] == 1) {
cnt[i] = -1;
--t;
flag = true;
for (j = 1; j <= n; ++j)
if (q[i][j]) {
q[j][i] = q[i][j] = false;
--cnt[j];
}
}
} while (flag);
if (t < 3) return false;
for (i = 1; i <= n; ++i)
if (cnt[i] != -1 && cnt[i] != 2) return false;
return true;
}
int main() {
puts(gao() ? "FHTAGN!" : "NO");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int vis[int(1e5 + 10)];
void bfs(vector<vector<int> > Adj, int s) {
vis[s] = 1;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (auto v : Adj[u]) {
if (vis[v] == 0) {
vis[v] = 1;
Q.push(v);
}
}
}
}
int main() {
vector<vector<int> > Adj;
int n, m;
cin >> n >> m;
Adj.resize(n);
memset(vis, 0, sizeof vis);
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
--x;
--y;
Adj[x].push_back(y);
Adj[y].push_back(x);
}
int cnx = 0;
for (int i = 0; i < n; ++i) {
if (vis[i] == 0) {
bfs(Adj, i);
++cnx;
}
}
if (cnx == 1) {
if (n == m)
cout << "FHTAGN!\n";
else
cout << "NO\n";
} else
cout << "NO\n";
}
| ### Prompt
Develop a solution in cpp to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int vis[int(1e5 + 10)];
void bfs(vector<vector<int> > Adj, int s) {
vis[s] = 1;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (auto v : Adj[u]) {
if (vis[v] == 0) {
vis[v] = 1;
Q.push(v);
}
}
}
}
int main() {
vector<vector<int> > Adj;
int n, m;
cin >> n >> m;
Adj.resize(n);
memset(vis, 0, sizeof vis);
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
--x;
--y;
Adj[x].push_back(y);
Adj[y].push_back(x);
}
int cnx = 0;
for (int i = 0; i < n; ++i) {
if (vis[i] == 0) {
bfs(Adj, i);
++cnx;
}
}
if (cnx == 1) {
if (n == m)
cout << "FHTAGN!\n";
else
cout << "NO\n";
} else
cout << "NO\n";
}
``` |
#include <bits/stdc++.h>
const int MAXN = 110;
int n, m;
int f[MAXN];
int getf(int p) { return f[p] == p ? p : f[p] = getf(f[p]); }
int main() {
scanf("%d %d\n", &n, &m);
int i, s, t;
if (n == 1 || n != m) {
printf("NO\n");
return 0;
}
for (int i = 1; i <= n; ++i) f[i] = i;
int c = 0;
for (int i = 1; i <= m; ++i) {
scanf("%d %d", &s, &t);
if (getf(s) != getf(t)) {
f[getf(s)] = getf(t);
} else {
if (c) {
printf("NO\n");
return 0;
}
++c;
}
}
printf("FHTAGN!\n");
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
const int MAXN = 110;
int n, m;
int f[MAXN];
int getf(int p) { return f[p] == p ? p : f[p] = getf(f[p]); }
int main() {
scanf("%d %d\n", &n, &m);
int i, s, t;
if (n == 1 || n != m) {
printf("NO\n");
return 0;
}
for (int i = 1; i <= n; ++i) f[i] = i;
int c = 0;
for (int i = 1; i <= m; ++i) {
scanf("%d %d", &s, &t);
if (getf(s) != getf(t)) {
f[getf(s)] = getf(t);
} else {
if (c) {
printf("NO\n");
return 0;
}
++c;
}
}
printf("FHTAGN!\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long visited[100010];
vector<long long> g[100010];
void dfs(long long u) {
visited[u] = true;
for (auto x : g[u]) {
if (!visited[x]) {
dfs(x);
}
}
}
void solve() {
long long n, m;
cin >> n >> m;
for (long long i = 0; i < m; ++i) {
long long a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
long long c = 0;
for (long long i = 1; i <= n; i++)
if (!visited[i]) c++, dfs(i);
if (n == m and c == 1)
cout << "FHTAGN!" << '\n';
else
cout << "NO" << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
long long t = 1;
while (t--) solve();
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long visited[100010];
vector<long long> g[100010];
void dfs(long long u) {
visited[u] = true;
for (auto x : g[u]) {
if (!visited[x]) {
dfs(x);
}
}
}
void solve() {
long long n, m;
cin >> n >> m;
for (long long i = 0; i < m; ++i) {
long long a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
long long c = 0;
for (long long i = 1; i <= n; i++)
if (!visited[i]) c++, dfs(i);
if (n == m and c == 1)
cout << "FHTAGN!" << '\n';
else
cout << "NO" << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
long long t = 1;
while (t--) solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool connected[100];
void dfs(const vector<vector<int>>& a, int node) {
connected[node] = true;
for (int child : a[node]) {
if (connected[child] == false) {
dfs(a, child);
}
}
}
int main() {
memset(connected, false, sizeof(connected));
int n, m, a, b;
cin >> n >> m;
vector<vector<int>> adjlist(n);
for (int i = 0; i < m; i++) {
cin >> a >> b;
a--;
b--;
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
dfs(adjlist, 0);
for (int i = 0; i < n; i++) {
if (connected[i] == false) {
cout << "NO";
return 0;
}
}
if (n == m) {
cout << "FHTAGN!";
} else {
cout << "NO";
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool connected[100];
void dfs(const vector<vector<int>>& a, int node) {
connected[node] = true;
for (int child : a[node]) {
if (connected[child] == false) {
dfs(a, child);
}
}
}
int main() {
memset(connected, false, sizeof(connected));
int n, m, a, b;
cin >> n >> m;
vector<vector<int>> adjlist(n);
for (int i = 0; i < m; i++) {
cin >> a >> b;
a--;
b--;
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
dfs(adjlist, 0);
for (int i = 0; i < n; i++) {
if (connected[i] == false) {
cout << "NO";
return 0;
}
}
if (n == m) {
cout << "FHTAGN!";
} else {
cout << "NO";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<long long> arr[101];
long long vis[101];
void dfs(long long node) {
vis[node] = 1;
for (auto x : arr[node]) {
if (vis[x]) continue;
dfs(x);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m;
cin >> n >> m;
long long u, v;
for (long long i = 0; i < m; i++)
cin >> u >> v, arr[u].push_back(v), arr[v].push_back(u);
if (n != m)
cout << "NO";
else {
long long count = 0;
for (long long i = 1; i <= n; i++) {
if (vis[i]) continue;
dfs(i);
count++;
}
if (count > 1)
cout << "NO"
<< "\n";
else {
cout << "FHTAGN!";
}
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<long long> arr[101];
long long vis[101];
void dfs(long long node) {
vis[node] = 1;
for (auto x : arr[node]) {
if (vis[x]) continue;
dfs(x);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m;
cin >> n >> m;
long long u, v;
for (long long i = 0; i < m; i++)
cin >> u >> v, arr[u].push_back(v), arr[v].push_back(u);
if (n != m)
cout << "NO";
else {
long long count = 0;
for (long long i = 1; i <= n; i++) {
if (vis[i]) continue;
dfs(i);
count++;
}
if (count > 1)
cout << "NO"
<< "\n";
else {
cout << "FHTAGN!";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const bool testcase = 0;
const long long int mod1 = 1000000007;
const long long int mod2 = 998244353;
const long long int N = 1e5 + 5;
std::vector<long long int> adj[N];
std::vector<long long int> vis(N, false);
bool hasLoop(long long int i, long long int p) {
vis[i] = true;
bool ans = false;
for (auto it : adj[i]) {
if (!vis[it]) {
ans |= hasLoop(it, i);
} else if (it != p) {
ans |= true;
}
}
return ans;
}
void dfs(long long int i) {
vis[i] = true;
for (auto it : adj[i]) {
if (!vis[it]) {
dfs(it);
}
}
}
void solve() {
long long int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
long long int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
bool ok = true;
dfs(1);
for (int i = 1; i <= n; ++i) {
ok &= (vis[i] == 1);
}
if (ok && n == m) {
cout << "FHTAGN!\n";
} else {
cout << "NO\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int T_T = 1;
if (testcase) {
cin >> T_T;
}
while (T_T--) {
solve();
}
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const bool testcase = 0;
const long long int mod1 = 1000000007;
const long long int mod2 = 998244353;
const long long int N = 1e5 + 5;
std::vector<long long int> adj[N];
std::vector<long long int> vis(N, false);
bool hasLoop(long long int i, long long int p) {
vis[i] = true;
bool ans = false;
for (auto it : adj[i]) {
if (!vis[it]) {
ans |= hasLoop(it, i);
} else if (it != p) {
ans |= true;
}
}
return ans;
}
void dfs(long long int i) {
vis[i] = true;
for (auto it : adj[i]) {
if (!vis[it]) {
dfs(it);
}
}
}
void solve() {
long long int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
long long int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
bool ok = true;
dfs(1);
for (int i = 1; i <= n; ++i) {
ok &= (vis[i] == 1);
}
if (ok && n == m) {
cout << "FHTAGN!\n";
} else {
cout << "NO\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int T_T = 1;
if (testcase) {
cin >> T_T;
}
while (T_T--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, unsigned long long y) {
long long temp;
if (y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
long long modpow(long long x, unsigned int y, long long p) {
long long res = 1;
x = x % p;
if (y == 0) return 1;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long exponentMod(long long A, long long B, long long C) {
if (B == 0) return 1;
if (A == 0) return 0;
long long y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
} else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (long long)((y + C) % C);
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int gcdExtended(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
void modInverse(int a, int m) {
int x, y;
int g = gcdExtended(a, m, &x, &y);
if (g != 1)
cout << "Inverse doesn't exist";
else {
int res = (x % m + m) % m;
cout << "Modular multiplicative inverse is " << res;
}
}
void SieveOfEratosthenes(int n) {
bool sieve[n + 1];
long long cnt = 0;
memset(sieve, 0, sizeof(sieve));
for (int p = 2; p * p <= n; p++) {
if (!sieve[p]) {
for (int i = 2 * p; i <= n; i += p) sieve[i] = p;
}
}
for (int p = 2; p <= n; p++) {
if (sieve[p]) cnt++;
}
cout << cnt;
}
int phi(unsigned int n) {
float result = n;
for (int p = 2; p * p <= n; ++p) {
if (n % p == 0) {
while (n % p == 0) n /= p;
result *= (1.0 - (1.0 / (float)p));
}
}
if (n > 1) result *= (1.0 - (1.0 / (float)n));
return (int)result;
}
long long floorSqrt(long long x) {
if (x == 0 || x == 1) return x;
unsigned long long start = 1, end = x, ans;
while (start <= end) {
unsigned long long mid = start + (end - start) / 2;
if (mid * mid == x) return mid;
if (mid * mid < x) {
start = mid + 1;
ans = mid;
} else
end = mid - 1;
}
return ans;
}
void start() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int main() {
start();
long long n, m;
cin >> n >> m;
vector<long long> adj[n + 1];
for (long long i = 0; i < m; i++) {
long long a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
queue<long long> q;
bool vis[n + 1];
memset(vis, false, sizeof(vis));
long long cnt = 0;
for (long long i = 1; i < n + 1; i++) {
if (!vis[i]) {
q.push(i);
cnt++;
vis[i] = true;
while (!q.empty()) {
long long curr = q.front();
q.pop();
for (auto x : adj[curr]) {
if (!vis[x]) {
vis[x] = true;
q.push(x);
}
}
}
}
}
if (cnt == 1 && n == m) {
cout << "FHTAGN!";
} else
cout << "NO";
}
| ### Prompt
Please create a solution in Cpp to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, unsigned long long y) {
long long temp;
if (y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
long long modpow(long long x, unsigned int y, long long p) {
long long res = 1;
x = x % p;
if (y == 0) return 1;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long exponentMod(long long A, long long B, long long C) {
if (B == 0) return 1;
if (A == 0) return 0;
long long y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
} else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (long long)((y + C) % C);
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int gcdExtended(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
void modInverse(int a, int m) {
int x, y;
int g = gcdExtended(a, m, &x, &y);
if (g != 1)
cout << "Inverse doesn't exist";
else {
int res = (x % m + m) % m;
cout << "Modular multiplicative inverse is " << res;
}
}
void SieveOfEratosthenes(int n) {
bool sieve[n + 1];
long long cnt = 0;
memset(sieve, 0, sizeof(sieve));
for (int p = 2; p * p <= n; p++) {
if (!sieve[p]) {
for (int i = 2 * p; i <= n; i += p) sieve[i] = p;
}
}
for (int p = 2; p <= n; p++) {
if (sieve[p]) cnt++;
}
cout << cnt;
}
int phi(unsigned int n) {
float result = n;
for (int p = 2; p * p <= n; ++p) {
if (n % p == 0) {
while (n % p == 0) n /= p;
result *= (1.0 - (1.0 / (float)p));
}
}
if (n > 1) result *= (1.0 - (1.0 / (float)n));
return (int)result;
}
long long floorSqrt(long long x) {
if (x == 0 || x == 1) return x;
unsigned long long start = 1, end = x, ans;
while (start <= end) {
unsigned long long mid = start + (end - start) / 2;
if (mid * mid == x) return mid;
if (mid * mid < x) {
start = mid + 1;
ans = mid;
} else
end = mid - 1;
}
return ans;
}
void start() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int main() {
start();
long long n, m;
cin >> n >> m;
vector<long long> adj[n + 1];
for (long long i = 0; i < m; i++) {
long long a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
queue<long long> q;
bool vis[n + 1];
memset(vis, false, sizeof(vis));
long long cnt = 0;
for (long long i = 1; i < n + 1; i++) {
if (!vis[i]) {
q.push(i);
cnt++;
vis[i] = true;
while (!q.empty()) {
long long curr = q.front();
q.pop();
for (auto x : adj[curr]) {
if (!vis[x]) {
vis[x] = true;
q.push(x);
}
}
}
}
}
if (cnt == 1 && n == m) {
cout << "FHTAGN!";
} else
cout << "NO";
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool use[105];
vector<int> a[105];
void dfs(int f) {
use[f] = 1;
for (int i = 0; i < a[f].size(); i++)
if (!use[a[f][i]]) dfs(a[f][i]);
}
int main() {
cin >> n >> m;
int i, j, x, y;
if (n != m) {
cout << "NO" << endl;
return 0;
}
for (i = 0; i < n; i++) {
cin >> x >> y;
a[x - 1].push_back(y - 1);
a[y - 1].push_back(x - 1);
}
dfs(0);
for (i = 0; i < n; i++)
if (!use[i]) {
cout << "NO" << endl;
return 0;
}
cout << "FHTAGN!" << endl;
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool use[105];
vector<int> a[105];
void dfs(int f) {
use[f] = 1;
for (int i = 0; i < a[f].size(); i++)
if (!use[a[f][i]]) dfs(a[f][i]);
}
int main() {
cin >> n >> m;
int i, j, x, y;
if (n != m) {
cout << "NO" << endl;
return 0;
}
for (i = 0; i < n; i++) {
cin >> x >> y;
a[x - 1].push_back(y - 1);
a[y - 1].push_back(x - 1);
}
dfs(0);
for (i = 0; i < n; i++)
if (!use[i]) {
cout << "NO" << endl;
return 0;
}
cout << "FHTAGN!" << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
int visited[100010];
int parent[100010];
int flag;
void explore(int u) {
visited[u] = 1;
for (int i = 0; i < g[u].size(); i++) {
if (!visited[g[u][i]]) {
parent[g[u][i]] = u;
explore(g[u][i]);
} else if (g[u][i] == parent[u])
;
else
flag++;
}
}
int n;
int dfs() {
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (visited[i])
continue;
else {
explore(i);
cnt++;
if (cnt > 1) return 0;
}
}
if (flag == 2) return 1;
}
int main() {
int m;
int e = 0;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
e++;
}
if (m != n) {
cout << "NO" << endl;
return 0;
}
int k = dfs();
if (k)
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
int visited[100010];
int parent[100010];
int flag;
void explore(int u) {
visited[u] = 1;
for (int i = 0; i < g[u].size(); i++) {
if (!visited[g[u][i]]) {
parent[g[u][i]] = u;
explore(g[u][i]);
} else if (g[u][i] == parent[u])
;
else
flag++;
}
}
int n;
int dfs() {
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (visited[i])
continue;
else {
explore(i);
cnt++;
if (cnt > 1) return 0;
}
}
if (flag == 2) return 1;
}
int main() {
int m;
int e = 0;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
e++;
}
if (m != n) {
cout << "NO" << endl;
return 0;
}
int k = dfs();
if (k)
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INFint = 2e9;
const long long INF = 1e18;
const long long MOD = 1000000007ll;
int g[100][100];
vector<int> used;
set<int> roots;
int n, m;
void dfs(int v, int p) {
used[v] = 1;
for (int i = 0; i < n; i++) {
if (i == p || !g[v][i]) continue;
if (used[i] || roots.count(i)) {
cout << "NO";
exit(0);
}
dfs(i, v);
}
used[v] = 1;
}
vector<int> anc;
void addCycle(int st, int en) {
int v = en;
while (v != st) {
roots.insert(v);
v = anc[v];
}
roots.insert(st);
}
bool cycle(int v, int p = -1) {
used[v] = 1;
for (int i = 0; i < n; i++) {
if (i == p || !g[v][i]) continue;
if (used[i]) {
addCycle(i, v);
return 1;
}
anc[i] = v;
if (cycle(i, v)) return 1;
}
used[v] = 1;
return 0;
}
vector<int> p;
int get(int v) { return p[v] == v ? v : p[v] = get(p[v]); }
void unite(int a, int b) {
a = get(a);
b = get(b);
if (a == b) return;
if (rand() & 1) swap(a, b);
p[a] = b;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
used.resize(n);
p.resize(n);
for (int i = 0; i < n; i++) p[i] = i;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
unite(a, b);
g[a][b] = g[b][a] = 1;
}
int cnt = 0;
for (int i = 0; i < n; i++)
if (p[i] == i) cnt++;
if (cnt != 1) {
cout << "NO";
return 0;
}
anc.resize(n);
for (int i = 0; i < n; i++) {
if (!used[i]) {
if (cycle(i)) break;
}
}
if (roots.size() < 3) {
cout << "NO";
return 0;
}
used.assign(n, 0);
for (auto v : roots) {
for (int i = 0; i < n; i++) {
if (g[v][i] && !roots.count(i)) {
dfs(i, v);
}
}
}
cout << "FHTAGN!";
fprintf(stderr, "\nTIME = %lf\n", 1.0 * clock() / CLOCKS_PER_SEC);
;
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INFint = 2e9;
const long long INF = 1e18;
const long long MOD = 1000000007ll;
int g[100][100];
vector<int> used;
set<int> roots;
int n, m;
void dfs(int v, int p) {
used[v] = 1;
for (int i = 0; i < n; i++) {
if (i == p || !g[v][i]) continue;
if (used[i] || roots.count(i)) {
cout << "NO";
exit(0);
}
dfs(i, v);
}
used[v] = 1;
}
vector<int> anc;
void addCycle(int st, int en) {
int v = en;
while (v != st) {
roots.insert(v);
v = anc[v];
}
roots.insert(st);
}
bool cycle(int v, int p = -1) {
used[v] = 1;
for (int i = 0; i < n; i++) {
if (i == p || !g[v][i]) continue;
if (used[i]) {
addCycle(i, v);
return 1;
}
anc[i] = v;
if (cycle(i, v)) return 1;
}
used[v] = 1;
return 0;
}
vector<int> p;
int get(int v) { return p[v] == v ? v : p[v] = get(p[v]); }
void unite(int a, int b) {
a = get(a);
b = get(b);
if (a == b) return;
if (rand() & 1) swap(a, b);
p[a] = b;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
used.resize(n);
p.resize(n);
for (int i = 0; i < n; i++) p[i] = i;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
unite(a, b);
g[a][b] = g[b][a] = 1;
}
int cnt = 0;
for (int i = 0; i < n; i++)
if (p[i] == i) cnt++;
if (cnt != 1) {
cout << "NO";
return 0;
}
anc.resize(n);
for (int i = 0; i < n; i++) {
if (!used[i]) {
if (cycle(i)) break;
}
}
if (roots.size() < 3) {
cout << "NO";
return 0;
}
used.assign(n, 0);
for (auto v : roots) {
for (int i = 0; i < n; i++) {
if (g[v][i] && !roots.count(i)) {
dfs(i, v);
}
}
}
cout << "FHTAGN!";
fprintf(stderr, "\nTIME = %lf\n", 1.0 * clock() / CLOCKS_PER_SEC);
;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> g[111];
bool u[111];
int n, m, x, y, z;
void dfs(int i) {
u[i] = true;
z++;
for (int j = 0; j < g[i].size(); j++) {
if (u[g[i][j]] == false) dfs(g[i][j]);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) u[i] = false;
for (int i = 0; i < m; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
if (n != m) {
cout << "NO";
return 0;
}
dfs(1);
if (n != z) {
cout << "NO";
return 0;
}
cout << "FHTAGN!";
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> g[111];
bool u[111];
int n, m, x, y, z;
void dfs(int i) {
u[i] = true;
z++;
for (int j = 0; j < g[i].size(); j++) {
if (u[g[i][j]] == false) dfs(g[i][j]);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) u[i] = false;
for (int i = 0; i < m; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
if (n != m) {
cout << "NO";
return 0;
}
dfs(1);
if (n != z) {
cout << "NO";
return 0;
}
cout << "FHTAGN!";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void build(long long v, long long tl, long long tr, long long st[],
long long lz[], bool f[], long long a[]) {
if (tl == tr) {
st[v] = a[tl];
lz[v] = 0LL;
f[v] = false;
return;
}
build((v << 1), tl, ((tl + tr) >> 1), st, lz, f, a);
build((v << 1) | 1, ((tl + tr) >> 1) + 1, tr, st, lz, f, a);
st[v] = st[(v << 1)] + st[(v << 1) | 1];
lz[v] = 0LL;
f[v] = false;
return;
}
void push(long long v, long long tl, long long tr, long long st[],
long long lz[], bool f[]) {
if (f[v]) {
st[(v << 1)] = lz[(v << 1)] = st[(v << 1) | 1] = lz[(v << 1) | 1] = 0LL;
f[(v << 1)] = f[(v << 1) | 1] = true;
f[v] = false;
}
st[(v << 1)] += lz[v] * (((tl + tr) >> 1) - tl + 1);
lz[(v << 1)] += lz[v];
st[(v << 1) | 1] += lz[v] * (tr - ((tl + tr) >> 1));
lz[(v << 1) | 1] += lz[v];
lz[v] = 0LL;
return;
}
void update(long long v, long long tl, long long tr, long long l, long long r,
long long val, bool set, long long st[], long long lz[], bool f[]) {
if (l > r) {
return;
}
if (l == tl && tr == r) {
if (set) {
st[v] = lz[v] = 0LL;
f[v] = true;
}
st[v] += val * (tr - tl + 1);
lz[v] += val;
return;
}
push(v, tl, tr, st, lz, f);
update((v << 1), tl, ((tl + tr) >> 1), l, min(r, ((tl + tr) >> 1)), val, set,
st, lz, f);
update((v << 1) | 1, ((tl + tr) >> 1) + 1, tr, max(l, ((tl + tr) >> 1) + 1),
r, val, set, st, lz, f);
st[v] = st[(v << 1)] + st[(v << 1) | 1];
return;
}
long long query(long long v, long long tl, long long tr, long long l,
long long r, long long st[], long long lz[], bool f[]) {
if (l > r) {
return 0LL;
}
if (l == tl && tr == r) {
return st[v];
}
push(v, tl, tr, st, lz, f);
return query((v << 1), tl, ((tl + tr) >> 1), l, min(r, ((tl + tr) >> 1)), st,
lz, f) +
query((v << 1) | 1, ((tl + tr) >> 1) + 1, tr,
max(l, ((tl + tr) >> 1) + 1), r, st, lz, f);
}
void build(int v, int tl, int tr, int a[], long long st[]) {
if (tl == tr) {
st[v] = a[tl];
return;
}
int tm = ((tl + tr) / 2);
build(2 * v, tl, tm, a, st);
build(2 * v + 1, tm + 1, tr, a, st);
st[v] = st[v * 2] + st[v * 2 + 1];
return;
}
void update(int v, int tl, int tr, int pos, int val, long long st[]) {
if (tl == tr && tl == pos) {
st[v] = val;
return;
}
if (tl > pos || tr < pos) {
return;
}
int tm = ((tl + tr) / 2);
update(2 * v, tl, tm, pos, val, st);
update(2 * v + 1, tm + 1, tr, pos, val, st);
st[v] = st[v * 2] + st[v * 2 + 1];
return;
}
long long query(int v, int tl, int tr, int l, int r, long long st[]) {
if (tl == l && tr == r) {
return st[v];
}
if (l > r) {
return 0LL;
}
int tm = ((tl + tr) / 2);
return query(v * 2, tl, tm, l, std::min(tm, r), st) +
query(v * 2 + 1, tm + 1, tr, std::max(tm + 1, l), r, st);
}
long long fn(long long x, long long rn[]) {
if (x == rn[x])
return x;
else
return rn[x] = fn(rn[x], rn);
}
bool un(long long x, long long y, long long rn[], long long sz[]) {
x = fn(x, rn);
y = fn(y, rn);
if (x == y) return false;
if (sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y];
rn[y] = x;
return true;
}
long long power(long long k) {
long long temp = 1;
for (long long i = 0; i <= k - 1; i++) {
temp = temp * 2;
}
return temp;
}
bool compare(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return a.second < b.second;
}
pair<int, int> mostFrequent(long long arr[], long long n) {
unordered_map<long long, long long> hash;
for (long long i = 0; i < n; i++) hash[arr[i]]++;
long long max_count = 0, res = -1;
for (auto i : hash) {
if (max_count < i.second) {
res = i.first;
max_count = i.second;
}
}
pair<long long, long long> temp;
temp.first = res;
temp.second = max_count;
return temp;
}
string findSum(string str1, string str2) {
if (str1.length() > str2.length()) swap(str1, str2);
string str = "";
int n1 = str1.length(), n2 = str2.length();
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
int carry = 0;
for (int i = 0; i < n1; i++) {
int sum = ((str1[i] - '0') + (str2[i] - '0') + carry);
str.push_back(sum % 10 + '0');
carry = sum / 10;
}
for (int i = n1; i < n2; i++) {
int sum = ((str2[i] - '0') + carry);
str.push_back(sum % 10 + '0');
carry = sum / 10;
}
if (carry) str.push_back(carry + '0');
reverse(str.begin(), str.end());
return str;
}
long long div(long long n) {
if (n % 2 == 0) return 2;
for (long long i = 3; i * i <= n; i += 2) {
if (n % i == 0) return i;
}
return n;
}
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
bool sec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.first > b.first);
}
bool sec1(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second > b.second);
}
vector<long long> v[4001];
vector<bool> vis(100005, 0);
long long vert;
long long ed;
long long flag = 0;
void dfs(long long t) {
vis[t] = 1;
vert++;
ed = ed + v[t].size();
for (auto u : v[t]) {
if (vis[u] == 0) {
dfs(u);
}
}
}
long long msum(long long a[], long long size) {
long long max_so_far = a[0];
long long curr_max = a[0];
for (long long i = 1; i < size; i++) {
curr_max = max(a[i], curr_max + a[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
for (long long i = 0; i <= m - 1; i++) {
long long x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
long long cnt = 0;
long long flag = 0;
for (long long i = 1; i <= n; i++) {
if (vis[i] == 0) {
vert = 0;
ed = 0;
dfs(i);
if (ed / 2 != vert) {
cout << "NO";
flag = 1;
break;
}
cnt++;
}
}
if (cnt > 1 and flag == 0) {
cout << "NO";
flag = 1;
}
if (flag == 0) {
cout << "FHTAGN!";
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void build(long long v, long long tl, long long tr, long long st[],
long long lz[], bool f[], long long a[]) {
if (tl == tr) {
st[v] = a[tl];
lz[v] = 0LL;
f[v] = false;
return;
}
build((v << 1), tl, ((tl + tr) >> 1), st, lz, f, a);
build((v << 1) | 1, ((tl + tr) >> 1) + 1, tr, st, lz, f, a);
st[v] = st[(v << 1)] + st[(v << 1) | 1];
lz[v] = 0LL;
f[v] = false;
return;
}
void push(long long v, long long tl, long long tr, long long st[],
long long lz[], bool f[]) {
if (f[v]) {
st[(v << 1)] = lz[(v << 1)] = st[(v << 1) | 1] = lz[(v << 1) | 1] = 0LL;
f[(v << 1)] = f[(v << 1) | 1] = true;
f[v] = false;
}
st[(v << 1)] += lz[v] * (((tl + tr) >> 1) - tl + 1);
lz[(v << 1)] += lz[v];
st[(v << 1) | 1] += lz[v] * (tr - ((tl + tr) >> 1));
lz[(v << 1) | 1] += lz[v];
lz[v] = 0LL;
return;
}
void update(long long v, long long tl, long long tr, long long l, long long r,
long long val, bool set, long long st[], long long lz[], bool f[]) {
if (l > r) {
return;
}
if (l == tl && tr == r) {
if (set) {
st[v] = lz[v] = 0LL;
f[v] = true;
}
st[v] += val * (tr - tl + 1);
lz[v] += val;
return;
}
push(v, tl, tr, st, lz, f);
update((v << 1), tl, ((tl + tr) >> 1), l, min(r, ((tl + tr) >> 1)), val, set,
st, lz, f);
update((v << 1) | 1, ((tl + tr) >> 1) + 1, tr, max(l, ((tl + tr) >> 1) + 1),
r, val, set, st, lz, f);
st[v] = st[(v << 1)] + st[(v << 1) | 1];
return;
}
long long query(long long v, long long tl, long long tr, long long l,
long long r, long long st[], long long lz[], bool f[]) {
if (l > r) {
return 0LL;
}
if (l == tl && tr == r) {
return st[v];
}
push(v, tl, tr, st, lz, f);
return query((v << 1), tl, ((tl + tr) >> 1), l, min(r, ((tl + tr) >> 1)), st,
lz, f) +
query((v << 1) | 1, ((tl + tr) >> 1) + 1, tr,
max(l, ((tl + tr) >> 1) + 1), r, st, lz, f);
}
void build(int v, int tl, int tr, int a[], long long st[]) {
if (tl == tr) {
st[v] = a[tl];
return;
}
int tm = ((tl + tr) / 2);
build(2 * v, tl, tm, a, st);
build(2 * v + 1, tm + 1, tr, a, st);
st[v] = st[v * 2] + st[v * 2 + 1];
return;
}
void update(int v, int tl, int tr, int pos, int val, long long st[]) {
if (tl == tr && tl == pos) {
st[v] = val;
return;
}
if (tl > pos || tr < pos) {
return;
}
int tm = ((tl + tr) / 2);
update(2 * v, tl, tm, pos, val, st);
update(2 * v + 1, tm + 1, tr, pos, val, st);
st[v] = st[v * 2] + st[v * 2 + 1];
return;
}
long long query(int v, int tl, int tr, int l, int r, long long st[]) {
if (tl == l && tr == r) {
return st[v];
}
if (l > r) {
return 0LL;
}
int tm = ((tl + tr) / 2);
return query(v * 2, tl, tm, l, std::min(tm, r), st) +
query(v * 2 + 1, tm + 1, tr, std::max(tm + 1, l), r, st);
}
long long fn(long long x, long long rn[]) {
if (x == rn[x])
return x;
else
return rn[x] = fn(rn[x], rn);
}
bool un(long long x, long long y, long long rn[], long long sz[]) {
x = fn(x, rn);
y = fn(y, rn);
if (x == y) return false;
if (sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y];
rn[y] = x;
return true;
}
long long power(long long k) {
long long temp = 1;
for (long long i = 0; i <= k - 1; i++) {
temp = temp * 2;
}
return temp;
}
bool compare(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return a.second < b.second;
}
pair<int, int> mostFrequent(long long arr[], long long n) {
unordered_map<long long, long long> hash;
for (long long i = 0; i < n; i++) hash[arr[i]]++;
long long max_count = 0, res = -1;
for (auto i : hash) {
if (max_count < i.second) {
res = i.first;
max_count = i.second;
}
}
pair<long long, long long> temp;
temp.first = res;
temp.second = max_count;
return temp;
}
string findSum(string str1, string str2) {
if (str1.length() > str2.length()) swap(str1, str2);
string str = "";
int n1 = str1.length(), n2 = str2.length();
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
int carry = 0;
for (int i = 0; i < n1; i++) {
int sum = ((str1[i] - '0') + (str2[i] - '0') + carry);
str.push_back(sum % 10 + '0');
carry = sum / 10;
}
for (int i = n1; i < n2; i++) {
int sum = ((str2[i] - '0') + carry);
str.push_back(sum % 10 + '0');
carry = sum / 10;
}
if (carry) str.push_back(carry + '0');
reverse(str.begin(), str.end());
return str;
}
long long div(long long n) {
if (n % 2 == 0) return 2;
for (long long i = 3; i * i <= n; i += 2) {
if (n % i == 0) return i;
}
return n;
}
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
bool sec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.first > b.first);
}
bool sec1(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second > b.second);
}
vector<long long> v[4001];
vector<bool> vis(100005, 0);
long long vert;
long long ed;
long long flag = 0;
void dfs(long long t) {
vis[t] = 1;
vert++;
ed = ed + v[t].size();
for (auto u : v[t]) {
if (vis[u] == 0) {
dfs(u);
}
}
}
long long msum(long long a[], long long size) {
long long max_so_far = a[0];
long long curr_max = a[0];
for (long long i = 1; i < size; i++) {
curr_max = max(a[i], curr_max + a[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
for (long long i = 0; i <= m - 1; i++) {
long long x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
long long cnt = 0;
long long flag = 0;
for (long long i = 1; i <= n; i++) {
if (vis[i] == 0) {
vert = 0;
ed = 0;
dfs(i);
if (ed / 2 != vert) {
cout << "NO";
flag = 1;
break;
}
cnt++;
}
}
if (cnt > 1 and flag == 0) {
cout << "NO";
flag = 1;
}
if (flag == 0) {
cout << "FHTAGN!";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct edge {
long long u, v;
} e[5005 << 1] = {0};
long long n, m, cnt = 0, tot = 0, v[5005] = {0}, h[5005] = {0};
inline void add(long long u, long long v) {
e[++tot] = (edge){h[u], v}, h[u] = tot;
}
void dfs(long long x) {
v[x] = 1, cnt++;
for (register long long i = h[x]; i; i = e[i].u)
if (!v[e[i].v]) dfs(e[i].v);
}
signed main() {
scanf("%lld%lld", &n, &m);
for (register long long i = 1, x, y; i <= m; i++)
scanf("%lld%lld", &x, &y), add(x, y), add(y, x);
if (n ^ m) {
puts("NO");
return 0;
}
dfs(1);
if (cnt ^ n)
puts("NO");
else
puts("FHTAGN!");
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct edge {
long long u, v;
} e[5005 << 1] = {0};
long long n, m, cnt = 0, tot = 0, v[5005] = {0}, h[5005] = {0};
inline void add(long long u, long long v) {
e[++tot] = (edge){h[u], v}, h[u] = tot;
}
void dfs(long long x) {
v[x] = 1, cnt++;
for (register long long i = h[x]; i; i = e[i].u)
if (!v[e[i].v]) dfs(e[i].v);
}
signed main() {
scanf("%lld%lld", &n, &m);
for (register long long i = 1, x, y; i <= m; i++)
scanf("%lld%lld", &x, &y), add(x, y), add(y, x);
if (n ^ m) {
puts("NO");
return 0;
}
dfs(1);
if (cnt ^ n)
puts("NO");
else
puts("FHTAGN!");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long maxn = 105;
long cht;
vector<long> edges[maxn];
long n, m;
long mark[maxn];
void dfs(long u, long pred) {
mark[u] = 1;
if (edges[u].size() != 0) {
for (long choice = 0; choice < edges[u].size(); choice++) {
long next = edges[u][choice];
if ((mark[next] == 1) && (next != pred)) cht++;
if (mark[next] == 0) dfs(next, u);
}
}
}
int main() {
scanf("%ld%ld", &n, &m);
long q;
for (q = 1; q <= n; q++) edges[q].resize(0);
for (q = 1; q <= m; q++) {
long t1, t2;
scanf("%ld%ld", &t1, &t2);
edges[t1].push_back(t2);
edges[t2].push_back(t1);
}
cht = 0;
for (q = 1; q <= n; q++) mark[q] = 0;
long count = 0;
for (q = 1; q <= n; q++)
if (mark[q] == 0) {
dfs(q, -1);
count++;
}
cht = cht / 2;
if ((cht != 1) || (count > 1))
printf("NO");
else
printf("FHTAGN!");
}
| ### Prompt
Please formulate a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long maxn = 105;
long cht;
vector<long> edges[maxn];
long n, m;
long mark[maxn];
void dfs(long u, long pred) {
mark[u] = 1;
if (edges[u].size() != 0) {
for (long choice = 0; choice < edges[u].size(); choice++) {
long next = edges[u][choice];
if ((mark[next] == 1) && (next != pred)) cht++;
if (mark[next] == 0) dfs(next, u);
}
}
}
int main() {
scanf("%ld%ld", &n, &m);
long q;
for (q = 1; q <= n; q++) edges[q].resize(0);
for (q = 1; q <= m; q++) {
long t1, t2;
scanf("%ld%ld", &t1, &t2);
edges[t1].push_back(t2);
edges[t2].push_back(t1);
}
cht = 0;
for (q = 1; q <= n; q++) mark[q] = 0;
long count = 0;
for (q = 1; q <= n; q++)
if (mark[q] == 0) {
dfs(q, -1);
count++;
}
cht = cht / 2;
if ((cht != 1) || (count > 1))
printf("NO");
else
printf("FHTAGN!");
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 101;
int mat[N][N];
bool chk[N];
int cnt;
int n, m;
void dfs(int now) {
if (chk[now]) return;
chk[now] = true;
cnt++;
for (int i = 1; i <= n; i++) {
if (mat[now][i]) {
dfs(i);
}
}
}
int main() {
while (scanf("%d %d", &n, &m) == 2) {
memset(mat, 0, sizeof(mat));
memset(chk, 0, sizeof(chk));
cnt = 0;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
mat[u][v] = mat[v][u] = 1;
}
if (n != m) {
printf("NO\n");
continue;
}
dfs(1);
cnt == n ? printf("FHTAGN!\n") : printf("NO\n");
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 101;
int mat[N][N];
bool chk[N];
int cnt;
int n, m;
void dfs(int now) {
if (chk[now]) return;
chk[now] = true;
cnt++;
for (int i = 1; i <= n; i++) {
if (mat[now][i]) {
dfs(i);
}
}
}
int main() {
while (scanf("%d %d", &n, &m) == 2) {
memset(mat, 0, sizeof(mat));
memset(chk, 0, sizeof(chk));
cnt = 0;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
mat[u][v] = mat[v][u] = 1;
}
if (n != m) {
printf("NO\n");
continue;
}
dfs(1);
cnt == n ? printf("FHTAGN!\n") : printf("NO\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int find_parent(int a, int *parent) {
if (parent[a] == a) return a;
return find_parent(parent[a], parent);
}
int dfs(int u, int v, vector<vector<int>> &adj, bool *visited) {
visited[u] = true;
if (u == v) return 0;
int myres = INT_MIN;
for (int i = 0; i < adj[u].size(); i++) {
int s = adj[u][i];
if (!visited[s]) myres = max(myres, 1 + dfs(s, v, adj, visited));
}
return myres;
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n + 1);
int *parent = new int[n + 1];
for (int i = 1; i <= n; i++) parent[i] = i;
bool temp = false;
bool *visited = new bool[n + 1];
for (int i = 0; i <= n; i++) visited[i] = false;
int ln;
int myx = -1;
int myy = -1;
int cnt = 0;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
ln = a;
int g = find_parent(a, parent);
int e = find_parent(b, parent);
parent[g] = e;
if (g == e) {
cnt++;
temp = true;
myx = a;
myy = b;
}
}
if (!temp || cnt > 1 || n == m + 1)
cout << "NO" << endl;
else {
int k = dfs(myx, myy, adj, visited);
if (k >= 2)
cout << "FHTAGN!" << endl;
else
cout << "-1" << endl;
}
}
| ### Prompt
In Cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int find_parent(int a, int *parent) {
if (parent[a] == a) return a;
return find_parent(parent[a], parent);
}
int dfs(int u, int v, vector<vector<int>> &adj, bool *visited) {
visited[u] = true;
if (u == v) return 0;
int myres = INT_MIN;
for (int i = 0; i < adj[u].size(); i++) {
int s = adj[u][i];
if (!visited[s]) myres = max(myres, 1 + dfs(s, v, adj, visited));
}
return myres;
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n + 1);
int *parent = new int[n + 1];
for (int i = 1; i <= n; i++) parent[i] = i;
bool temp = false;
bool *visited = new bool[n + 1];
for (int i = 0; i <= n; i++) visited[i] = false;
int ln;
int myx = -1;
int myy = -1;
int cnt = 0;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
ln = a;
int g = find_parent(a, parent);
int e = find_parent(b, parent);
parent[g] = e;
if (g == e) {
cnt++;
temp = true;
myx = a;
myy = b;
}
}
if (!temp || cnt > 1 || n == m + 1)
cout << "NO" << endl;
else {
int k = dfs(myx, myy, adj, visited);
if (k >= 2)
cout << "FHTAGN!" << endl;
else
cout << "-1" << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
bool a[maxn][maxn];
int u[maxn];
vector<int> st;
int n, m;
void dfs0(int x) {
u[x] = 1;
for (int i = 0; i < n; i++) {
if (a[x][i] && !u[i]) dfs0(i);
}
}
int dfs(int x, int p = -1) {
u[x] = 1;
st.push_back(x);
for (int i = 0; i < n; i++)
if (a[x][i] && i != p) {
if (u[i] == 2) continue;
if (u[i] == 1) {
int t = 0;
while (st[i] != i) ++t;
int s = st.size() - t;
a[st[t]][x] = 0;
a[x][st[t]] = 0;
while (t < st.size() - 1) {
a[st[t]][st[t + 1]] = 0;
a[st[t + 1]][st[t]] = 0;
++t;
}
return s;
}
}
}
int main() {
cin >> n >> m;
memset(a, 0, sizeof(a));
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
--x, --y;
a[x][y] = a[y][x] = 1;
}
memset(u, 0, sizeof(u));
dfs0(0);
for (int i = 0; i < n; i++) {
if (!u[i]) {
printf("NO\n");
return 0;
}
}
printf("%s\n", m == n ? "FHTAGN!" : "NO");
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
bool a[maxn][maxn];
int u[maxn];
vector<int> st;
int n, m;
void dfs0(int x) {
u[x] = 1;
for (int i = 0; i < n; i++) {
if (a[x][i] && !u[i]) dfs0(i);
}
}
int dfs(int x, int p = -1) {
u[x] = 1;
st.push_back(x);
for (int i = 0; i < n; i++)
if (a[x][i] && i != p) {
if (u[i] == 2) continue;
if (u[i] == 1) {
int t = 0;
while (st[i] != i) ++t;
int s = st.size() - t;
a[st[t]][x] = 0;
a[x][st[t]] = 0;
while (t < st.size() - 1) {
a[st[t]][st[t + 1]] = 0;
a[st[t + 1]][st[t]] = 0;
++t;
}
return s;
}
}
}
int main() {
cin >> n >> m;
memset(a, 0, sizeof(a));
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
--x, --y;
a[x][y] = a[y][x] = 1;
}
memset(u, 0, sizeof(u));
dfs0(0);
for (int i = 0; i < n; i++) {
if (!u[i]) {
printf("NO\n");
return 0;
}
}
printf("%s\n", m == n ? "FHTAGN!" : "NO");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int g[128][128];
vector<bool> visited;
int n, m;
void dfs(int v = 0) {
for (int i = 0; i < n; i++)
if (!visited[i] && g[v][i]) visited[i] = true, dfs(i);
}
int main() {
cin >> n >> m;
if (n != m) {
cout << "NO" << endl;
return 0;
}
visited.resize(n, false);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[x - 1][y - 1] = g[y - 1][x - 1] = 1;
}
dfs();
if (find(visited.begin(), visited.end(), false) != visited.end()) {
cout << "NO" << endl;
} else {
cout << "FHTAGN!" << endl;
}
}
| ### Prompt
In cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int g[128][128];
vector<bool> visited;
int n, m;
void dfs(int v = 0) {
for (int i = 0; i < n; i++)
if (!visited[i] && g[v][i]) visited[i] = true, dfs(i);
}
int main() {
cin >> n >> m;
if (n != m) {
cout << "NO" << endl;
return 0;
}
visited.resize(n, false);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[x - 1][y - 1] = g[y - 1][x - 1] = 1;
}
dfs();
if (find(visited.begin(), visited.end(), false) != visited.end()) {
cout << "NO" << endl;
} else {
cout << "FHTAGN!" << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T Get_Max(const T& a, const T& b) {
return a < b ? b : a;
}
template <class T>
inline T Get_Min(const T& a, const T& b) {
return a < b ? a : b;
}
const int maxn = 110;
vector<int> G[maxn];
int mark[maxn];
int dfs(int u) {
int cnt = 0;
if (mark[u])
return 0;
else {
mark[u] = 1;
cnt++;
for (int i = 0; i < G[u].size(); i++) {
cnt += dfs(G[u][i]);
}
}
return cnt;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
memset(mark, 0, sizeof mark);
if (n == m && dfs(1) == n)
printf("FHTAGN!\n");
else
printf("NO\n");
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T Get_Max(const T& a, const T& b) {
return a < b ? b : a;
}
template <class T>
inline T Get_Min(const T& a, const T& b) {
return a < b ? a : b;
}
const int maxn = 110;
vector<int> G[maxn];
int mark[maxn];
int dfs(int u) {
int cnt = 0;
if (mark[u])
return 0;
else {
mark[u] = 1;
cnt++;
for (int i = 0; i < G[u].size(); i++) {
cnt += dfs(G[u][i]);
}
}
return cnt;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
memset(mark, 0, sizeof mark);
if (n == m && dfs(1) == n)
printf("FHTAGN!\n");
else
printf("NO\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000;
vector<int> g[MAX_N + 10];
vector<int> dfs_trace;
bool is_circle[MAX_N + 10] = {0};
int circles_finded = 0;
int b[MAX_N + 10] = {0};
void scanGraph(int m) {
int x;
int y;
for (int i = 0; i < m; ++i) {
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
}
void dfs(int v, int prev) {
b[v] = 1;
dfs_trace.push_back(v);
for (int e = 0; e < g[v].size(); ++e) {
if (b[g[v][e]] == 1 && prev != g[v][e]) {
circles_finded++;
for (int i = dfs_trace.size() - 1; dfs_trace[i] != g[v][e];
is_circle[dfs_trace[i]] = true, --i)
;
is_circle[g[v][e]] = true;
}
if (b[g[v][e]] == 0) {
dfs(g[v][e], v);
}
}
while (dfs_trace[dfs_trace.size() - 1] != v) {
dfs_trace.pop_back();
}
dfs_trace.pop_back();
b[v] = 2;
}
bool find1Cilrclre(int n) {
dfs(1, 0);
for (int i = 1; i <= n; ++i) {
if (b[i] != 2) return false;
}
return (circles_finded == 1);
}
int countTreesOnCircle(int n) {
int count = 0;
for (int i = 1; i <= n; ++i) {
if (is_circle[i]) {
count++;
}
}
return count;
}
int main() {
int n;
int m;
scanf("%d %d", &n, &m);
scanGraph(m);
if (!find1Cilrclre(n)) {
printf("NO");
return 0;
}
if (countTreesOnCircle(n) < 3) {
printf("NO");
return 0;
}
printf("FHTAGN!");
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000;
vector<int> g[MAX_N + 10];
vector<int> dfs_trace;
bool is_circle[MAX_N + 10] = {0};
int circles_finded = 0;
int b[MAX_N + 10] = {0};
void scanGraph(int m) {
int x;
int y;
for (int i = 0; i < m; ++i) {
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
}
void dfs(int v, int prev) {
b[v] = 1;
dfs_trace.push_back(v);
for (int e = 0; e < g[v].size(); ++e) {
if (b[g[v][e]] == 1 && prev != g[v][e]) {
circles_finded++;
for (int i = dfs_trace.size() - 1; dfs_trace[i] != g[v][e];
is_circle[dfs_trace[i]] = true, --i)
;
is_circle[g[v][e]] = true;
}
if (b[g[v][e]] == 0) {
dfs(g[v][e], v);
}
}
while (dfs_trace[dfs_trace.size() - 1] != v) {
dfs_trace.pop_back();
}
dfs_trace.pop_back();
b[v] = 2;
}
bool find1Cilrclre(int n) {
dfs(1, 0);
for (int i = 1; i <= n; ++i) {
if (b[i] != 2) return false;
}
return (circles_finded == 1);
}
int countTreesOnCircle(int n) {
int count = 0;
for (int i = 1; i <= n; ++i) {
if (is_circle[i]) {
count++;
}
}
return count;
}
int main() {
int n;
int m;
scanf("%d %d", &n, &m);
scanGraph(m);
if (!find1Cilrclre(n)) {
printf("NO");
return 0;
}
if (countTreesOnCircle(n) < 3) {
printf("NO");
return 0;
}
printf("FHTAGN!");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> x[101];
int vis[101] = {0}, flag = 0;
void dfs(int i, int parent) {
if (vis[i]) {
if (vis[i] != parent) flag++;
return;
}
vis[i] = parent;
for (int j = 0; j < x[i].size(); ++j) {
if (x[i][j] == parent) continue;
dfs(x[i][j], i);
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int t, t1;
cin >> t >> t1;
x[t].push_back(t1);
x[t1].push_back(t);
}
int z = 0;
if (n != m) {
cout << "NO";
return 0;
}
for (int i = 1; i <= n; ++i)
if (!vis[i]) {
dfs(1, 0);
++z;
}
if (flag >= 1 && z == 1)
cout << "FHTAGN!";
else
cout << "NO";
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> x[101];
int vis[101] = {0}, flag = 0;
void dfs(int i, int parent) {
if (vis[i]) {
if (vis[i] != parent) flag++;
return;
}
vis[i] = parent;
for (int j = 0; j < x[i].size(); ++j) {
if (x[i][j] == parent) continue;
dfs(x[i][j], i);
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int t, t1;
cin >> t >> t1;
x[t].push_back(t1);
x[t1].push_back(t);
}
int z = 0;
if (n != m) {
cout << "NO";
return 0;
}
for (int i = 1; i <= n; ++i)
if (!vis[i]) {
dfs(1, 0);
++z;
}
if (flag >= 1 && z == 1)
cout << "FHTAGN!";
else
cout << "NO";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int i;
int x, y;
vector<int> a[100];
int color[100];
int c_count;
bool isCyclic(int node, int prev) {
int i;
color[node] = 1;
for (i = 0; i < a[node].size(); i++) {
int k = a[node][i];
if (k == prev) continue;
if (color[k] == 1) return true;
if (color[k] == 0)
if (isCyclic(k, node)) return true;
}
color[node] = 2;
return false;
}
void dfs(int node) {
color[node] = 1;
int i;
for (i = 0; i < a[node].size(); i++) {
int k = a[node][i];
if (color[k] == 0) dfs(k);
}
return;
}
void Cnt(int node, int prev) {
int i;
color[node] = 1;
for (i = 0; i < a[node].size(); i++) {
int k = a[node][i];
if (k == prev) continue;
if (color[k] == 1) c_count++;
if (color[k] == 0) Cnt(k, node);
}
color[node] = 2;
return;
}
int main() {
scanf("%d %d", &n, &m);
for (i = 0; i < m; i++) {
scanf("%d %d", &x, &y);
x--;
y--;
a[x].push_back(y);
a[y].push_back(x);
}
if (n <= 2) {
printf("NO\n");
return 0;
}
for (i = 0; i < n; i++) color[i] = 0;
dfs(0);
for (i = 0; i < n; i++)
if (color[i] == 0) {
printf("NO\n");
return 0;
}
for (i = 0; i < n; i++) color[i] = 0;
if (!isCyclic(0, -1)) {
printf("NO\n");
return 0;
}
for (i = 0; i < n; i++) color[i] = 0;
Cnt(0, -1);
if (c_count > 1) {
printf("NO\n");
return 0;
}
printf("FHTAGN!\n");
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
int i;
int x, y;
vector<int> a[100];
int color[100];
int c_count;
bool isCyclic(int node, int prev) {
int i;
color[node] = 1;
for (i = 0; i < a[node].size(); i++) {
int k = a[node][i];
if (k == prev) continue;
if (color[k] == 1) return true;
if (color[k] == 0)
if (isCyclic(k, node)) return true;
}
color[node] = 2;
return false;
}
void dfs(int node) {
color[node] = 1;
int i;
for (i = 0; i < a[node].size(); i++) {
int k = a[node][i];
if (color[k] == 0) dfs(k);
}
return;
}
void Cnt(int node, int prev) {
int i;
color[node] = 1;
for (i = 0; i < a[node].size(); i++) {
int k = a[node][i];
if (k == prev) continue;
if (color[k] == 1) c_count++;
if (color[k] == 0) Cnt(k, node);
}
color[node] = 2;
return;
}
int main() {
scanf("%d %d", &n, &m);
for (i = 0; i < m; i++) {
scanf("%d %d", &x, &y);
x--;
y--;
a[x].push_back(y);
a[y].push_back(x);
}
if (n <= 2) {
printf("NO\n");
return 0;
}
for (i = 0; i < n; i++) color[i] = 0;
dfs(0);
for (i = 0; i < n; i++)
if (color[i] == 0) {
printf("NO\n");
return 0;
}
for (i = 0; i < n; i++) color[i] = 0;
if (!isCyclic(0, -1)) {
printf("NO\n");
return 0;
}
for (i = 0; i < n; i++) color[i] = 0;
Cnt(0, -1);
if (c_count > 1) {
printf("NO\n");
return 0;
}
printf("FHTAGN!\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
int a = 0;
vector<int> v[101];
int visited[101];
int parent[101];
void dsf(int s) {
visited[s] = 1;
for (int n = 0; n < v[s].size(); n++) {
int d = v[s][n];
if (parent[s] != d && visited[d] != 2) {
if (visited[d] == 1) {
ans++;
} else {
visited[d] = 1;
parent[d] = s;
dsf(d);
}
}
}
visited[s] = 2;
}
int main() {
int n, e;
cin >> n >> e;
int s, d;
for (int m = 0; m < e; m++) {
cin >> s >> d;
v[s].push_back(d);
v[d].push_back(s);
}
for (int m = 1; m <= n; m++) {
if (visited[m] == 0) {
parent[m] = m;
dsf(m);
a++;
}
}
if (a == 1 && ans == 1)
cout << "FHTAGN!";
else
cout << "NO";
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
int a = 0;
vector<int> v[101];
int visited[101];
int parent[101];
void dsf(int s) {
visited[s] = 1;
for (int n = 0; n < v[s].size(); n++) {
int d = v[s][n];
if (parent[s] != d && visited[d] != 2) {
if (visited[d] == 1) {
ans++;
} else {
visited[d] = 1;
parent[d] = s;
dsf(d);
}
}
}
visited[s] = 2;
}
int main() {
int n, e;
cin >> n >> e;
int s, d;
for (int m = 0; m < e; m++) {
cin >> s >> d;
v[s].push_back(d);
v[d].push_back(s);
}
for (int m = 1; m <= n; m++) {
if (visited[m] == 0) {
parent[m] = m;
dsf(m);
a++;
}
}
if (a == 1 && ans == 1)
cout << "FHTAGN!";
else
cout << "NO";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long INF64 = 3e18;
const int mod = (int)1e9 + 7;
const int MAX_N = 100000 + 5;
long long binp(long long a, long long b) {
if (b == 0) return 1;
long long ans = binp(a, b / 2);
long long tmp = (ans * ans);
if (b % 2) return ((tmp * a));
return ((tmp));
}
void display(vector<long long> v) {
for (auto x : v) cout << x << " ";
cout << "\n";
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void qr(long long i, long long j) {
cout << "? " << i << " " << j << '\n';
cout.flush();
}
void here(long long a) { cout << "here " << a << '\n'; }
const long long nmax = 150005;
long long par[nmax];
long long siize[nmax];
long long find(long long x) {
if (par[x] == x) return x;
return par[x] = find(par[x]);
}
void merge(long long a, long long b) {
long long x = find(a);
long long y = find(b);
if (x == y) return;
long long sizex = siize[x];
long long sizey = siize[y];
if (sizex < sizey) {
par[x] = par[y];
siize[y] += siize[x];
} else {
par[y] = par[x];
siize[x] += siize[y];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m;
cin >> n >> m;
for (long long i = 0; i < n; i++) {
par[i] = i;
siize[i] = 1;
}
for (long long j = 0; j < m; j++) {
long long a, b;
cin >> a >> b;
merge(a - 1, b - 1);
}
if (n == m && siize[find(0)] == n)
cout << "FHTAGN!\n";
else
cout << "NO\n";
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF64 = 3e18;
const int mod = (int)1e9 + 7;
const int MAX_N = 100000 + 5;
long long binp(long long a, long long b) {
if (b == 0) return 1;
long long ans = binp(a, b / 2);
long long tmp = (ans * ans);
if (b % 2) return ((tmp * a));
return ((tmp));
}
void display(vector<long long> v) {
for (auto x : v) cout << x << " ";
cout << "\n";
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void qr(long long i, long long j) {
cout << "? " << i << " " << j << '\n';
cout.flush();
}
void here(long long a) { cout << "here " << a << '\n'; }
const long long nmax = 150005;
long long par[nmax];
long long siize[nmax];
long long find(long long x) {
if (par[x] == x) return x;
return par[x] = find(par[x]);
}
void merge(long long a, long long b) {
long long x = find(a);
long long y = find(b);
if (x == y) return;
long long sizex = siize[x];
long long sizey = siize[y];
if (sizex < sizey) {
par[x] = par[y];
siize[y] += siize[x];
} else {
par[y] = par[x];
siize[x] += siize[y];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m;
cin >> n >> m;
for (long long i = 0; i < n; i++) {
par[i] = i;
siize[i] = 1;
}
for (long long j = 0; j < m; j++) {
long long a, b;
cin >> a >> b;
merge(a - 1, b - 1);
}
if (n == m && siize[find(0)] == n)
cout << "FHTAGN!\n";
else
cout << "NO\n";
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, m, x, y;
void dfs(int s, vector<bool>& visited, vector<vector<long long> >& g) {
visited[s] = true;
for (int i = 0; i < g[s].size(); i++) {
if (visited[g[s][i]] == false) dfs(g[s][i], visited, g);
}
}
int main() {
cin >> n >> m;
vector<vector<long long> > g(n + 1);
vector<bool> visited(n + 1);
for (long long i = 0; i < m; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
if (n != m) {
cout << "NO";
return 0;
}
dfs(x, visited, g);
for (long long i = 1; i <= n; i++)
if (visited[i] == false) {
cout << "NO";
return 0;
}
cout << "FHTAGN!";
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, m, x, y;
void dfs(int s, vector<bool>& visited, vector<vector<long long> >& g) {
visited[s] = true;
for (int i = 0; i < g[s].size(); i++) {
if (visited[g[s][i]] == false) dfs(g[s][i], visited, g);
}
}
int main() {
cin >> n >> m;
vector<vector<long long> > g(n + 1);
vector<bool> visited(n + 1);
for (long long i = 0; i < m; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
if (n != m) {
cout << "NO";
return 0;
}
dfs(x, visited, g);
for (long long i = 1; i <= n; i++)
if (visited[i] == false) {
cout << "NO";
return 0;
}
cout << "FHTAGN!";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int fa[1005];
int n, m;
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void merge(int u, int v) {
int ru = find(u);
int rv = find(v);
if (ru != rv) {
fa[ru] = rv;
}
}
int main() {
int u, v;
int flag = 0, fail = 0, r = 0;
cin >> n >> m;
int temp = m;
for (int i = 0; i < n; i++) {
fa[i] = i;
}
while (temp--) {
cin >> u >> v;
if (fail) continue;
int ru = find(u);
int rv = find(v);
if (ru != rv) {
r++;
fa[ru] = rv;
} else {
if (!flag) {
r++;
flag = 1;
} else {
fail = 1;
}
}
}
if (fail || !flag || n != m) {
cout << "NO" << endl;
} else {
cout << "FHTAGN!" << endl;
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int fa[1005];
int n, m;
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void merge(int u, int v) {
int ru = find(u);
int rv = find(v);
if (ru != rv) {
fa[ru] = rv;
}
}
int main() {
int u, v;
int flag = 0, fail = 0, r = 0;
cin >> n >> m;
int temp = m;
for (int i = 0; i < n; i++) {
fa[i] = i;
}
while (temp--) {
cin >> u >> v;
if (fail) continue;
int ru = find(u);
int rv = find(v);
if (ru != rv) {
r++;
fa[ru] = rv;
} else {
if (!flag) {
r++;
flag = 1;
} else {
fail = 1;
}
}
}
if (fail || !flag || n != m) {
cout << "NO" << endl;
} else {
cout << "FHTAGN!" << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int hits = 0;
vector<int> edges[101];
void print_vector2d(vector<vector<int>>& a, string name) {
cout << name << " is\n";
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[0].size(); j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
}
void print_vector(vector<int>& a, string name) {
cout << name << " is\n";
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}
cout << "\n";
}
void dfs(int n, vector<int>& visited) {
visited[n] = 1;
for (int i = 0; i < edges[n].size(); i++) {
if (visited[edges[n][i]] == 1)
hits += 1;
else {
dfs(edges[n][i], visited);
}
}
}
int main() {
int n, m;
cin >> n >> m;
vector<int> visited(n + 1, 0);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
edges[a].push_back(b);
edges[b].push_back(a);
}
dfs(1, visited);
bool all_visited = true;
for (int i = 1; i <= n; i++) {
if (visited[i] != 1) {
all_visited = false;
break;
}
}
if (all_visited && m == n) {
cout << "FHTAGN!\n";
} else {
cout << "NO\n";
}
return (0);
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int hits = 0;
vector<int> edges[101];
void print_vector2d(vector<vector<int>>& a, string name) {
cout << name << " is\n";
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[0].size(); j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
}
void print_vector(vector<int>& a, string name) {
cout << name << " is\n";
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}
cout << "\n";
}
void dfs(int n, vector<int>& visited) {
visited[n] = 1;
for (int i = 0; i < edges[n].size(); i++) {
if (visited[edges[n][i]] == 1)
hits += 1;
else {
dfs(edges[n][i], visited);
}
}
}
int main() {
int n, m;
cin >> n >> m;
vector<int> visited(n + 1, 0);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
edges[a].push_back(b);
edges[b].push_back(a);
}
dfs(1, visited);
bool all_visited = true;
for (int i = 1; i <= n; i++) {
if (visited[i] != 1) {
all_visited = false;
break;
}
}
if (all_visited && m == n) {
cout << "FHTAGN!\n";
} else {
cout << "NO\n";
}
return (0);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long)1e15 + 1;
const int MAX = 1e5 + 5;
vector<vector<int> > adjLst;
int n, m, vis[101], countt = 0, x, y;
void dfs(int cur = 1) {
if (vis[cur]) return;
vis[cur] = 1;
for (int i = 0; i < adjLst[cur].size(); i++) {
int child = adjLst[cur][i];
if (!vis[child]) {
countt++;
dfs(child);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
adjLst.resize(n + 1);
for (int i = 0; i < m; i++) {
cin >> x >> y;
adjLst[x].push_back(y);
adjLst[y].push_back(x);
}
dfs();
if (countt != m - 1) return cout << "NO\n", 0;
for (int i = 1; i < n + 1; i++)
if (!vis[i]) return cout << "NO\n", 0;
cout << "FHTAGN!\n";
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long)1e15 + 1;
const int MAX = 1e5 + 5;
vector<vector<int> > adjLst;
int n, m, vis[101], countt = 0, x, y;
void dfs(int cur = 1) {
if (vis[cur]) return;
vis[cur] = 1;
for (int i = 0; i < adjLst[cur].size(); i++) {
int child = adjLst[cur][i];
if (!vis[child]) {
countt++;
dfs(child);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
adjLst.resize(n + 1);
for (int i = 0; i < m; i++) {
cin >> x >> y;
adjLst[x].push_back(y);
adjLst[y].push_back(x);
}
dfs();
if (countt != m - 1) return cout << "NO\n", 0;
for (int i = 1; i < n + 1; i++)
if (!vis[i]) return cout << "NO\n", 0;
cout << "FHTAGN!\n";
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> mp[105];
int vis[105];
struct node {
int fa;
int kid;
node(int fa, int b) {
this->fa = fa;
kid = b;
}
};
bool bfs(int rt, int n) {
queue<node> q;
q.push(node(-1, rt));
vis[rt] = 1;
while (!q.empty()) {
int now = q.front().kid;
int fa = q.front().fa;
q.pop();
for (int i = 0; i < mp[now].size(); i++) {
int v = mp[now][i];
if (vis[v] && v != fa) {
vis[v]++;
continue;
}
if (vis[v] || v == fa) continue;
vis[v] = 1;
q.push(node(now, v));
}
}
int f = 0;
for (int i = 1; i <= n; i++) {
if (vis[i] > 2) return false;
if (vis[i] == 2) f = 1;
}
if (f) return true;
return false;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
mp[u].push_back(v);
mp[v].push_back(u);
}
int yes = 0;
int no = 0;
for (int i = 1; i <= n; i++) {
if (vis[i] == 0) {
if (bfs(i, n)) {
yes++;
} else {
no = 1;
}
}
}
if (yes == 1 && no == 0)
puts("FHTAGN!");
else
puts("NO");
}
| ### Prompt
Create a solution in CPP for the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> mp[105];
int vis[105];
struct node {
int fa;
int kid;
node(int fa, int b) {
this->fa = fa;
kid = b;
}
};
bool bfs(int rt, int n) {
queue<node> q;
q.push(node(-1, rt));
vis[rt] = 1;
while (!q.empty()) {
int now = q.front().kid;
int fa = q.front().fa;
q.pop();
for (int i = 0; i < mp[now].size(); i++) {
int v = mp[now][i];
if (vis[v] && v != fa) {
vis[v]++;
continue;
}
if (vis[v] || v == fa) continue;
vis[v] = 1;
q.push(node(now, v));
}
}
int f = 0;
for (int i = 1; i <= n; i++) {
if (vis[i] > 2) return false;
if (vis[i] == 2) f = 1;
}
if (f) return true;
return false;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
mp[u].push_back(v);
mp[v].push_back(u);
}
int yes = 0;
int no = 0;
for (int i = 1; i <= n; i++) {
if (vis[i] == 0) {
if (bfs(i, n)) {
yes++;
} else {
no = 1;
}
}
}
if (yes == 1 && no == 0)
puts("FHTAGN!");
else
puts("NO");
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a, b, n, m, ok, passou[1 << 20];
vector<int> grafo[1 << 20];
void dfs(int u) {
passou[u] = 1;
for (int i = 0; i < grafo[u].size(); i++) {
int v = grafo[u][i];
if (!passou[v]) dfs(v);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
if (n != m) ok = 1;
while (m--) {
cin >> a >> b;
grafo[a].push_back(b);
grafo[b].push_back(a);
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (!passou[i]) ok = 1;
}
cout << (!ok ? "FHTAGN!" : "NO") << endl;
}
| ### Prompt
In CPP, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a, b, n, m, ok, passou[1 << 20];
vector<int> grafo[1 << 20];
void dfs(int u) {
passou[u] = 1;
for (int i = 0; i < grafo[u].size(); i++) {
int v = grafo[u][i];
if (!passou[v]) dfs(v);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
if (n != m) ok = 1;
while (m--) {
cin >> a >> b;
grafo[a].push_back(b);
grafo[b].push_back(a);
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (!passou[i]) ok = 1;
}
cout << (!ok ? "FHTAGN!" : "NO") << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> gr[1000];
int vis[1000];
int ans = 0;
void dfs(int a) {
vis[a] = 1;
ans++;
for (int i = 0; i < gr[a].size(); i++)
if (vis[gr[a][i]] == 0) dfs(gr[a][i]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
memset(vis, 0, sizeof(vis));
int n, m, a, b;
cin >> n >> m;
set<int> st;
int h;
for (int i = 0; i < m; i++) {
cin >> a >> b;
gr[a].push_back(b);
gr[b].push_back(a);
st.insert(a);
st.insert(b);
h = a;
}
if (m > 0) dfs(h);
if (ans == m && m > 0 && m == n)
cout << "FHTAGN!" << ("\n");
else
cout << "NO" << ("\n");
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> gr[1000];
int vis[1000];
int ans = 0;
void dfs(int a) {
vis[a] = 1;
ans++;
for (int i = 0; i < gr[a].size(); i++)
if (vis[gr[a][i]] == 0) dfs(gr[a][i]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
memset(vis, 0, sizeof(vis));
int n, m, a, b;
cin >> n >> m;
set<int> st;
int h;
for (int i = 0; i < m; i++) {
cin >> a >> b;
gr[a].push_back(b);
gr[b].push_back(a);
st.insert(a);
st.insert(b);
h = a;
}
if (m > 0) dfs(h);
if (ans == m && m > 0 && m == n)
cout << "FHTAGN!" << ("\n");
else
cout << "NO" << ("\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int eD[150][150], mark[150], delx, dely, flag, n, m;
int dfs(int x, int rem) {
mark[x] = 1;
for (int i = 1; i <= n; i++) {
if (!eD[x][i] || i == rem) continue;
if (mark[i]) {
delx = x;
dely = i;
flag = 1;
return 1;
} else
dfs(i, x);
if (flag) return 1;
}
return 0;
}
int main() {
int x, y;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
eD[x][y] = eD[y][x] = 1;
}
if (!dfs(1, -1)) {
puts("NO");
return 0;
}
flag = eD[delx][dely] = eD[dely][delx] = 0;
memset(mark, 0, sizeof(mark));
flag = dfs(1, -1);
for (int i = 1; i <= n; i++)
if (!mark[i]) {
flag = 1;
break;
}
if (flag)
puts("NO");
else
puts("FHTAGN!");
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int eD[150][150], mark[150], delx, dely, flag, n, m;
int dfs(int x, int rem) {
mark[x] = 1;
for (int i = 1; i <= n; i++) {
if (!eD[x][i] || i == rem) continue;
if (mark[i]) {
delx = x;
dely = i;
flag = 1;
return 1;
} else
dfs(i, x);
if (flag) return 1;
}
return 0;
}
int main() {
int x, y;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
eD[x][y] = eD[y][x] = 1;
}
if (!dfs(1, -1)) {
puts("NO");
return 0;
}
flag = eD[delx][dely] = eD[dely][delx] = 0;
memset(mark, 0, sizeof(mark));
flag = dfs(1, -1);
for (int i = 1; i <= n; i++)
if (!mark[i]) {
flag = 1;
break;
}
if (flag)
puts("NO");
else
puts("FHTAGN!");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MX = (1 << 20);
class Disjoint_Set {
public:
int sz[MX], P[MX];
int find_(int x) {
if (x != P[x]) P[x] = find_(P[x]);
return P[x];
}
int comp;
void init(int N) {
for (int j = 1; j <= N; j++) sz[j] = 1, P[j] = j;
comp = N;
}
bool same(int x, int y) { return find_(x) == find_(y); }
int compsize(int node) { return sz[find_(node)]; }
bool merge_(int x, int y) {
int px = find_(x), py = find_(y);
if (px == py) return false;
if (sz[px] < sz[py]) swap(px, py);
P[py] = px;
comp--;
sz[px] += sz[py];
return true;
}
} G;
int n, m;
int main() {
scanf("%d %d", &n, &m);
if (n != m) {
puts("NO");
return 0;
}
G.init(n);
int tt = 0;
while (m--) {
int a, b;
scanf("%d %d", &a, &b);
if (!G.merge_(a, b)) tt++;
}
if (tt == 1)
puts("FHTAGN!");
else
puts("NO");
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MX = (1 << 20);
class Disjoint_Set {
public:
int sz[MX], P[MX];
int find_(int x) {
if (x != P[x]) P[x] = find_(P[x]);
return P[x];
}
int comp;
void init(int N) {
for (int j = 1; j <= N; j++) sz[j] = 1, P[j] = j;
comp = N;
}
bool same(int x, int y) { return find_(x) == find_(y); }
int compsize(int node) { return sz[find_(node)]; }
bool merge_(int x, int y) {
int px = find_(x), py = find_(y);
if (px == py) return false;
if (sz[px] < sz[py]) swap(px, py);
P[py] = px;
comp--;
sz[px] += sz[py];
return true;
}
} G;
int n, m;
int main() {
scanf("%d %d", &n, &m);
if (n != m) {
puts("NO");
return 0;
}
G.init(n);
int tt = 0;
while (m--) {
int a, b;
scanf("%d %d", &a, &b);
if (!G.merge_(a, b)) tt++;
}
if (tt == 1)
puts("FHTAGN!");
else
puts("NO");
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int x, y;
vector<int> ga[200];
bool vis[200];
void dfs(int u) {
vis[u] = true;
for (int i = 0; i < ga[u].size(); i++) {
int v = ga[u][i];
if (!vis[v]) dfs(v);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
ga[y].push_back(x);
ga[x].push_back(y);
}
dfs(1);
if (n != m) {
cout << "NO" << endl;
return 0;
} else {
if (n < 3) {
cout << "NO" << endl;
return 0;
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cout << "NO" << endl;
return 0;
}
}
cout << "FHTAGN!" << endl;
}
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
int x, y;
vector<int> ga[200];
bool vis[200];
void dfs(int u) {
vis[u] = true;
for (int i = 0; i < ga[u].size(); i++) {
int v = ga[u][i];
if (!vis[v]) dfs(v);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
ga[y].push_back(x);
ga[x].push_back(y);
}
dfs(1);
if (n != m) {
cout << "NO" << endl;
return 0;
} else {
if (n < 3) {
cout << "NO" << endl;
return 0;
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cout << "NO" << endl;
return 0;
}
}
cout << "FHTAGN!" << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int P[102];
int p(int x) {
if (P[x] == x)
return x;
else
return p(P[x]);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= 100; i++) {
P[i] = i;
}
for (int i = 1; i <= m; i++) {
int k, l;
scanf("%d%d", &k, &l);
int u = p(k);
int v = p(l);
P[u] = v;
}
if (n != m) {
printf("NO");
return 0;
}
for (int i = 1; i < n; i++) {
if (p(i) != p(i + 1)) {
printf("NO");
return 0;
}
}
printf("FHTAGN!");
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
int P[102];
int p(int x) {
if (P[x] == x)
return x;
else
return p(P[x]);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= 100; i++) {
P[i] = i;
}
for (int i = 1; i <= m; i++) {
int k, l;
scanf("%d%d", &k, &l);
int u = p(k);
int v = p(l);
P[u] = v;
}
if (n != m) {
printf("NO");
return 0;
}
for (int i = 1; i < n; i++) {
if (p(i) != p(i + 1)) {
printf("NO");
return 0;
}
}
printf("FHTAGN!");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
vector<int> adj[N];
int n, m, cnt = 0;
bool vis[N];
void dfs(int u) {
cnt++;
vis[u] = 1;
for (auto v : adj[u])
if (!vis[v]) dfs(v);
}
int main() {
scanf("%d %d", &n, &m);
if (n != m) {
printf("NO\n");
return 0;
}
int u, v;
for (int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
if (cnt == n)
printf("FHTAGN!\n");
else
printf("NO\n");
}
| ### Prompt
Please create a solution in cpp to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
vector<int> adj[N];
int n, m, cnt = 0;
bool vis[N];
void dfs(int u) {
cnt++;
vis[u] = 1;
for (auto v : adj[u])
if (!vis[v]) dfs(v);
}
int main() {
scanf("%d %d", &n, &m);
if (n != m) {
printf("NO\n");
return 0;
}
int u, v;
for (int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
if (cnt == n)
printf("FHTAGN!\n");
else
printf("NO\n");
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = (100 + 10);
int N, M, cnt, mark[MAX_N];
vector<int> g[MAX_N];
void dfs(int u) {
mark[u] = 1;
++cnt;
for (int i = (0); i < ((int)g[u].size()); ++i) {
int v = g[u][i];
if (!mark[v]) dfs(v);
}
}
int main() {
while (cin >> N >> M) {
for (int i = (1); i <= (N); ++i) g[i].clear();
memset(mark, 0, sizeof(mark));
for (int i = (1); i <= (M); ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
if (N == M) {
cnt = 0;
dfs(1);
cnt == N ? puts("FHTAGN!") : puts("NO");
} else
puts("NO");
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = (100 + 10);
int N, M, cnt, mark[MAX_N];
vector<int> g[MAX_N];
void dfs(int u) {
mark[u] = 1;
++cnt;
for (int i = (0); i < ((int)g[u].size()); ++i) {
int v = g[u][i];
if (!mark[v]) dfs(v);
}
}
int main() {
while (cin >> N >> M) {
for (int i = (1); i <= (N); ++i) g[i].clear();
memset(mark, 0, sizeof(mark));
for (int i = (1); i <= (M); ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
if (N == M) {
cnt = 0;
dfs(1);
cnt == N ? puts("FHTAGN!") : puts("NO");
} else
puts("NO");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
template <class T>
inline void checkmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
int mp[101][101], in[101], out[101], m, n;
bool vis[101];
bool sch(int k) {
vis[k] = 1;
for (int j = 0; j < n; j++)
if (mp[k][j] && !vis[j]) {
in[k]--;
in[j]--;
if (in[j] == 1)
return sch(j);
else if (in[j] == 0)
return false;
else
return true;
}
}
void sch2(int k) {
vis[k] = 1;
for (int i = 0; i < n; i++)
if (mp[k][i] && !vis[i]) sch2(i);
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
mp[u][v] = 1;
in[u]++;
swap(u, v);
mp[u][v] = 1;
in[u]++;
}
for (int i = 0; i < n; i++) {
if (in[i] == 0) {
printf("NO\n");
return 0;
}
}
for (int i = 0; i < n; i++) {
if (in[i] == 1) {
if (!sch(i)) {
printf("NO\n");
return 0;
}
}
}
int tmp = 0;
for (int i = 0; i < n; i++) {
if (!vis[i] && in[i] != 2) {
printf("NO\n");
return 0;
}
if (in[i] == 2) tmp++;
}
if (tmp < 3) {
printf("NO\n");
return 0;
}
for (int i = 0; i < n; i++) {
if (!vis[i]) {
sch2(i);
break;
}
}
for (int i = 0; i < n; i++) {
if (!vis[i]) {
printf("NO\n");
return 0;
}
}
printf("FHTAGN!\n");
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
template <class T>
inline void checkmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
int mp[101][101], in[101], out[101], m, n;
bool vis[101];
bool sch(int k) {
vis[k] = 1;
for (int j = 0; j < n; j++)
if (mp[k][j] && !vis[j]) {
in[k]--;
in[j]--;
if (in[j] == 1)
return sch(j);
else if (in[j] == 0)
return false;
else
return true;
}
}
void sch2(int k) {
vis[k] = 1;
for (int i = 0; i < n; i++)
if (mp[k][i] && !vis[i]) sch2(i);
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
mp[u][v] = 1;
in[u]++;
swap(u, v);
mp[u][v] = 1;
in[u]++;
}
for (int i = 0; i < n; i++) {
if (in[i] == 0) {
printf("NO\n");
return 0;
}
}
for (int i = 0; i < n; i++) {
if (in[i] == 1) {
if (!sch(i)) {
printf("NO\n");
return 0;
}
}
}
int tmp = 0;
for (int i = 0; i < n; i++) {
if (!vis[i] && in[i] != 2) {
printf("NO\n");
return 0;
}
if (in[i] == 2) tmp++;
}
if (tmp < 3) {
printf("NO\n");
return 0;
}
for (int i = 0; i < n; i++) {
if (!vis[i]) {
sch2(i);
break;
}
}
for (int i = 0; i < n; i++) {
if (!vis[i]) {
printf("NO\n");
return 0;
}
}
printf("FHTAGN!\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int visited[100000];
long long int len[100000];
long long int cycle = 0;
void dfs(long long int second, vector<long long int> v[]) {
visited[second] = 1;
long long int vp;
for (long long int i = 0; i < v[second].size(); i++) {
vp = v[second][i];
if (visited[vp] == 0) {
len[vp] = len[second] + 1;
dfs(vp, v);
} else if (visited[vp] == true && len[second] - len[vp] > 1) {
cycle++;
}
}
}
int main() {
long long int n, m, x, y;
cin >> n >> m;
vector<long long int> v[n + 1];
for (int i = 0; i < m; i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, v);
if (cycle != 1)
cout << "NO";
else if (cycle == 1) {
if (n == m) {
for (int i = 1; i < n; i++) {
if (visited[i] == false) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
} else
cout << "NO";
}
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int visited[100000];
long long int len[100000];
long long int cycle = 0;
void dfs(long long int second, vector<long long int> v[]) {
visited[second] = 1;
long long int vp;
for (long long int i = 0; i < v[second].size(); i++) {
vp = v[second][i];
if (visited[vp] == 0) {
len[vp] = len[second] + 1;
dfs(vp, v);
} else if (visited[vp] == true && len[second] - len[vp] > 1) {
cycle++;
}
}
}
int main() {
long long int n, m, x, y;
cin >> n >> m;
vector<long long int> v[n + 1];
for (int i = 0; i < m; i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, v);
if (cycle != 1)
cout << "NO";
else if (cycle == 1) {
if (n == m) {
for (int i = 1; i < n; i++) {
if (visited[i] == false) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
} else
cout << "NO";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> edges[5001];
int n, m;
bool vis1[110];
bool flag = false;
int ans = 0;
void dfs(int u, int parent) {
vis1[u] = true;
for (int i = 0; i < edges[u].size(); i++) {
int v = edges[u][i];
if (!vis1[v]) {
dfs(v, u);
} else if (v != parent) {
ans++;
}
}
return;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d %d", &x, &y);
edges[x].push_back(y);
edges[y].push_back(x);
}
if (n == m) {
memset(vis1, false, sizeof(vis1));
dfs(1, -1);
for (int i = 1; i <= n; i++) {
if (!vis1[i]) {
flag = true;
break;
}
}
if (!flag && ans == 2)
printf("FHTAGN!\n");
else
printf("NO\n");
} else
printf("NO\n");
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> edges[5001];
int n, m;
bool vis1[110];
bool flag = false;
int ans = 0;
void dfs(int u, int parent) {
vis1[u] = true;
for (int i = 0; i < edges[u].size(); i++) {
int v = edges[u][i];
if (!vis1[v]) {
dfs(v, u);
} else if (v != parent) {
ans++;
}
}
return;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d %d", &x, &y);
edges[x].push_back(y);
edges[y].push_back(x);
}
if (n == m) {
memset(vis1, false, sizeof(vis1));
dfs(1, -1);
for (int i = 1; i <= n; i++) {
if (!vis1[i]) {
flag = true;
break;
}
}
if (!flag && ans == 2)
printf("FHTAGN!\n");
else
printf("NO\n");
} else
printf("NO\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int inf = 1e9 + 1;
const double eps = 1e-15;
const double EPS = 1e-9;
const double PI = acos(-1.0);
vector<int> edge[200];
int vis[200];
int n, m, u, v;
void dfs(int id) {
vis[id] = 1;
for (int i = 0; i < edge[id].size(); i++) {
int x = edge[id][i];
if (!vis[x]) {
dfs(x);
}
}
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> u >> v;
edge[u].push_back(v);
edge[v].push_back(u);
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cnt++;
dfs(i);
}
}
(cnt > 1 || n != m) ? cout << "NO\n" : cout << "FHTAGN!\n";
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int inf = 1e9 + 1;
const double eps = 1e-15;
const double EPS = 1e-9;
const double PI = acos(-1.0);
vector<int> edge[200];
int vis[200];
int n, m, u, v;
void dfs(int id) {
vis[id] = 1;
for (int i = 0; i < edge[id].size(); i++) {
int x = edge[id][i];
if (!vis[x]) {
dfs(x);
}
}
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> u >> v;
edge[u].push_back(v);
edge[v].push_back(u);
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cnt++;
dfs(i);
}
}
(cnt > 1 || n != m) ? cout << "NO\n" : cout << "FHTAGN!\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool v[105], f[105][105];
int n, m, s = 0;
void dfs(int x) {
v[x] = 1;
s++;
for (int i = 1; i <= n; i++)
if (!v[i] && f[x][i]) dfs(i);
}
int main() {
scanf("%d%d", &n, &m);
if (n != m) {
puts("NO");
return 0;
}
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
f[a][b] = f[b][a] = true;
}
memset(v, 0, sizeof(v));
dfs(1);
if (s == n)
puts("FHTAGN!");
else
puts("NO");
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool v[105], f[105][105];
int n, m, s = 0;
void dfs(int x) {
v[x] = 1;
s++;
for (int i = 1; i <= n; i++)
if (!v[i] && f[x][i]) dfs(i);
}
int main() {
scanf("%d%d", &n, &m);
if (n != m) {
puts("NO");
return 0;
}
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
f[a][b] = f[b][a] = true;
}
memset(v, 0, sizeof(v));
dfs(1);
if (s == n)
puts("FHTAGN!");
else
puts("NO");
}
``` |
#include <bits/stdc++.h>
using namespace std;
int parent[1005];
bool visited[1005];
int f = 0;
int findParent(int node) {
if (parent[node] == node) return node;
return parent[node] = findParent(parent[node]);
}
set<int> Connected[1005];
void dfs(int node) {
if (visited[node]) return;
f++;
visited[node] = true;
for (set<int>::iterator it = Connected[node].begin();
it != Connected[node].end(); it++)
dfs(*it);
}
bool tree[1005];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) parent[i] = i;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
Connected[v].insert(u);
Connected[u].insert(v);
}
dfs(1);
if (f == n && n == m) {
cout << "FHTAGN!";
return 0;
}
cout << "NO";
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int parent[1005];
bool visited[1005];
int f = 0;
int findParent(int node) {
if (parent[node] == node) return node;
return parent[node] = findParent(parent[node]);
}
set<int> Connected[1005];
void dfs(int node) {
if (visited[node]) return;
f++;
visited[node] = true;
for (set<int>::iterator it = Connected[node].begin();
it != Connected[node].end(); it++)
dfs(*it);
}
bool tree[1005];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) parent[i] = i;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
Connected[v].insert(u);
Connected[u].insert(v);
}
dfs(1);
if (f == n && n == m) {
cout << "FHTAGN!";
return 0;
}
cout << "NO";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int v, e, a, b, x, y, cyc, found, head, chk[105];
vector<int> par, deg;
int findr(int a) {
if (par[a] != a) par[a] = findr(par[a]);
return par[a];
}
int main() {
scanf("%d %d", &v, &e);
for (int i = 0; i <= v; ++i) par.push_back(i);
deg.resize(v + 1);
while (e--) {
scanf("%d %d", &a, &b);
deg[a]++;
deg[b]++;
x = findr(a);
y = findr(b);
if (x == y)
cyc++;
else
par[y] = x;
}
for (int i = 1; i <= v; ++i) findr(i);
for (int i = 1; i <= v; ++i) {
if (chk[par[i]] == 0) {
chk[par[i]] = 1;
head++;
}
}
if (cyc == 1 && head == 1)
printf("FHTAGN!");
else
printf("NO");
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int v, e, a, b, x, y, cyc, found, head, chk[105];
vector<int> par, deg;
int findr(int a) {
if (par[a] != a) par[a] = findr(par[a]);
return par[a];
}
int main() {
scanf("%d %d", &v, &e);
for (int i = 0; i <= v; ++i) par.push_back(i);
deg.resize(v + 1);
while (e--) {
scanf("%d %d", &a, &b);
deg[a]++;
deg[b]++;
x = findr(a);
y = findr(b);
if (x == y)
cyc++;
else
par[y] = x;
}
for (int i = 1; i <= v; ++i) findr(i);
for (int i = 1; i <= v; ++i) {
if (chk[par[i]] == 0) {
chk[par[i]] = 1;
head++;
}
}
if (cyc == 1 && head == 1)
printf("FHTAGN!");
else
printf("NO");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, d[111], fa[111];
bool bo[111][111];
bool vis[111], flag, pd;
int getfa(int x) {
if (fa[x] == x)
return x;
else
return fa[x] = getfa(fa[x]);
}
void round() {
int i, j;
while (1) {
flag = true;
for (i = 1; i <= n; i++)
if (d[i] <= 1 && !vis[i]) {
flag = false;
vis[i] = true;
for (j = 1; j <= n; j++)
if (bo[i][j]) {
bo[i][j] = bo[j][i] = false;
d[i]--;
d[j]--;
}
}
if (flag) return;
}
}
int main() {
int i, j, k, t, m, T, x, y, ans, now, xx, yy;
while (scanf("%d%d", &n, &m) != EOF) {
for (i = 1; i <= n; i++) fa[i] = i;
memset(d, 0, sizeof(d));
memset(bo, 0, sizeof(bo));
memset(vis, 0, sizeof(vis));
for (i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
d[x]++;
d[y]++;
bo[x][y] = bo[y][x] = true;
xx = getfa(x);
yy = getfa(y);
if (xx != yy) fa[xx] = yy;
}
pd = true;
for (i = 2; i <= n; i++)
if (getfa(i) != getfa(i - 1)) pd = false;
round();
flag = false;
for (i = 1; i <= n; i++)
if (!vis[i]) {
flag = true;
break;
}
if (!flag) pd = false;
if (flag) {
for (j = 1; j <= n; j++)
if (bo[i][j]) {
bo[i][j] = bo[j][i] = false;
d[i]--;
d[j]--;
break;
}
round();
for (i = 1; i <= n; i++)
if (!vis[i]) flag = false;
}
if (flag && pd)
puts("FHTAGN!");
else
puts("NO");
}
}
| ### Prompt
Please create a solution in cpp to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, d[111], fa[111];
bool bo[111][111];
bool vis[111], flag, pd;
int getfa(int x) {
if (fa[x] == x)
return x;
else
return fa[x] = getfa(fa[x]);
}
void round() {
int i, j;
while (1) {
flag = true;
for (i = 1; i <= n; i++)
if (d[i] <= 1 && !vis[i]) {
flag = false;
vis[i] = true;
for (j = 1; j <= n; j++)
if (bo[i][j]) {
bo[i][j] = bo[j][i] = false;
d[i]--;
d[j]--;
}
}
if (flag) return;
}
}
int main() {
int i, j, k, t, m, T, x, y, ans, now, xx, yy;
while (scanf("%d%d", &n, &m) != EOF) {
for (i = 1; i <= n; i++) fa[i] = i;
memset(d, 0, sizeof(d));
memset(bo, 0, sizeof(bo));
memset(vis, 0, sizeof(vis));
for (i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
d[x]++;
d[y]++;
bo[x][y] = bo[y][x] = true;
xx = getfa(x);
yy = getfa(y);
if (xx != yy) fa[xx] = yy;
}
pd = true;
for (i = 2; i <= n; i++)
if (getfa(i) != getfa(i - 1)) pd = false;
round();
flag = false;
for (i = 1; i <= n; i++)
if (!vis[i]) {
flag = true;
break;
}
if (!flag) pd = false;
if (flag) {
for (j = 1; j <= n; j++)
if (bo[i][j]) {
bo[i][j] = bo[j][i] = false;
d[i]--;
d[j]--;
break;
}
round();
for (i = 1; i <= n; i++)
if (!vis[i]) flag = false;
}
if (flag && pd)
puts("FHTAGN!");
else
puts("NO");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> g[100000];
bool used[100000];
void dfs(int v) {
used[v] = true;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (used[to] == false) {
dfs(to);
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
g[l].push_back(r);
g[r].push_back(l);
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (used[i] == false) {
cout << "NO";
return 0;
}
}
if (n == m) {
cout << "FHTAGN!";
return 0;
}
cout << "NO";
}
| ### Prompt
Create a solution in CPP for the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> g[100000];
bool used[100000];
void dfs(int v) {
used[v] = true;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (used[to] == false) {
dfs(to);
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
g[l].push_back(r);
g[r].push_back(l);
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (used[i] == false) {
cout << "NO";
return 0;
}
}
if (n == m) {
cout << "FHTAGN!";
return 0;
}
cout << "NO";
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 105;
vector<long long int> gr[N];
long long int vis[N];
bool cycle, cycle2;
void dfs(long long int cur, long long int par) {
vis[cur] = 1;
bool fl = 0;
for (long long int x : gr[cur]) {
if (vis[x] == 0) dfs(x, cur);
if (vis[x] == 1 && x != par) {
if (cycle == 1) cycle2 = 1;
cycle = 1;
}
}
vis[cur] = 2;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
{
long long int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
cin >> n >> m;
for (i = 0; i < m; i++) {
long long int x, y;
cin >> x >> y;
gr[x].emplace_back(y);
gr[y].emplace_back(x);
}
dfs(1, 0);
for (i = 1; i <= n; i++) {
if (vis[i] == 0) cycle = 0;
}
(cycle && !cycle2) ? puts("FHTAGN!") : puts("NO");
}
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int N = 105;
vector<long long int> gr[N];
long long int vis[N];
bool cycle, cycle2;
void dfs(long long int cur, long long int par) {
vis[cur] = 1;
bool fl = 0;
for (long long int x : gr[cur]) {
if (vis[x] == 0) dfs(x, cur);
if (vis[x] == 1 && x != par) {
if (cycle == 1) cycle2 = 1;
cycle = 1;
}
}
vis[cur] = 2;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
{
long long int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
cin >> n >> m;
for (i = 0; i < m; i++) {
long long int x, y;
cin >> x >> y;
gr[x].emplace_back(y);
gr[y].emplace_back(x);
}
dfs(1, 0);
for (i = 1; i <= n; i++) {
if (vis[i] == 0) cycle = 0;
}
(cycle && !cycle2) ? puts("FHTAGN!") : puts("NO");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<long long> v[1005];
bool vis[1005];
void dfs(long long s) {
vis[s] = 1;
for (auto i : v[s]) {
if (vis[i]) continue;
dfs(i);
}
}
int main() {
long long n, m, flag = 1;
cin >> n >> m;
for (int i = 0; i < m; i++) {
long long a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1);
if (n != m) flag = 0;
for (int i = 1; i <= n; i++)
if (!vis[i]) flag = 0;
if (flag)
cout << "FHTAGN!";
else
cout << "NO";
}
| ### Prompt
Create a solution in cpp for the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<long long> v[1005];
bool vis[1005];
void dfs(long long s) {
vis[s] = 1;
for (auto i : v[s]) {
if (vis[i]) continue;
dfs(i);
}
}
int main() {
long long n, m, flag = 1;
cin >> n >> m;
for (int i = 0; i < m; i++) {
long long a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1);
if (n != m) flag = 0;
for (int i = 1; i <= n; i++)
if (!vis[i]) flag = 0;
if (flag)
cout << "FHTAGN!";
else
cout << "NO";
}
``` |
#include <bits/stdc++.h>
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (((a * b) / gcd(a, b))); }
long long phi(long long n) {
long long result = n;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0) n /= i;
result -= result / i;
}
}
if (n > 1) result -= result / n;
return result;
}
long long binpow(long long a, long long n, long long c) {
long long res = 1;
while (n) {
if (n & 1) res *= a;
res %= c;
a *= a;
a %= c;
n >>= 1;
}
res = (res + c) % c;
return res;
}
void nxi(long long& n) {
bool min = 0;
char c;
n = 0;
while ((c = getc(stdin)) && c <= 32)
;
if (c == '-')
min = 1;
else
n = c - 48;
while ((c = getc(stdin)) && c > 32) n = (n << 3) + (n << 1) + c - 48;
if (min) n = -n;
}
void prl(long long n) {
if (n == 0) {
puts("0");
return;
}
if (n < 0) {
putchar('-');
n = -n;
}
static long long s[10];
long long top = 0;
while (n > 0) s[top++] = n % 10, n /= 10;
while (top--) putchar(s[top] + 48);
puts("");
}
using namespace std;
const int nmax = 1100;
long long n, a[nmax][nmax], m;
vector<int> g[nmax];
int cycle = -1, par[nmax], color[nmax];
bool ban[nmax];
int cnt = 0;
void outv(vector<int> v) {
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}
void out() {
puts("NO");
exit(0);
}
void dfs(int v, int p = -1) {
par[v] = p;
color[v] = 1;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (color[to] == 1 && to != p)
cycle = v, cnt++;
else if (color[to] == 0)
dfs(to, v);
}
color[v] = 2;
}
bool check(vector<int> cc) {
cc.erase(cc.begin());
cc.erase(cc.begin() + cc.size() - 1);
for (int i = 0; i < cc.size(); i++)
for (int j = i + 2; j < cc.size(); j++) {
int v = cc[i], to = cc[j];
if (a[v][to]) return false;
}
return 1;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
a[x][y] = a[y][x] = 1;
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (color[i] == 0) out();
}
if (cycle == -1 || cnt > 1) out();
vector<int> cc;
while (cycle > 0) {
cc.push_back(cycle);
ban[cycle] = 1;
cycle = par[cycle];
}
int cntV = cc.size(), edges = cntV;
if (n - cntV + 1 == m - edges + 1 && n - cntV >= 3)
puts("FHTAGN!");
else if (check(cc))
puts("FHTAGN!");
else
out();
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (((a * b) / gcd(a, b))); }
long long phi(long long n) {
long long result = n;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0) n /= i;
result -= result / i;
}
}
if (n > 1) result -= result / n;
return result;
}
long long binpow(long long a, long long n, long long c) {
long long res = 1;
while (n) {
if (n & 1) res *= a;
res %= c;
a *= a;
a %= c;
n >>= 1;
}
res = (res + c) % c;
return res;
}
void nxi(long long& n) {
bool min = 0;
char c;
n = 0;
while ((c = getc(stdin)) && c <= 32)
;
if (c == '-')
min = 1;
else
n = c - 48;
while ((c = getc(stdin)) && c > 32) n = (n << 3) + (n << 1) + c - 48;
if (min) n = -n;
}
void prl(long long n) {
if (n == 0) {
puts("0");
return;
}
if (n < 0) {
putchar('-');
n = -n;
}
static long long s[10];
long long top = 0;
while (n > 0) s[top++] = n % 10, n /= 10;
while (top--) putchar(s[top] + 48);
puts("");
}
using namespace std;
const int nmax = 1100;
long long n, a[nmax][nmax], m;
vector<int> g[nmax];
int cycle = -1, par[nmax], color[nmax];
bool ban[nmax];
int cnt = 0;
void outv(vector<int> v) {
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}
void out() {
puts("NO");
exit(0);
}
void dfs(int v, int p = -1) {
par[v] = p;
color[v] = 1;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (color[to] == 1 && to != p)
cycle = v, cnt++;
else if (color[to] == 0)
dfs(to, v);
}
color[v] = 2;
}
bool check(vector<int> cc) {
cc.erase(cc.begin());
cc.erase(cc.begin() + cc.size() - 1);
for (int i = 0; i < cc.size(); i++)
for (int j = i + 2; j < cc.size(); j++) {
int v = cc[i], to = cc[j];
if (a[v][to]) return false;
}
return 1;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
a[x][y] = a[y][x] = 1;
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (color[i] == 0) out();
}
if (cycle == -1 || cnt > 1) out();
vector<int> cc;
while (cycle > 0) {
cc.push_back(cycle);
ban[cycle] = 1;
cycle = par[cycle];
}
int cntV = cc.size(), edges = cntV;
if (n - cntV + 1 == m - edges + 1 && n - cntV >= 3)
puts("FHTAGN!");
else if (check(cc))
puts("FHTAGN!");
else
out();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
int t = 1;
while (t--) solve();
return 0;
}
int n, m;
vector<int> e[200];
bool be[200];
void dfs1(int v, int beg = -1, int end = -1) {
be[v] = true;
for (int i = 0; i < e[v].size(); i++)
if ((v != beg || e[v][i] != end) && (v != end || e[v][i] != beg))
if (!be[e[v][i]]) dfs1(e[v][i], beg, end);
}
vector<pair<int, int> > nemost;
void solve() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int q, w;
cin >> q >> w;
e[q - 1].push_back(w - 1);
e[w - 1].push_back(q - 1);
}
dfs1(0);
for (int i = 0; i < n; i++)
if (!be[i]) {
cout << "NO";
return;
}
memset(be, false, sizeof(be));
for (int i = 0; i < n; i++)
for (int j = 0; j < e[i].size(); j++)
if (i < e[i][j]) {
bool flag = true;
dfs1(0, i, e[i][j]);
for (int k = 0; k < n; k++)
if (!be[k]) {
flag = false;
break;
}
if (flag) nemost.push_back(make_pair(i, e[i][j]));
memset(be, false, sizeof(be));
}
int r = nemost.size();
int step[200];
memset(step, 0, sizeof(step));
for (int i = 0; i < r; i++) {
step[nemost[i].first]++;
step[nemost[i].second]++;
}
if (nemost.size() < 3) {
cout << "NO";
return;
}
for (int i = 0; i < n; i++)
if (step[i] != 0 && step[i] != 2) {
cout << "NO";
return;
}
cout << "FHTAGN!";
}
| ### Prompt
In cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
int t = 1;
while (t--) solve();
return 0;
}
int n, m;
vector<int> e[200];
bool be[200];
void dfs1(int v, int beg = -1, int end = -1) {
be[v] = true;
for (int i = 0; i < e[v].size(); i++)
if ((v != beg || e[v][i] != end) && (v != end || e[v][i] != beg))
if (!be[e[v][i]]) dfs1(e[v][i], beg, end);
}
vector<pair<int, int> > nemost;
void solve() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int q, w;
cin >> q >> w;
e[q - 1].push_back(w - 1);
e[w - 1].push_back(q - 1);
}
dfs1(0);
for (int i = 0; i < n; i++)
if (!be[i]) {
cout << "NO";
return;
}
memset(be, false, sizeof(be));
for (int i = 0; i < n; i++)
for (int j = 0; j < e[i].size(); j++)
if (i < e[i][j]) {
bool flag = true;
dfs1(0, i, e[i][j]);
for (int k = 0; k < n; k++)
if (!be[k]) {
flag = false;
break;
}
if (flag) nemost.push_back(make_pair(i, e[i][j]));
memset(be, false, sizeof(be));
}
int r = nemost.size();
int step[200];
memset(step, 0, sizeof(step));
for (int i = 0; i < r; i++) {
step[nemost[i].first]++;
step[nemost[i].second]++;
}
if (nemost.size() < 3) {
cout << "NO";
return;
}
for (int i = 0; i < n; i++)
if (step[i] != 0 && step[i] != 2) {
cout << "NO";
return;
}
cout << "FHTAGN!";
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool visited[110];
bool a[110][110];
int color[110];
vector<int> v;
int n;
int cycle;
void dfs(int u) {
visited[u] = true;
color[u] = 1;
for (int(i) = 0; (i) < (n); (i)++)
if (a[u][i + 1]) {
a[u][i + 1] = a[i + 1][u] = false;
if (color[i + 1] == 1) {
cycle++;
for (int(j) = 0; (j) < (n); (j)++)
if (color[j + 1] == 1) v.push_back(j + 1);
}
if (!visited[i + 1]) dfs(i + 1);
}
color[u] = 2;
}
bool check() {
v.clear();
cycle = 0;
dfs(1);
if (cycle != 1) return false;
for (int(i) = 0; (i) < (n); (i)++)
if (!visited[i + 1]) return false;
if (v.size() < 3) return false;
return true;
}
int main() {
for (int(i) = 0; (i) < (110); (i)++)
for (int(j) = 0; (j) < (110); (j)++)
a[i][j] = visited[i] = color[i] = false;
int m;
cin >> n >> m;
for (int(i) = 0; (i) < (m); (i)++) {
int u, v;
cin >> u >> v;
a[u][v] = a[v][u] = true;
}
if (check())
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool visited[110];
bool a[110][110];
int color[110];
vector<int> v;
int n;
int cycle;
void dfs(int u) {
visited[u] = true;
color[u] = 1;
for (int(i) = 0; (i) < (n); (i)++)
if (a[u][i + 1]) {
a[u][i + 1] = a[i + 1][u] = false;
if (color[i + 1] == 1) {
cycle++;
for (int(j) = 0; (j) < (n); (j)++)
if (color[j + 1] == 1) v.push_back(j + 1);
}
if (!visited[i + 1]) dfs(i + 1);
}
color[u] = 2;
}
bool check() {
v.clear();
cycle = 0;
dfs(1);
if (cycle != 1) return false;
for (int(i) = 0; (i) < (n); (i)++)
if (!visited[i + 1]) return false;
if (v.size() < 3) return false;
return true;
}
int main() {
for (int(i) = 0; (i) < (110); (i)++)
for (int(j) = 0; (j) < (110); (j)++)
a[i][j] = visited[i] = color[i] = false;
int m;
cin >> n >> m;
for (int(i) = 0; (i) < (m); (i)++) {
int u, v;
cin >> u >> v;
a[u][v] = a[v][u] = true;
}
if (check())
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
}
``` |
#include <bits/stdc++.h>
int g[110][110];
int v[110];
void dfs(int a, int n) {
v[a] = 1;
for (int i = 1; i <= n; i++) {
if (g[a][i] && !v[i]) dfs(i, n);
}
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = 0;
memset(v, 0, sizeof(v));
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d %d", &a, &b);
g[a][b] = 1;
g[b][a] = 1;
}
if (n != m) {
printf("NO\n");
return 0;
}
dfs(1, n);
for (int i = 1; i <= n; i++) {
if (v[i] == 0) {
printf("NO\n");
return 0;
}
}
printf("FHTAGN!\n");
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
int g[110][110];
int v[110];
void dfs(int a, int n) {
v[a] = 1;
for (int i = 1; i <= n; i++) {
if (g[a][i] && !v[i]) dfs(i, n);
}
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = 0;
memset(v, 0, sizeof(v));
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d %d", &a, &b);
g[a][b] = 1;
g[b][a] = 1;
}
if (n != m) {
printf("NO\n");
return 0;
}
dfs(1, n);
for (int i = 1; i <= n; i++) {
if (v[i] == 0) {
printf("NO\n");
return 0;
}
}
printf("FHTAGN!\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > G;
vector<int> was;
int cycle;
void pdfs(int node, int par) {
was[node]++;
for (int i = 0; i < G[node].size(); i++) {
int ch = G[node][i];
if (was[ch] && ch != par) cycle++;
if (!was[ch]) pdfs(ch, node);
}
}
int main() {
int N, M;
cin >> N >> M;
G.resize(N + 2);
was.resize(N + 2);
for (int i = 0; i < M; i++) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
pdfs(1, -1);
bool con = true;
for (int i = 1; i <= N; i++)
if (!was[i]) con = false;
if (cycle == 2 && con)
puts("FHTAGN!\n");
else
puts("NO\n");
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > G;
vector<int> was;
int cycle;
void pdfs(int node, int par) {
was[node]++;
for (int i = 0; i < G[node].size(); i++) {
int ch = G[node][i];
if (was[ch] && ch != par) cycle++;
if (!was[ch]) pdfs(ch, node);
}
}
int main() {
int N, M;
cin >> N >> M;
G.resize(N + 2);
was.resize(N + 2);
for (int i = 0; i < M; i++) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
pdfs(1, -1);
bool con = true;
for (int i = 1; i <= N; i++)
if (!was[i]) con = false;
if (cycle == 2 && con)
puts("FHTAGN!\n");
else
puts("NO\n");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > ar(110, vector<int>(110, 0));
vector<vector<int> > te(110, vector<int>(110, 0));
int ar1[110];
int flag[110];
int n1;
bool f;
void dfs(int n) {
int i;
ar1[n] = 1;
flag[n] = 1;
for (i = 1; i <= n1; i++) {
if (ar[n][i] == 1 && flag[i] == 1) {
f = false;
} else if (ar[n][i] == 1) {
ar[i][n] = 0;
dfs(i);
}
}
flag[n] = 0;
}
int main() {
int n, m, cnt = 0;
vector<pair<int, int> > v;
cin >> n >> m;
n1 = n;
int i, j, a, b;
for (i = 0; i < m; i++) {
cin >> a >> b;
v.push_back(make_pair(a, b));
ar[a][b] = 1;
ar[b][a] = 1;
}
te = ar;
for (i = 0; i < m; i++) {
cnt = 0;
memset(ar1, 0, sizeof(ar1));
memset(flag, 0, sizeof(flag));
ar = te;
f = true;
a = v[i].first;
b = v[i].second;
ar[a][b] = ar[b][a] = 0;
dfs(1);
if (f == false)
continue;
else {
for (j = 1; j <= n; j++)
if (ar1[j] == 1) cnt++;
if (cnt == n) {
cout << "FHTAGN!"
<< "\n";
return 0;
}
}
}
cout << "NO"
<< "\n";
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > ar(110, vector<int>(110, 0));
vector<vector<int> > te(110, vector<int>(110, 0));
int ar1[110];
int flag[110];
int n1;
bool f;
void dfs(int n) {
int i;
ar1[n] = 1;
flag[n] = 1;
for (i = 1; i <= n1; i++) {
if (ar[n][i] == 1 && flag[i] == 1) {
f = false;
} else if (ar[n][i] == 1) {
ar[i][n] = 0;
dfs(i);
}
}
flag[n] = 0;
}
int main() {
int n, m, cnt = 0;
vector<pair<int, int> > v;
cin >> n >> m;
n1 = n;
int i, j, a, b;
for (i = 0; i < m; i++) {
cin >> a >> b;
v.push_back(make_pair(a, b));
ar[a][b] = 1;
ar[b][a] = 1;
}
te = ar;
for (i = 0; i < m; i++) {
cnt = 0;
memset(ar1, 0, sizeof(ar1));
memset(flag, 0, sizeof(flag));
ar = te;
f = true;
a = v[i].first;
b = v[i].second;
ar[a][b] = ar[b][a] = 0;
dfs(1);
if (f == false)
continue;
else {
for (j = 1; j <= n; j++)
if (ar1[j] == 1) cnt++;
if (cnt == n) {
cout << "FHTAGN!"
<< "\n";
return 0;
}
}
}
cout << "NO"
<< "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> adjList[110];
int state[110];
int parent[110];
int qtyCycles;
void dfs(int u) {
state[u] = 0;
for (int v : adjList[u]) {
if (state[v] == -1) {
parent[v] = u;
dfs(v);
} else if (state[v] == 0 && v != parent[u]) {
qtyCycles++;
}
}
state[u] = 1;
}
int main() {
int V, E;
cin >> V >> E;
memset(state, -1, sizeof state);
memset(parent, -1, sizeof parent);
qtyCycles = 0;
while (E--) {
int u, v;
cin >> u >> v;
u--;
v--;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
int qtyCCs = 0;
for (int u = 0; u < V; u++)
if (state[u] == -1) {
dfs(u);
qtyCCs++;
}
cout << (qtyCycles == 1 && qtyCCs == 1 ? "FHTAGN!" : "NO") << endl;
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> adjList[110];
int state[110];
int parent[110];
int qtyCycles;
void dfs(int u) {
state[u] = 0;
for (int v : adjList[u]) {
if (state[v] == -1) {
parent[v] = u;
dfs(v);
} else if (state[v] == 0 && v != parent[u]) {
qtyCycles++;
}
}
state[u] = 1;
}
int main() {
int V, E;
cin >> V >> E;
memset(state, -1, sizeof state);
memset(parent, -1, sizeof parent);
qtyCycles = 0;
while (E--) {
int u, v;
cin >> u >> v;
u--;
v--;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
int qtyCCs = 0;
for (int u = 0; u < V; u++)
if (state[u] == -1) {
dfs(u);
qtyCCs++;
}
cout << (qtyCycles == 1 && qtyCCs == 1 ? "FHTAGN!" : "NO") << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int vis[101];
vector<vector<int>> adj;
void dfs(int s) {
vis[s] = 1;
for (int i = 0; i < adj[s].size(); i++)
if (!vis[adj[s][i]]) dfs(adj[s][i]);
}
int main() {
int n, m;
cin >> n >> m;
if (m != n) {
cout << "NO\n";
return 0;
}
adj.resize(n + 1);
int f, t;
for (int i = 0; i < m; i++) {
cin >> f >> t;
adj[f].push_back(t);
adj[t].push_back(f);
}
bool flag = true;
vector<int> res1;
memset(vis, 0, sizeof(vis));
dfs(1);
for (int j = 1; j <= n; j++)
if (!vis[j]) {
flag = false;
break;
}
if (flag)
cout << "FHTAGN!\n";
else
cout << "NO\n";
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int vis[101];
vector<vector<int>> adj;
void dfs(int s) {
vis[s] = 1;
for (int i = 0; i < adj[s].size(); i++)
if (!vis[adj[s][i]]) dfs(adj[s][i]);
}
int main() {
int n, m;
cin >> n >> m;
if (m != n) {
cout << "NO\n";
return 0;
}
adj.resize(n + 1);
int f, t;
for (int i = 0; i < m; i++) {
cin >> f >> t;
adj[f].push_back(t);
adj[t].push_back(f);
}
bool flag = true;
vector<int> res1;
memset(vis, 0, sizeof(vis));
dfs(1);
for (int j = 1; j <= n; j++)
if (!vis[j]) {
flag = false;
break;
}
if (flag)
cout << "FHTAGN!\n";
else
cout << "NO\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 101;
int n, m, c, par[N];
int get_par(int u) { return u == par[u] ? u : par[u] = get_par(par[u]); }
bool uni(int u, int v) {
u = get_par(u);
v = get_par(v);
if (u == v) return 0;
par[u] = v;
c--;
return 1;
}
int main() {
iota(par, par + N, 0);
cin >> n >> m;
bool ok = 0;
c = n;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
if (!uni(u, v)) ok = 1;
}
ok &= (c == 1);
ok &= (n == m);
cout << (ok ? "FHTAGN!" : "NO");
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 101;
int n, m, c, par[N];
int get_par(int u) { return u == par[u] ? u : par[u] = get_par(par[u]); }
bool uni(int u, int v) {
u = get_par(u);
v = get_par(v);
if (u == v) return 0;
par[u] = v;
c--;
return 1;
}
int main() {
iota(par, par + N, 0);
cin >> n >> m;
bool ok = 0;
c = n;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
if (!uni(u, v)) ok = 1;
}
ok &= (c == 1);
ok &= (n == m);
cout << (ok ? "FHTAGN!" : "NO");
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, x, y, s;
vector<int> a[105];
bool c[105];
void dfs(int i) {
c[i] = true;
s++;
for (int j = 0; j < a[i].size(); j++) {
if (!c[a[i][j]]) dfs(a[i][j]);
}
}
int main() {
cin >> n >> m;
for (i = 0; i < m; i++) {
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
if (n != m) {
cout << "NO";
return 0;
} else {
dfs(1);
if (s == n) {
cout << "FHTAGN!";
return 0;
} else {
cout << "NO";
return 0;
}
}
}
| ### Prompt
Create a solution in Cpp for the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, i, x, y, s;
vector<int> a[105];
bool c[105];
void dfs(int i) {
c[i] = true;
s++;
for (int j = 0; j < a[i].size(); j++) {
if (!c[a[i][j]]) dfs(a[i][j]);
}
}
int main() {
cin >> n >> m;
for (i = 0; i < m; i++) {
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
if (n != m) {
cout << "NO";
return 0;
} else {
dfs(1);
if (s == n) {
cout << "FHTAGN!";
return 0;
} else {
cout << "NO";
return 0;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int size[205];
int arr[205];
int root(int i) {
while (i != arr[i]) {
arr[i] = arr[arr[i]];
i = arr[i];
}
return i;
}
void unionn(int A, int B) {
int root_A = root(A);
int root_B = root(B);
if (size[root_A] < size[root_B]) {
arr[root_A] = arr[root_B];
size[root_B] += size[root_A];
} else {
arr[root_B] = arr[root_A];
size[root_A] += size[root_B];
}
}
int find(int a, int b) {
if (root(a) == root(b))
return 1;
else
return 0;
}
void initialize() {
for (int i = 0; i < 205; i++) {
arr[i] = i;
size[i] = 1;
}
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n;
cin >> n;
int m;
cin >> m;
int x, y;
initialize();
int cycle = 0;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
if (find(x, y)) cycle++;
unionn(x, y);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (root(i) == i) ans++;
}
if (ans > 1 || cycle != 1) {
cout << "NO" << endl;
return 0;
}
cout << "FHTAGN!" << endl;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int size[205];
int arr[205];
int root(int i) {
while (i != arr[i]) {
arr[i] = arr[arr[i]];
i = arr[i];
}
return i;
}
void unionn(int A, int B) {
int root_A = root(A);
int root_B = root(B);
if (size[root_A] < size[root_B]) {
arr[root_A] = arr[root_B];
size[root_B] += size[root_A];
} else {
arr[root_B] = arr[root_A];
size[root_A] += size[root_B];
}
}
int find(int a, int b) {
if (root(a) == root(b))
return 1;
else
return 0;
}
void initialize() {
for (int i = 0; i < 205; i++) {
arr[i] = i;
size[i] = 1;
}
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n;
cin >> n;
int m;
cin >> m;
int x, y;
initialize();
int cycle = 0;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
if (find(x, y)) cycle++;
unionn(x, y);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (root(i) == i) ans++;
}
if (ans > 1 || cycle != 1) {
cout << "NO" << endl;
return 0;
}
cout << "FHTAGN!" << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int p[105], s[105];
int fnd(int a) { return a == p[a] ? a : p[a] = fnd(p[a]); }
void unn(int a, int b) {
a = fnd(a);
b = fnd(b);
if (s[a] < s[b]) swap(a, b);
s[a] += s[b];
p[b] = a;
}
int main() {
int n, m, u, v;
scanf("%d %d", &n, &m);
iota(p, p + n + 1, 0);
fill(s, s + n, 1);
int cc = 0;
for (int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
if (fnd(u) != fnd(v))
unn(u, v);
else
++cc;
}
if (cc != 1)
puts("NO");
else {
bool c = 0;
for (int i = 1; i <= n; ++i) {
if (p[i] == i)
if (c)
return cout << "NO", 0;
else
c = 1;
}
puts("FHTAGN!");
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int p[105], s[105];
int fnd(int a) { return a == p[a] ? a : p[a] = fnd(p[a]); }
void unn(int a, int b) {
a = fnd(a);
b = fnd(b);
if (s[a] < s[b]) swap(a, b);
s[a] += s[b];
p[b] = a;
}
int main() {
int n, m, u, v;
scanf("%d %d", &n, &m);
iota(p, p + n + 1, 0);
fill(s, s + n, 1);
int cc = 0;
for (int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
if (fnd(u) != fnd(v))
unn(u, v);
else
++cc;
}
if (cc != 1)
puts("NO");
else {
bool c = 0;
for (int i = 1; i <= n; ++i) {
if (p[i] == i)
if (c)
return cout << "NO", 0;
else
c = 1;
}
puts("FHTAGN!");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int Max = 110;
vector<int> adj[Max];
void init(int n) {
int i;
for (i = 1; i <= n; i++) adj[i].clear();
}
int vis[Max];
int ans;
int s;
void dfs(int i, int lev) {
vis[i] = 1;
int j, len = adj[i].size();
for (j = 0; j < len; j++) {
int v = adj[i][j];
if (v == s) {
if (ans < lev) ans = lev;
}
if (vis[v] == 0) {
dfs(v, lev + 1);
}
}
}
int solve(int n) {
int i;
ans = 0;
s = 1;
memset(vis, 0, sizeof(vis));
dfs(1, 1);
for (i = 1; i <= n; i++) {
if (vis[i] == 0) return 0;
}
if (ans >= 3) return 1;
for (i = 2; i <= n; i++) {
memset(vis, 0, sizeof(vis));
s = i;
dfs(i, 1);
if (ans >= 3) return 1;
}
return 0;
}
int main() {
int n, m;
int go;
while (scanf("%d %d", &n, &m) != EOF) {
init(n);
int a, b;
int tm = m;
while (tm--) {
scanf("%d %d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
if (m != n) {
printf("NO\n");
continue;
}
go = solve(n);
if (go)
printf("FHTAGN!\n");
else
printf("NO\n");
}
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int Max = 110;
vector<int> adj[Max];
void init(int n) {
int i;
for (i = 1; i <= n; i++) adj[i].clear();
}
int vis[Max];
int ans;
int s;
void dfs(int i, int lev) {
vis[i] = 1;
int j, len = adj[i].size();
for (j = 0; j < len; j++) {
int v = adj[i][j];
if (v == s) {
if (ans < lev) ans = lev;
}
if (vis[v] == 0) {
dfs(v, lev + 1);
}
}
}
int solve(int n) {
int i;
ans = 0;
s = 1;
memset(vis, 0, sizeof(vis));
dfs(1, 1);
for (i = 1; i <= n; i++) {
if (vis[i] == 0) return 0;
}
if (ans >= 3) return 1;
for (i = 2; i <= n; i++) {
memset(vis, 0, sizeof(vis));
s = i;
dfs(i, 1);
if (ans >= 3) return 1;
}
return 0;
}
int main() {
int n, m;
int go;
while (scanf("%d %d", &n, &m) != EOF) {
init(n);
int a, b;
int tm = m;
while (tm--) {
scanf("%d %d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
if (m != n) {
printf("NO\n");
continue;
}
go = solve(n);
if (go)
printf("FHTAGN!\n");
else
printf("NO\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int g[102][102];
bool used[102];
int n, m, a, b;
bool cycle;
int dfs(int cur, int par = -1) {
used[cur] = true;
int ret = 1;
for (int i = 0; i < n; i++)
if (g[cur][i] && i != par) {
if (!used[i])
ret += dfs(i, cur);
else {
a = cur;
b = i;
cycle = true;
}
}
return ret;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b;
a--;
b--;
g[a][b] = g[b][a] = 1;
}
int cnt = dfs(0);
if (cnt != n || !cycle)
cout << "NO\n";
else {
memset(used, false, sizeof(used));
cycle = false;
g[a][b] = g[b][a] = 0;
dfs(0);
if (cycle)
cout << "NO\n";
else
cout << "FHTAGN!\n";
}
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int g[102][102];
bool used[102];
int n, m, a, b;
bool cycle;
int dfs(int cur, int par = -1) {
used[cur] = true;
int ret = 1;
for (int i = 0; i < n; i++)
if (g[cur][i] && i != par) {
if (!used[i])
ret += dfs(i, cur);
else {
a = cur;
b = i;
cycle = true;
}
}
return ret;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b;
a--;
b--;
g[a][b] = g[b][a] = 1;
}
int cnt = dfs(0);
if (cnt != n || !cycle)
cout << "NO\n";
else {
memset(used, false, sizeof(used));
cycle = false;
g[a][b] = g[b][a] = 0;
dfs(0);
if (cycle)
cout << "NO\n";
else
cout << "FHTAGN!\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
const int MaxSize = 1e2 + 5;
using namespace std;
int n, m;
bool Edge[MaxSize][MaxSize];
bool Node[MaxSize];
void DFS(int cur) {
if (Node[cur]) return;
Node[cur] = 1;
for (long long i = 1; i < n + 1; i++)
if (Edge[i][cur]) DFS(i);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
scanf("%d%d", &n, &m);
for (long long i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
Edge[x][y] = Edge[y][x] = 1;
}
DFS(1);
bool Connect = 1;
for (long long j = 2; j < n + 1; j++) Connect &= Node[j];
printf("%s", (Connect & m == n) ? "FHTAGN!" : "NO");
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
const int MaxSize = 1e2 + 5;
using namespace std;
int n, m;
bool Edge[MaxSize][MaxSize];
bool Node[MaxSize];
void DFS(int cur) {
if (Node[cur]) return;
Node[cur] = 1;
for (long long i = 1; i < n + 1; i++)
if (Edge[i][cur]) DFS(i);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
scanf("%d%d", &n, &m);
for (long long i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
Edge[x][y] = Edge[y][x] = 1;
}
DFS(1);
bool Connect = 1;
for (long long j = 2; j < n + 1; j++) Connect &= Node[j];
printf("%s", (Connect & m == n) ? "FHTAGN!" : "NO");
}
``` |
#include <bits/stdc++.h>
using namespace std;
list<int> adj[101];
int m, n, x, y, cnt;
bool visited[101];
void dfs(int u) {
visited[u] = true;
++cnt;
for (list<int>::iterator it = adj[u].begin(); it != adj[u].end(); ++it)
if (!visited[*it]) dfs(*it);
}
int main() {
scanf("%d %d\n", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d %d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
if (n != m)
puts("NO");
else {
dfs(1);
if (cnt != n)
puts("NO");
else
puts("FHTAGN!");
}
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
list<int> adj[101];
int m, n, x, y, cnt;
bool visited[101];
void dfs(int u) {
visited[u] = true;
++cnt;
for (list<int>::iterator it = adj[u].begin(); it != adj[u].end(); ++it)
if (!visited[*it]) dfs(*it);
}
int main() {
scanf("%d %d\n", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d %d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
if (n != m)
puts("NO");
else {
dfs(1);
if (cnt != n)
puts("NO");
else
puts("FHTAGN!");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool was[101];
vector<int> G[101];
void dfs(int u) {
was[u] = true;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!was[v]) dfs(v);
}
}
int main() {
int n, m;
cin >> n >> m;
bool res = true;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(n);
for (int i = 1; i <= n; i++)
if (!was[i]) res = false;
if (res && n == m)
cout << "FHTAGN!";
else
cout << "NO";
return (0);
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool was[101];
vector<int> G[101];
void dfs(int u) {
was[u] = true;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!was[v]) dfs(v);
}
}
int main() {
int n, m;
cin >> n >> m;
bool res = true;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(n);
for (int i = 1; i <= n; i++)
if (!was[i]) res = false;
if (res && n == m)
cout << "FHTAGN!";
else
cout << "NO";
return (0);
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > arr(120);
bool vis[120] = {false};
void dfs(int node) {
if (vis[node]) {
return;
}
vis[node] = true;
for (int i = 0; i < arr[node].size(); i++) {
dfs(arr[node][i]);
}
return;
}
int main() {
int n, m;
cin >> n >> m;
if (n != m) {
cout << "NO";
return 0;
}
for (int i = 0; i < m; i++) {
int from, to;
cin >> from >> to;
arr[from].push_back(to);
arr[to].push_back(from);
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > arr(120);
bool vis[120] = {false};
void dfs(int node) {
if (vis[node]) {
return;
}
vis[node] = true;
for (int i = 0; i < arr[node].size(); i++) {
dfs(arr[node][i]);
}
return;
}
int main() {
int n, m;
cin >> n >> m;
if (n != m) {
cout << "NO";
return 0;
}
for (int i = 0; i < m; i++) {
int from, to;
cin >> from >> to;
arr[from].push_back(to);
arr[to].push_back(from);
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, vis[104];
vector<int> adj[104];
void dfs(int s) {
vis[s] = 1;
for (int v : adj[s]) {
if (!vis[v]) {
dfs(v);
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
int f = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) f |= 1;
}
if (!f && n == m) {
puts("FHTAGN!");
} else {
puts("NO");
}
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, vis[104];
vector<int> adj[104];
void dfs(int s) {
vis[s] = 1;
for (int v : adj[s]) {
if (!vis[v]) {
dfs(v);
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
int f = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) f |= 1;
}
if (!f && n == m) {
puts("FHTAGN!");
} else {
puts("NO");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt = 0;
int dat[105][105];
bool vis[105];
void dfs(int x) {
vis[x] = true;
cnt++;
for (int i = 1; i <= n; i++)
if (!vis[i] && dat[x][i] == 1) dfs(i);
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
dat[x][y] = 1;
dat[y][x] = 1;
}
dfs(1);
if (cnt == n && m == n)
cout << "FHTAGN!"
<< "\n";
else
cout << "NO\n";
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt = 0;
int dat[105][105];
bool vis[105];
void dfs(int x) {
vis[x] = true;
cnt++;
for (int i = 1; i <= n; i++)
if (!vis[i] && dat[x][i] == 1) dfs(i);
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
dat[x][y] = 1;
dat[y][x] = 1;
}
dfs(1);
if (cnt == n && m == n)
cout << "FHTAGN!"
<< "\n";
else
cout << "NO\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m;
int fa[N];
bool ans, ansf = true;
int find(int x) {
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
void unionn(int x, int y) { fa[y] = x; }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) fa[i] = i;
for (int i = 1; i <= m; ++i) {
int x, y;
cin >> x >> y;
if (x > y) swap(x, y);
int fx = find(x);
int fy = find(y);
if (fx == fy)
ans = true;
else
unionn(fx, fy);
}
int x = find(1);
for (int i = 2; i <= n; ++i)
if (find(i) != x) {
ansf = false;
break;
}
if (ans && ansf && n == m)
cout << "FHTAGN!";
else
cout << "NO";
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m;
int fa[N];
bool ans, ansf = true;
int find(int x) {
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
void unionn(int x, int y) { fa[y] = x; }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) fa[i] = i;
for (int i = 1; i <= m; ++i) {
int x, y;
cin >> x >> y;
if (x > y) swap(x, y);
int fx = find(x);
int fy = find(y);
if (fx == fy)
ans = true;
else
unionn(fx, fy);
}
int x = find(1);
for (int i = 2; i <= n; ++i)
if (find(i) != x) {
ansf = false;
break;
}
if (ans && ansf && n == m)
cout << "FHTAGN!";
else
cout << "NO";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const char letters[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
const int dx[] = {0, -1, 0, 1, -1, -1, 1, 1};
const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
long long inline max3(long long a, long long b, long long c) {
return max(a, max(b, c));
}
long long inline min3(long long a, long long b, long long c) {
return min(a, min(b, c));
}
long long power(long long x, long long p) {
if (p == 0)
return 1;
else if (p % 2 == 0)
return power(1LL * x * x, p / 2);
else
return 1LL * x * power(1LL * x * x, (p - 1) / 2);
}
long long GCD(long long a, long long b) {
if (b == 0) return a;
return GCD(b, a % b);
}
const int N = 150;
int n, m;
vector<vector<int> > G(N);
bool vis[N];
void dfs(int u, int p) {
vis[u] = 1;
for (int i = 0; i < ((int)G[u].size()); ++i) {
if (!vis[G[u][i]]) dfs(G[u][i], u);
}
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 0, u, v; i < m; ++i) {
scanf("%d", &u);
scanf("%d", &v);
G[u].push_back(v), G[v].push_back(u);
}
int cnt = 0;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
++cnt;
if (cnt > 1 || n != m)
return puts("NO");
else
dfs(i, i);
}
}
puts("FHTAGN!");
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const char letters[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
const int dx[] = {0, -1, 0, 1, -1, -1, 1, 1};
const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
long long inline max3(long long a, long long b, long long c) {
return max(a, max(b, c));
}
long long inline min3(long long a, long long b, long long c) {
return min(a, min(b, c));
}
long long power(long long x, long long p) {
if (p == 0)
return 1;
else if (p % 2 == 0)
return power(1LL * x * x, p / 2);
else
return 1LL * x * power(1LL * x * x, (p - 1) / 2);
}
long long GCD(long long a, long long b) {
if (b == 0) return a;
return GCD(b, a % b);
}
const int N = 150;
int n, m;
vector<vector<int> > G(N);
bool vis[N];
void dfs(int u, int p) {
vis[u] = 1;
for (int i = 0; i < ((int)G[u].size()); ++i) {
if (!vis[G[u][i]]) dfs(G[u][i], u);
}
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 0, u, v; i < m; ++i) {
scanf("%d", &u);
scanf("%d", &v);
G[u].push_back(v), G[v].push_back(u);
}
int cnt = 0;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
++cnt;
if (cnt > 1 || n != m)
return puts("NO");
else
dfs(i, i);
}
}
puts("FHTAGN!");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int const N = 3112;
int const INF = 1e9 + 7;
int n, m;
vector<int> g[N];
int used[N];
int cnt;
void dfs(int v) {
used[v] = true;
for (int c = 0; c < g[v].size(); c++) {
if (!used[g[v][c]]) dfs(g[v][c]);
}
}
int main() {
int x, y;
cin >> n >> m;
if (n != m) {
cout << "NO";
return 0;
}
for (int c = 1; c <= m; c++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
for (int c = 1; c <= n; c++) {
if (!used[c]) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int const N = 3112;
int const INF = 1e9 + 7;
int n, m;
vector<int> g[N];
int used[N];
int cnt;
void dfs(int v) {
used[v] = true;
for (int c = 0; c < g[v].size(); c++) {
if (!used[g[v][c]]) dfs(g[v][c]);
}
}
int main() {
int x, y;
cin >> n >> m;
if (n != m) {
cout << "NO";
return 0;
}
for (int c = 1; c <= m; c++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
for (int c = 1; c <= n; c++) {
if (!used[c]) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 511111;
int mm[105][105];
bool vis[1005];
int m, n;
void dfs(int u) {
vis[u] = 1;
for (int i = 1; i <= n; i++) {
if (vis[i] == 0 && mm[u][i] == 1) dfs(i);
}
}
int main() {
while (~scanf("%d%d", &n, &m)) {
int i;
for (i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
mm[u][v] = mm[v][u] = 1;
}
int flag = 0;
memset(vis, 0, sizeof(vis));
dfs(1);
for (int i = 1; i <= n; i++)
if (!vis[i]) flag = 1;
if (flag == 0 && m == n)
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 511111;
int mm[105][105];
bool vis[1005];
int m, n;
void dfs(int u) {
vis[u] = 1;
for (int i = 1; i <= n; i++) {
if (vis[i] == 0 && mm[u][i] == 1) dfs(i);
}
}
int main() {
while (~scanf("%d%d", &n, &m)) {
int i;
for (i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
mm[u][v] = mm[v][u] = 1;
}
int flag = 0;
memset(vis, 0, sizeof(vis));
dfs(1);
for (int i = 1; i <= n; i++)
if (!vis[i]) flag = 1;
if (flag == 0 && m == n)
cout << "FHTAGN!" << endl;
else
cout << "NO" << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000;
vector<int> g[MAX_N + 10];
vector<int> dfs_trace;
bool is_circle[MAX_N + 10] = {0};
int circles_finded = 0;
int b[MAX_N + 10] = {0};
void scanGraph(int m) {
int x;
int y;
for (int i = 0; i < m; ++i) {
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
}
void dfs(int v, int prev = 0) {
b[v] = 1;
for (int e = 0; e < g[v].size(); ++e) {
if (b[g[v][e]] == 0) {
dfs(g[v][e], v);
}
}
b[v] = 2;
}
bool isConnected(int n) {
dfs(1);
for (int i = 1; i <= n; ++i) {
if (b[i] != 2) return false;
}
return true;
}
int main() {
int n;
int m;
scanf("%d %d", &n, &m);
scanGraph(m);
if (n == m && n > 2 && isConnected(n)) {
printf("FHTAGN!");
} else {
printf("NO");
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000;
vector<int> g[MAX_N + 10];
vector<int> dfs_trace;
bool is_circle[MAX_N + 10] = {0};
int circles_finded = 0;
int b[MAX_N + 10] = {0};
void scanGraph(int m) {
int x;
int y;
for (int i = 0; i < m; ++i) {
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
}
void dfs(int v, int prev = 0) {
b[v] = 1;
for (int e = 0; e < g[v].size(); ++e) {
if (b[g[v][e]] == 0) {
dfs(g[v][e], v);
}
}
b[v] = 2;
}
bool isConnected(int n) {
dfs(1);
for (int i = 1; i <= n; ++i) {
if (b[i] != 2) return false;
}
return true;
}
int main() {
int n;
int m;
scanf("%d %d", &n, &m);
scanGraph(m);
if (n == m && n > 2 && isConnected(n)) {
printf("FHTAGN!");
} else {
printf("NO");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<bool> used;
vector<int> cycle;
vector<int> p;
bool find_cycle(int v, int pr) {
p[v] = pr;
used[v] = 1;
for (int i : g[v]) {
if (used[i] && i != pr) {
while (v != i) {
cycle.push_back(v);
if (v == -1) {
for (int i = 0; i < cycle.size(); i++) {
cout << cycle[i] << " ";
}
exit(0);
}
v = p[v];
}
cycle.push_back(i);
return 1;
}
if (!used[i] && find_cycle(i, v)) {
return 1;
}
}
return 0;
}
void dele(int v1, int v2) {
for (int i = 0; i < g[v1].size(); i++) {
if (g[v1][i] == v2) {
g[v1].erase(g[v1].begin() + i);
}
}
for (int i = 0; i < g[v2].size(); i++) {
if (g[v2][i] == v1) {
g[v2].erase(g[v2].begin() + i);
}
}
}
void dfs(int v) {
used[v] = 1;
for (int i : g[v]) {
if (!used[i]) {
dfs(i);
}
}
}
int main() {
int n, m;
cin >> n >> m;
g.resize(n);
p.resize(n);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[--x].push_back(--y);
g[y].push_back(x);
}
used.resize(n, 0);
if (!find_cycle(0, -1) || cycle.size() < 3) {
cout << "NO" << endl;
return 0;
} else {
for (int i = 0; i < cycle.size(); i++) {
dele(cycle[i], cycle[(i + 1) % cycle.size()]);
m--;
}
}
used.assign(n, 0);
bool flag = 0;
for (int i = 0; i < cycle.size(); i++) {
if (used[cycle[i]]) {
cout << "NO" << endl;
return 0;
}
if (find_cycle(cycle[i], -1)) {
cout << "NO" << endl;
return 0;
}
}
for (int i = 0; i < n; i++) {
if (!used[i]) {
cout << "NO" << endl;
return 0;
}
}
cout << "FHTAGN!" << endl;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<bool> used;
vector<int> cycle;
vector<int> p;
bool find_cycle(int v, int pr) {
p[v] = pr;
used[v] = 1;
for (int i : g[v]) {
if (used[i] && i != pr) {
while (v != i) {
cycle.push_back(v);
if (v == -1) {
for (int i = 0; i < cycle.size(); i++) {
cout << cycle[i] << " ";
}
exit(0);
}
v = p[v];
}
cycle.push_back(i);
return 1;
}
if (!used[i] && find_cycle(i, v)) {
return 1;
}
}
return 0;
}
void dele(int v1, int v2) {
for (int i = 0; i < g[v1].size(); i++) {
if (g[v1][i] == v2) {
g[v1].erase(g[v1].begin() + i);
}
}
for (int i = 0; i < g[v2].size(); i++) {
if (g[v2][i] == v1) {
g[v2].erase(g[v2].begin() + i);
}
}
}
void dfs(int v) {
used[v] = 1;
for (int i : g[v]) {
if (!used[i]) {
dfs(i);
}
}
}
int main() {
int n, m;
cin >> n >> m;
g.resize(n);
p.resize(n);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[--x].push_back(--y);
g[y].push_back(x);
}
used.resize(n, 0);
if (!find_cycle(0, -1) || cycle.size() < 3) {
cout << "NO" << endl;
return 0;
} else {
for (int i = 0; i < cycle.size(); i++) {
dele(cycle[i], cycle[(i + 1) % cycle.size()]);
m--;
}
}
used.assign(n, 0);
bool flag = 0;
for (int i = 0; i < cycle.size(); i++) {
if (used[cycle[i]]) {
cout << "NO" << endl;
return 0;
}
if (find_cycle(cycle[i], -1)) {
cout << "NO" << endl;
return 0;
}
}
for (int i = 0; i < n; i++) {
if (!used[i]) {
cout << "NO" << endl;
return 0;
}
}
cout << "FHTAGN!" << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int p[N];
vector<int> adj[N], roots;
int n, m;
bool vis[N], root[N];
int S = 0, E = 0, cnt = 0;
void store(int u) {
while (1) {
root[u] = 1;
roots.push_back(u);
if (u == E) return;
u = p[u];
}
}
void dfs(int u) {
vis[u] = 1;
for (auto v : adj[u]) {
if (!vis[v]) {
p[v] = u;
dfs(v);
} else if (p[u] != v) {
if (v == S) continue;
S = u, E = v, cnt++;
}
}
}
int main() {
for (int i = 0; i < N; i++) p[i] = 1;
scanf("%d %d", &n, &m);
int u, v;
for (int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i);
if (!S || cnt > 1) {
printf("NO\n");
return 0;
}
store(S);
for (int i = 0; i < N; i++) p[i] = 1;
memset(vis, 0, sizeof vis);
S = E = 0;
for (auto R : roots) {
vis[R] = 1;
for (auto v : adj[R]) {
if (root[v]) continue;
p[v] = R;
dfs(v);
if (S) {
printf("NO\n");
return 0;
}
}
}
for (int i = 1; i <= n; i++)
if (!vis[i]) {
printf("NO\n");
return 0;
}
printf("FHTAGN!\n");
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int p[N];
vector<int> adj[N], roots;
int n, m;
bool vis[N], root[N];
int S = 0, E = 0, cnt = 0;
void store(int u) {
while (1) {
root[u] = 1;
roots.push_back(u);
if (u == E) return;
u = p[u];
}
}
void dfs(int u) {
vis[u] = 1;
for (auto v : adj[u]) {
if (!vis[v]) {
p[v] = u;
dfs(v);
} else if (p[u] != v) {
if (v == S) continue;
S = u, E = v, cnt++;
}
}
}
int main() {
for (int i = 0; i < N; i++) p[i] = 1;
scanf("%d %d", &n, &m);
int u, v;
for (int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i);
if (!S || cnt > 1) {
printf("NO\n");
return 0;
}
store(S);
for (int i = 0; i < N; i++) p[i] = 1;
memset(vis, 0, sizeof vis);
S = E = 0;
for (auto R : roots) {
vis[R] = 1;
for (auto v : adj[R]) {
if (root[v]) continue;
p[v] = R;
dfs(v);
if (S) {
printf("NO\n");
return 0;
}
}
}
for (int i = 1; i <= n; i++)
if (!vis[i]) {
printf("NO\n");
return 0;
}
printf("FHTAGN!\n");
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int MAX = 1e18, MIN = -1e18, MOD = 1000000007;
void dfs(int s, vector<int> adj[], vector<int>& vis) {
vis[s] = 1;
for (auto i : adj[s])
if (!vis[i]) dfs(i, adj, vis);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, i, u, v, k = 0;
cin >> n >> m;
vector<int> adj[n + 1];
vector<int> vis(n + 1, 0);
for (i = 1; i <= m; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, adj, vis);
for (i = 1; i <= n; i++) {
if (!vis[i]) {
k = 1;
break;
}
}
if (k == 1 || n != m)
cout << "NO";
else
cout << "FHTAGN!";
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int MAX = 1e18, MIN = -1e18, MOD = 1000000007;
void dfs(int s, vector<int> adj[], vector<int>& vis) {
vis[s] = 1;
for (auto i : adj[s])
if (!vis[i]) dfs(i, adj, vis);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, i, u, v, k = 0;
cin >> n >> m;
vector<int> adj[n + 1];
vector<int> vis(n + 1, 0);
for (i = 1; i <= m; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, adj, vis);
for (i = 1; i <= n; i++) {
if (!vis[i]) {
k = 1;
break;
}
}
if (k == 1 || n != m)
cout << "NO";
else
cout << "FHTAGN!";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void dfs(int v, vector<vector<int>>& g, vector<bool>& used) {
if (!used[v]) {
used[v] = true;
for (auto u : g[v]) dfs(u, g, used);
}
}
int cycle_vertex(int v, vector<vector<int>>& g, vector<bool>& used,
vector<int>& p) {
if (used[v])
return v;
else {
used[v] = true;
for (auto u : g[v])
if (u != p[v]) {
p[u] = v;
int t = cycle_vertex(u, g, used, p);
if (t) return t;
}
return 0;
}
}
bool is_tree(int v, pair<int, int> prev, vector<vector<int>>& g,
vector<bool>& used) {
if (used[v]) return false;
used[v] = true;
for (auto u : g[v])
if (u != prev.first && u != prev.second)
if (!is_tree(u, {v, 0}, g, used)) return false;
return true;
}
int main() {
int n, m;
cin >> n >> m;
vector<int> p(n + 1), cycle;
vector<vector<int>> g(n + 1);
vector<bool> used(n + 1);
for (int i = 0; i < m; ++i) {
int x1, x2;
cin >> x1 >> x2;
g[x1].push_back(x2);
g[x2].push_back(x1);
}
int u = cycle_vertex(1, g, used, p);
used.clear();
used.resize(n + 1);
dfs(1, g, used);
int j = 1;
while (j <= n && used[j]) ++j;
if (j <= n || u == 0) {
cout << "NO";
return 0;
}
used.clear();
used.resize(n + 1);
cycle.push_back(u);
for (u = p[u]; u != cycle[0]; u = p[u]) cycle.push_back(u);
for (auto v : cycle) used[v] = true;
for (int i = 0; i < cycle.size(); ++i) {
used[cycle[i]] = false;
if (!is_tree(cycle[i],
{cycle[(i - 1 + cycle.size()) % cycle.size()],
cycle[(i + 1) % cycle.size()]},
g, used)) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void dfs(int v, vector<vector<int>>& g, vector<bool>& used) {
if (!used[v]) {
used[v] = true;
for (auto u : g[v]) dfs(u, g, used);
}
}
int cycle_vertex(int v, vector<vector<int>>& g, vector<bool>& used,
vector<int>& p) {
if (used[v])
return v;
else {
used[v] = true;
for (auto u : g[v])
if (u != p[v]) {
p[u] = v;
int t = cycle_vertex(u, g, used, p);
if (t) return t;
}
return 0;
}
}
bool is_tree(int v, pair<int, int> prev, vector<vector<int>>& g,
vector<bool>& used) {
if (used[v]) return false;
used[v] = true;
for (auto u : g[v])
if (u != prev.first && u != prev.second)
if (!is_tree(u, {v, 0}, g, used)) return false;
return true;
}
int main() {
int n, m;
cin >> n >> m;
vector<int> p(n + 1), cycle;
vector<vector<int>> g(n + 1);
vector<bool> used(n + 1);
for (int i = 0; i < m; ++i) {
int x1, x2;
cin >> x1 >> x2;
g[x1].push_back(x2);
g[x2].push_back(x1);
}
int u = cycle_vertex(1, g, used, p);
used.clear();
used.resize(n + 1);
dfs(1, g, used);
int j = 1;
while (j <= n && used[j]) ++j;
if (j <= n || u == 0) {
cout << "NO";
return 0;
}
used.clear();
used.resize(n + 1);
cycle.push_back(u);
for (u = p[u]; u != cycle[0]; u = p[u]) cycle.push_back(u);
for (auto v : cycle) used[v] = true;
for (int i = 0; i < cycle.size(); ++i) {
used[cycle[i]] = false;
if (!is_tree(cycle[i],
{cycle[(i - 1 + cycle.size()) % cycle.size()],
cycle[(i + 1) % cycle.size()]},
g, used)) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
vector<int> a[101];
vector<int> vis(101);
void DFS(int s) {
vis[s] = 1;
for (int i = 0; i < a[s].size(); i++) {
int u = a[s][i];
if (!vis[u]) {
DFS(u);
}
}
}
int main() {
int n, m;
cin >> n >> m;
if (n != m) {
cout << "NO";
return 0;
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
DFS(1);
for (int i = 1; i < n; i++) {
if (!vis[i]) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
}
| ### Prompt
Please formulate a cpp solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
vector<int> a[101];
vector<int> vis(101);
void DFS(int s) {
vis[s] = 1;
for (int i = 0; i < a[s].size(); i++) {
int u = a[s][i];
if (!vis[u]) {
DFS(u);
}
}
}
int main() {
int n, m;
cin >> n >> m;
if (n != m) {
cout << "NO";
return 0;
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
DFS(1);
for (int i = 1; i < n; i++) {
if (!vis[i]) {
cout << "NO";
return 0;
}
}
cout << "FHTAGN!";
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int inf = 100010;
const int MOD = 1000000007;
const double pi = acos(-1.0);
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int lcm(int a, int b) { return (a * (b / gcd(a, b))); }
int fx[] = {0, 0, 1, -1};
int fy[] = {-1, 1, 0, 0};
int n, m;
int flg = 0;
int gr[105][105];
int vis[105], dis[105];
void dfs(int u) {
int i;
vis[u] = 1;
for (i = 0; i < n + 1; i++) {
if (gr[u][i] && !vis[i]) {
;
dfs(i);
}
}
}
int main() {
int i = 0, j;
int t, cnt = 0;
cin >> n >> m;
for (i = 0; i < m; i++) {
cin >> t >> j;
gr[t][j] = gr[j][t] = 1;
}
if (n == m) {
for (i = 1; i < n + 1; i++) {
if (!vis[i]) {
cnt++;
if (cnt > 1) {
flg = 1;
;
break;
}
dfs(i);
}
}
} else {
flg = 1;
};
if (!flg) {
cout << "FHTAGN!";
} else {
cout << "NO";
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
...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.
<image>
Input
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 β€ <image>).
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.
Output
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).
A rooted tree is a tree where one vertex is selected to be the root.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 100010;
const int MOD = 1000000007;
const double pi = acos(-1.0);
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int lcm(int a, int b) { return (a * (b / gcd(a, b))); }
int fx[] = {0, 0, 1, -1};
int fy[] = {-1, 1, 0, 0};
int n, m;
int flg = 0;
int gr[105][105];
int vis[105], dis[105];
void dfs(int u) {
int i;
vis[u] = 1;
for (i = 0; i < n + 1; i++) {
if (gr[u][i] && !vis[i]) {
;
dfs(i);
}
}
}
int main() {
int i = 0, j;
int t, cnt = 0;
cin >> n >> m;
for (i = 0; i < m; i++) {
cin >> t >> j;
gr[t][j] = gr[j][t] = 1;
}
if (n == m) {
for (i = 1; i < n + 1; i++) {
if (!vis[i]) {
cnt++;
if (cnt > 1) {
flg = 1;
;
break;
}
dfs(i);
}
}
} else {
flg = 1;
};
if (!flg) {
cout << "FHTAGN!";
} else {
cout << "NO";
}
return 0;
}
``` |