1327B - Princesses and Princes - CodeForces Solution


brute force graphs greedy *1200

Please click on ads to support us..

Python Code:

for i in range(int(input())):
    n = int(input())
    taken = [False for _ in range(n)]
    v = -1
    for j in range(n):
        l = [int(i)-1 for i in input().split()][1:]
        for k in l:
            if not taken[k]:
                taken[k] = True
                break
        else:
            v = j
    if v == -1:
        print('OPTIMAL')
    else:
        f = taken.index(False)
        print(f"IMPROVE\n{v+1} {f+1}")

C++ Code:

#include<bits/stdc++.h>
#include<bitset>
using namespace std;
typedef long long ll; 
const ll p=1e9+7;
// ll factorial(ll n)
// {
    // const ll M = 1000000007;
    // ll f = 1;
//  
    // for (ll i = 1; i <= n; i++)
       // f = (f*i) % M;
    // return f ;
// }
ll power(ll x,ll y,ll p)
{
    ll res = 1; 
 
    x = x % p; 
 
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1; 
        x = (x * x) % p;
    }
    return res;
}
// ll modInverse(ll n, ll p)
// {
    // return power(n, p - 2, p);
// }
// ll nCrModPFermat(ll n,ll r, ll p)
// {
    // if (n < r)
        // return 0;
    // if (r == 0)
        // return 1;
    // ll fac[n + 1];
    // fac[0] = 1;
    // for (ll i = 1; i <= n; i++)
        // fac[i] = (fac[i - 1] * i) % p;
//  
    // return (fac[n] * modInverse(fac[r], p) % p
            // * modInverse(fac[n - r], p) % p)
           // % p;
// }
void solve()
    { 
  ll n;
  cin>>n;
  ll N=n;
  vector<ll> d(n+1,0);
  vector<ll> p(n+1,0);
  ll j=1;
  while(N--)
  {
  	ll x;
  	cin>>x;
  	vector<ll> a(x);
  	ll c=0;
  	for(int i=0;i<x;i++)
  	{
  	  cin>>a[i];
  	  if(p[a[i]]==0 && c==0)
  	 {
  		p[a[i]]=1;
  		d[j]=1;
  		c++;
  	 }
  	}
  	j++;
  }
 for(int i=1;i<=n;i++)
 {
 	if(d[i]==0)
 	{
 		cout<<"IMPROVE"<<"\n";
 		cout<<i<<" ";
 		break;
 	}
 }
 for(int i=1;i<=n;i++)
 {
 	if(p[i]==0)
 	{
 		cout<<i<<" ";
 		return;
 	}
 }
 cout<<"OPTIMAL";
 
   }
 int main()
  { 
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    ll T = 1;
    cin >> T; 
    while (T--)
    {
        solve();
        cout<<"\n";
    }
    return 0;
  }


Comments

Submit
0 Comments
More Questions

515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards