Social Network Community - SPOJ Solution C++

  Problem Link : SOCNETC 


👉 Hint : edit please

 


✅ C++ Solution :

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

int size[100001];
int par[100001];


int find(int x)
{
	if(par[x]==x)
		return x;
	return par[x]=find(par[x]);
}

int main()
{
	int q,n,m,p1,p2;
	char c;
	int x,y;
	cin>>n>>m;
	for(int i=0;i<=n;i++)
	{
		par[i]=i;
		size[i]=1;
	}
	cin>>q;
	while(q--)
	{
		cin>>c;
		if(c=='S')
		{
			cin>>x;
			p1=find(x);
			cout<<size[p1]<<endl;
		}
		else if(c=='E')
		{
			cin>>x>>y;
			p1=find(x);
			p2=find(y);
			if(p1==p2)
				cout<<"Yes"<<endl;
			else
				cout<<"No"<<endl;
		}
		else
		{
			cin>>x>>y;
			p1=find(x);
			p2=find(y);
			if(size[p1]+size[p2]<=m)
			{
				par[p2]=p1;
				size[p1]+=size[p2];
			}
		}
	}

}

 

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!!