1101C. Division and Union - Codeforces Solution C++

  Problem Link : 1101C. Division and Union 


✅ C++ Solution :

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

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,l,r;
		cin>>n;
		vector<pair<pair<int,int>,int> > v;
		for(int i=0;i<n;i++)
		{
			cin>>l>>r;
			v.push_back(make_pair(make_pair(l,r),i));
		}
		sort(v.begin(),v.end());
		int i=1;
		int mx=v[0].first.second;
		vector<int> ans(n);
		for(i=1;i<n;i++)
		{
			if(v[i].first.first > mx)
				break;
			mx=max(mx,v[i].first.second);
		}
		if(i==n)
			cout<<"-1\n";
		else
		{
			for(int j=0;j<n;j++)
			{
				if(j<i)
					ans[v[j].second]=1;
				else
					ans[v[j].second]=2;
			}
			for(int j=0;j<n;j++)
				cout<<ans[j]<<" ";
			cout<<"\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!!