Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define endl "\n"
- vector <vector <int>> g;
- vector <int> color;
- vector <int> pr;
- int Start = -1;
- int End = -1;
- bool dfs(int v, int p)
- {
- color[v] = 1;
- for (auto to : g[v])
- {
- if (to == p)
- continue;
- if (color[to] == 1)
- {
- Start = to;
- End = v;
- return true;
- }
- if (color[to] == 0)
- {
- pr[to] = v;
- if (dfs(to,v))
- return true;
- }
- }
- color[v] = 2;
- return false;
- }
- struct SNM
- {
- vector<list<int>> ar;
- vector<int> pr;
- SNM(int n)
- {
- ar.resize(n);
- pr.resize(n);
- for(int i = 0; i < n; i++)
- {
- pr[i] = i;
- ar[i].push_back(i);
- }
- }
- void Merge(int a, int b)
- {
- a = pr[a];
- b = pr[b];
- if(ar[b].size() < ar[a].size())
- swap(a,b);
- if(a == b)
- return;
- for(int tmp : ar[a])
- {
- ar[b].push_back(tmp);
- pr[tmp] = b;
- }
- }
- int Get(int i)
- {
- return pr[i];
- }
- };
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n,m;
- cin>>n>>m;
- g.resize(n);
- color.assign(n,0);
- pr.assign(n,-1);
- for (int i = 0; i < m; i++)
- {
- int x,y;
- cin>>x>>y;
- x--,y--;
- g[x].push_back(y);
- g[y].push_back(x);
- }
- bool fl = false;
- for (int i = 0; i < n && !fl; i++)
- {
- if (color[i] != 0)
- continue;
- fl = dfs(i, -1);
- if (fl)
- break;
- }
- if (!fl)
- {
- cout<<"NO"<<endl;
- return 0;
- }
- vector <int> circle;
- circle.push_back(Start);
- int myEnd = End;
- while(Start != End)
- {
- circle.push_back(End);
- End = pr[End];
- }
- End = myEnd;
- for (int i = 0; i < g[Start].size(); i++) {
- if (g[Start][i] == End)
- {
- swap(g[Start][i], g[Start].back());
- g[Start].pop_back();
- break;
- }
- }
- for (int i = 0; i < g[End].size(); i++) {
- if (g[End][i] == Start)
- {
- swap(g[End][i], g[End].back());
- g[End].pop_back();
- break;
- }
- }
- fl = false;
- color.assign(n,0);
- for (int i = 0; i < n; i++)
- {
- if (color[i] != 0)
- continue;
- fl = dfs(i, -1);
- if (fl)
- break;
- }
- if (fl)
- {
- cout<<"NO"<<endl;
- return 0;
- }
- SNM snm(n);
- for (int i = 0; i < n; i++)
- {
- for (int j = 1; j < g[i].size(); j++)
- {
- snm.Merge(g[i][j], g[i][j-1]);
- }
- }
- set<int> st;
- for (int i = 0; i < n; i++)
- {
- st.insert(snm.Get(i));
- }
- if (circle.size() < 3 || n != m)
- cout<<"NO"<<endl;
- else
- cout<<"FHTAGN!"<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement