1327B. Princesses and Princes - Codeforces Solution C++

  Problem Link : 1327B. Princesses and Princes 


✅ C++ Solution :

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

#define ll long long int

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k,val;
		cin>>n;
		unordered_set<int> us;
		vector<int> dgt[n+1];
		int cnt=0;
		int nd=-1;
		for(int i=1;i<=n;i++)
		{
			cin>>k;
			bool flag=1;
			for(int j=1;j<=k;j++)
			{
				cin>>val;
				dgt[i].push_back(val);
			}
			for(int j=0;j<dgt[i].size();j++)
			{
				if(us.find(dgt[i][j])==us.end())
				{
					flag=0;
					cnt++;
					us.insert(dgt[i][j]);
					break;
				}

			}
			if(flag)
				nd=i;
		}
		if(cnt==n)
			cout<<"OPTIMAL\n";
		else
		{
			cout<<"IMPROVE\n";
			for(int i=1;i<=n;i++)
			{
				if(us.find(i)==us.end())
				{
					cout<<nd<<" "<<i<<endl;
					break;
				}
			}
		}
	}
}

 

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