Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using i32 = int;
- using ui32 = unsigned int;
- using i64 = long long;
- using ui64 = unsigned long long;
- const ui32 MSIZE = 100;
- std::vector<ui32> g[MSIZE];
- ui32 color[MSIZE];
- bool cycle[MSIZE];
- bool used[MSIZE];
- void UsedDfs(ui32 from) {
- used[from] = true;
- for (ui32 v : g[from]) {
- if (!used[v]) {
- UsedDfs(v);
- }
- }
- }
- i32 Dfs(ui32 from, ui32 parent) {
- color[from] = 1;
- for (ui32 v : g[from]) {
- if (v == parent) continue;
- if (color[v] == 1) {
- cycle[from] = true;
- return v;
- }
- if (color[v] == 0) {
- i32 result = Dfs(v, from);
- if (result >= 0) {
- cycle[from] = true;
- if (result == from) {
- return -2;
- } else {
- return result;
- }
- } else if (result == -2) {
- return -2;
- }
- }
- }
- color[from] = 2;
- return -1;
- }
- i32 main() {
- ui32 n, m;
- scanf("%d%d", &n, &m);
- for (ui32 i = 0; i < m; i++) {
- int f, t;
- scanf("%d%d", &f, &t);
- f--, t--;
- g[f].push_back(t);
- g[t].push_back(f);
- }
- // check that graph is connected
- UsedDfs(0);
- bool isGod = true;
- for (ui32 i = 0; i < n; i++) {
- if (!used[i]) {
- isGod = false;
- }
- }
- // check that cycle exists
- if (Dfs(0, 0) != -2) {
- isGod = false;
- }
- // check that length of cycle >= 3
- ui32 trees = 0;
- for (ui32 i = 0; i < n; i++) {
- if (cycle[i]) {
- trees++;
- }
- }
- isGod &= trees >= 3;
- // check that it is only one cycle (cycle + trees = n edges => m should be equals to n)
- isGod &= m == n;
- puts(isGod ? "FHTAGN!" : "NO");
- }
Add Comment
Please, Sign In to add comment