There are n single boys and m single girls. Each of them may love none, one or several of other people unrequitedly and one-sidedly. For the coming q days, each night some of them will come together to hold a single party. In the party, if someone loves all the others, but is not loved by anyone, then he/she is called king/queen of unrequited love.
Input
There are multiple test cases. The first line of the input is an integer T ≈ 50 indicating the number of test cases.
Each test case starts with three positive integers no more than 30000
-- n m q
. Then each of the next n lines describes a boy, and each of the next m lines describes a girl. Each line consists of the name, the number of unrequitedly loved people, and the list of these people's names. Each of the last q lines describes a single party. It consists of the number of people who attend this party and their names. All people have different names whose lengths are no more than 20
. But there are no restrictions that all of them are heterosexuals.
Output
For each query, print the number of kings/queens of unrequited love, followed by their names in lexicographical order, separated by a space. Print an empty line after each test case. See sample for more details.
Sample Input
2 2 1 4 BoyA 1 GirlC BoyB 1 GirlC GirlC 1 BoyA 2 BoyA BoyB 2 BoyA GirlC 2 BoyB GirlC 3 BoyA BoyB GirlC 2 2 2 H 2 O S He 0 O 1 H S 1 H 3 H O S 4 H He O S
Sample Output
0 0 1 BoyB 0 0 0
Author: WU, Zejun
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest
题意:给出一些人名和各自喜欢的人,然后有q个咨询,每个咨询给你一个聚会名单,如果其中有人喜欢着所有其他的人,且不被任何其他人喜欢,那么就符合条件,按字典序输出符合条件的所有人名。
解题思路:模拟,如有这样的人,那么必然有且只有一个
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
const int INF=0x3f3f3f3f;
#define LL long long
const int MAX=30090;
map<string,int>mp;
set<int>s[MAX];
int sum;
int n,m,q;
string x[MAX];
int get(string ch)
{
if(!mp.count(ch))
mp[ch]=sum++;
return mp[ch];
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d",&n,&m,&q);
sum=0;
mp.clear();
for(int i=0;i<MAX;i++)
s[i].clear();
string ch;
int a,k;
for(int i=0;i<n+m;i++)
{
cin>>ch>>a;
k=get(ch);
for(int j=0;j<a;j++)
{
cin>>ch;
s[k].insert(get(ch));
}
}
while(q--)
{
scanf("%d",&a);
for(int i=0;i<a;i++)
cin>>x[i];
string p=x[0];
int k=0;
for(int i=1;i<a;i++)
{
if(!s[get(x[k])].count(get(x[i]))||s[get(x[i])].count(get(x[k])))
p=x[i],k=i;
}
for(int i=0;i<k;i++)
{
if(!s[get(x[k])].count(get(x[i]))||s[get(x[i])].count(get(x[k])))
{
p="";
break;
}
}
if(p=="") printf("0\n");
else cout<<1<<" "<<p<<endl;
}
printf("\n");
}
return 0;
}