Unrequited Love

来源:互联网 发布:网络综艺为什么这么火 编辑:程序博客网 时间:2024/05/24 01:38

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.

<h4< dd="">
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.

<h4< dd="">
Sample Input
22 1 4BoyA 1 GirlCBoyB 1 GirlCGirlC 1 BoyA2 BoyA BoyB2 BoyA GirlC2 BoyB GirlC3 BoyA BoyB GirlC2 2 2H 2 O SHe 0O 1 HS 1 H3 H O S4 H He O S

Sample Output
001 BoyB000
找出一个人--->这个人喜欢聚会里面的每个人,但是聚会里的每个人不喜欢他。
没有输出0;
思路:用STL和count函数。  还有很容易得出每次聚会最多只有一个人符合条件。
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<set>#include<map>#include<vector>using namespace std;int main(){    int n,m,i,j,k,t,p,q,n1,n2,n3,n4;    scanf("%d",&t);    while(t--)    {        string name,name1,g;        string z[30010];        map<string,int>id;        set<string>s[36000];        scanf("%d %d %d",&n,&m,&p);        q=-1;        for(i=0; i<n+m; i++)        {            cin>>name1;            if(id.count(name1)==0)            {                q++;                id[name1]=q;//把名字转化为数字存入map里面。            }            scanf("%d",&k);            for(j=0; j<k; j++)            {                cin>>name;                s[id[name1]].insert(name);            }        }        for(j=0; j<p; j++)        {            scanf("%d",&n1);            for(i=0; i<n1; i++)            {                cin>>z[i];            }            g=z[0];            int f=0;            for(i=1; i<n1; i++)            {                if(s[id[z[f]]].count(z[i])==0||s[id[z[i]]].count(z[f]))                {                    f=i;                    g=z[i];                }            }            //cout<<g<<endl;            int flag=0;            for(i=0; i<f; i++)            {                if(s[id[z[i]]].count(z[f])||s[id[g]].count(z[i])==0)                {                    g="0";                    break;                }            }        }        if(g=="0")            cout<<0<<endl;        else            cout<<1<<" "<<g<<endl;        cout<<endl;    }    return 0;}



原创粉丝点击