103B. Cthulhu - Codeforces Solution C++

  Problem Link : 103B. Cthulhu 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

int parent[101];

int find(int v)
{
    if(parent[v]==v)
        return v;
    
    return parent[v]=find(parent[v]);    
}

int main()
{
	int n,m,x,y,a,b;
	int oneCycle=0;  
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		parent[i]=i;
	while(m--)
	{
		cin>>x>>y;
		a=find(x);
		b=find(y);
		if(a!=b)
			parent[a]=b;
		else
			oneCycle++;

		if(oneCycle>1)
		{
			oneCycle=0;
			break;
		}
	}
	if(oneCycle!=1)
		cout<<"NO\n";
	else
	{
		bool flag=0;
		int v=find(1);
		for(int i=2;i<=n;i++)
			if(find(i)!=v)
			{
				flag=1;
				break;
			}

		if(!flag)
			cout<<"FHTAGN!\n";
		else
			cout<<"NO\n";		



	}


}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!