CodeForces 1327B - Princesses and Princes(思维+读题)
B. Princesses and Princes
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
The King of Berland Polycarp LXXXIV has n daughters. To establish his power to the neighbouring kingdoms he wants to marry his daughters to the princes of these kingdoms. As a lucky coincidence there are n other kingdoms as well.
So Polycarp LXXXIV has enumerated his daughters from 1 to n and the kingdoms from 1 to n. For each daughter he has compiled a list of kingdoms princes of which she wanted to marry.
Polycarp LXXXIV is very busy, so he finds a couple for his daughters greedily one after another.
For the first daughter he takes the kingdom with the lowest number from her list and marries the daughter to their prince. For the second daughter he takes the kingdom with the lowest number from her list, prince of which hasn’t been taken already. If there are no free princes in the list then the daughter marries nobody and Polycarp LXXXIV proceeds to the next daughter. The process ends after the n-th daughter.
For example, let there be 4 daughters and kingdoms, the lists daughters have are [2,3], [1,2], [3,4], [3], respectively.
In that case daughter 1 marries the prince of kingdom 2, daughter 2 marries the prince of kingdom 1, daughter 3 marries the prince of kingdom 3, leaving daughter 4 nobody to marry to.
Actually, before starting the marriage process Polycarp LXXXIV has the time to convince one of his daughters that some prince is also worth marrying to. Effectively, that means that he can add exactly one kingdom to exactly one of his daughter’s list. Note that this kingdom should not be present in the daughter’s list.
Polycarp LXXXIV wants to increase the number of married couples.
Unfortunately, what he doesn’t have the time for is determining what entry to add. If there is no way to increase the total number of married couples then output that the marriages are already optimal. Otherwise, find such an entry that the total number of married couples increases if Polycarp LXXXIV adds it.
If there are multiple ways to add an entry so that the total number of married couples increases then print any of them.
For your and our convenience you are asked to answer t independent test cases.
Input
The first line contains a single integer t (1≤t≤105) — the number of test cases.
Then t test cases follow.
The first line of each test case contains a single integer n (1≤n≤105) — the number of daughters and the number of kingdoms.
Each of the next n lines contains the description of each daughter’s list. The first integer k (0≤k≤n) is the number of entries in the i-th daughter’s list. After that k distinct integers follow gi[1],gi[2],…,gi[k] (1≤gi[j]≤n) — the indices of the kingdoms in the list in the increasing order (gi[1]<gi[2]<⋯<gi[k]).
It’s guaranteed that the total number of daughters over all test cases does not exceed 105.
It’s also guaranteed that the total number of kingdoms in lists over all test cases does not exceed 105.
Output
For each test case print the answer to it.
Print “IMPROVE” in the first line if Polycarp LXXXIV can add some kingdom to some of his daughter’s list so that the total number of married couples increases. The second line then should contain two integers — the index of the daughter and the index of the kingdom Polycarp LXXXIV should add to that daughter’s list.
If there are multiple ways to add an entry so that the total number of married couples increases then print any of them.
Otherwise the only line should contain one word “OPTIMAL”.
Example
input
5
4
2 2 3
2 1 2
2 3 4
1 3
2
0
0
3
3 1 2 3
3 1 2 3
3 1 2 3
1
1 1
4
1 1
1 2
1 3
1 4
output
IMPROVE
4 4
IMPROVE
1 1
OPTIMAL
OPTIMAL
OPTIMAL
Note
The first test case is depicted in the statement. Adding the fourth kingdom to the list of the fourth daughter makes her marry the prince of the fourth kingdom.
In the second test case any new entry will increase the number of marriages from 0 to 1.
In the third and the fourth test cases there is no way to add an entry.
In the fifth test case there is no way to change the marriages by adding any entry.
Berland Polycarp LXXXIV国王有n个女儿。为了建立对邻近王国的权力,他想与他的女儿们嫁给这些王国的王子。幸运的巧合是,也没有其他王国。
因此,Polycarp LXXXIV列举了他的女儿从1到n,王国从1到n。他为每个女儿汇总了她想结婚的王国王子名单。
Polycarp LXXXIV非常忙,所以他一个个接一个地为女儿寻找一对夫妇。
对于第一个女儿,他从清单上选择了人数最少的王国,然后将女儿嫁给了王子。对于第二个女儿,他接管了清单上人数最少的王国,而王子尚未被接任。如果列表中没有空闲的王子,则该女儿未婚,Polycarp LXXXIV继续生下一个女儿。该过程在第n个女儿之后结束。
例如,假设有4个女儿和王国,女儿拥有的列表分别是[2,3],[1,2],[3,4],[3]。
在这种情况下,女儿1嫁给王国2的王子,女儿2嫁给王国1的王子,女儿3嫁给王国3的王子,而女儿4则没有人嫁给。
实际上,在开始婚姻过程之前,Polycarp LXXXIV有时间说服他的一个女儿一些王子也值得嫁给他。实际上,这意味着他可以将一个王国完全添加到其女儿的列表中。请注意,这个王国不应该出现在女儿的名单中。
Polycarp LXXXIV希望增加已婚夫妇的数量。
不幸的是,他没有时间确定要添加的条目。如果没有办法增加已婚夫妇的总数,那么输出的婚姻已经是最优的。否则,找到一个条目,如果将Polycarp LXXXIV添加,则已婚夫妇总数增加。
如果有多种添加条目的方法,使已婚夫妇总数增加,请打印其中的任何一个。
为了您和我们的方便,要求您回答独立的测试用例。
输入值
第一行包含一个整数t(1≤t≤105)-测试用例的数量。
然后是t个测试用例。
每个测试用例的第一行包含一个整数n(1≤n≤105)-子节点数和王国数。
接下来的n行中的每行都包含每个女儿列表的描述。第一个整数k(0≤k≤n)是第i个女儿列表中的条目数。之后,k个不同的整数跟随gi [1],gi [2],…,gi [k](1≤gi[j]≤n)—列表中王国的索引按升序排列(gi [1] <gi [2] <⋯<gi [k])。
确保所有测试用例中的子代总数不超过105。
还可以保证所有测试用例中列表中的王国总数不超过105。
输出量
对于每个测试用例,请打印答案。
如果Polycarp LXXXIV可以在其女儿的某些清单中添加某个王国,则在第一行中打印“ IMPROVE”,以便增加已婚夫妇的总数。然后,第二行应包含两个整数-女儿的索引和王国Polycarp LXXXIV的索引应添加到该女儿的列表中。
如果有多种添加条目的方法,使已婚夫妇总数增加,请打印其中的任何一个。
否则,仅一行应包含一个单词“ OPTIMAL”。
例
输入
5
4
2 2 3
2 1 2
2 3 4
1 3
2
0
0
3
3 1 2 3
3 1 2 3
3 1 2 3
1
1 1
4
1 1
1 2
1 3
1 4
输出
IMPROVE
4 4
IMPROVE
1 1
OPTIMAL
OPTIMAL
OPTIMAL
注意
语句中描述了第一个测试用例。将第四个王国添加到第四个女儿的名单中,使她与第四个王国的王子结婚。
在第二个测试用例中,任何新条目都会使婚姻数从0增加到1。
在第三和第四个测试用例中,无法添加条目。
在第五个测试用例中,无法通过添加任何条目来更改婚姻。
题目大意:题面非常长。主要看样例解释和给出的数据,多组测试样例,对于每一组测试样例给出一个n,表示有n个国家,我们要优先根据第 i 个公主的意愿来选择婚姻,接下来有n行,每行有一个k,之后有一个包含k个元素的递增序列,表示第 i 个公主的意愿,我们优先选择靠前的,如果第一意愿已经有配对的人了则往后找,以此类推。如果第 i 号公主的意愿全部配对完了,则 i 号公主当前应该处于未婚的状态,对于每一个样例,我们应该判断n个公主是不是全嫁出去了。当然,如果有没有未婚的公主,我们可以为其选择未婚的王子与其结婚,如果每一个公主都能找到王子,则输出“OPTIMAL”,如果有找不到王子的公主,则输出“IMPROVE”,并输出该公主的配对方案(如果有多种答案,输出一个就可以)。
解题思路:我们先设一个标记数组,表示该号的王子有没有结婚。未婚为false,已婚为true,输入一个 t ,接下来输入n和n行,我们再设一个临时数组a来保存第 i 号公主的意愿。同时设一个变量w来存储未婚的公主,如果w=-1,则证明没有未婚的公主,如果w!=-1,则w的编号就是公主的编号,从前往后遍历数组查找这些王子是否已婚,每次遍历时将找到的第一个未婚王子的状态设为true并退出循环,走完n个公主以后,最后判断一下w时否等于-1,如果等于-1则输出“OPTIMAL”,反之输出“IMPROVE”,并遍历标记数组,找到未婚的王子与其配对,最后输出w和王子的编号。AC代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int _max=1e5+50;
bool flag[_max];
int main()
{
int t;
cin>>t;
while(t--)
{
memset(flag,false,sizeof flag);
int n,k=0,w=-1;//w表示未婚的公主编号,k表示第k个公主
cin>>n;
while(n--&&++k)
{
int s,a[_max];
cin>>s;
for(int i=0;i<s;i++)
cin>>a[i];
bool b=false;//用于判断公主是否已婚,默认为未婚
for(int i=0;i<s;i++)
{
if(flag[a[i]]==false)//判断a[i]号王子是否已婚
{
flag[a[i]]=true;
b=true;
break;
}
}
if(!b)//如果b==false则说明该公主未婚,更新w的值
w=k;
}
if(w!=-1)
{
cout<<"IMPROVE"<<endl;
int v=1;
for(int i=1;i<=k;i++)//遍历标记数组找到未婚的王子
if(flag[i]==false)
{
v=i;
break;
}
cout<<w<<" "<<v<<endl;
}
else
{
cout<<"OPTIMAL"<<endl;
}
}
//system("pause");
return 0;
}