zoj 3601

Unrequited Love

Time Limit: 16 Seconds      Memory Limit: 131072 KB

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 comingq 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 nextm 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 lastq 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 than20. 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


代码:

#include<iostream>
#include<stdio.h>
#include<map>
#include<set>
using namespace std;
int n,m,q;
int idx;
map<string,int> mymap;
string nmymap[30005];
pair<int,int> mypair;
set< pair<int,int> > myset;

map<string, int>::iterator it;

//int match(char *c)
//{
//    if(!mymap[c])
//    {
//        idx++;
//        mymap[c]=idx-1;
//        nmymap[idx-1]=c;
//        return idx-1;
//    }
//    else return mymap[c];
//}
int match(char * s)
{
    //name[cnt] = s;
    it = mymap.find(s);
    if(it != mymap.end())
        return it->second;
    else
    {
        nmymap[idx]=s;
        mymap[s]=idx++;
        return idx-1;
    }
}
int main()
{
    int T;
    char tmp[25];
    int party[30005];
    int ta;
    int peo;
    scanf("%d",&T);
    while(T--)
    {
        mymap.clear();
        myset.clear();
        idx=1;
        scanf("%d%d%d",&n,&m,&q);
        for(int i=0; i<n; i++)
        {
            scanf("%s",tmp);
            //if(!mymap[tmp])
                //{mymap[tmp]=idx++;nmymap[idx-1]=tmp;}
            int tm=match(tmp);
            scanf("%d",&ta);
            for(int i=0; i<ta; i++)
            {
                scanf("%s",tmp);
//                if(!mymap[tmp])
//                    mymap[tmp]=idx++,nmymap[idx-1]=tmp;
                int b=match(tmp);
                myset.insert(make_pair(tm,b));
            }
        }
        for(int i=0; i<m; i++)
        {
            scanf("%s",tmp);
            //if(!mymap[tmp])
               // mymap[tmp]=idx++,nmymap[idx-1]=tmp;
            int tm=match(tmp);
            scanf("%d",&ta);
            for(int i=0; i<ta; i++)
            {
                scanf("%s",tmp);
                //if(!mymap[tmp])
                    //mymap[tmp]=idx++,nmymap[idx-1]=tmp;
                    int b=match(tmp);
                myset.insert(make_pair(tm,b));
            }
        }
        char tmpp[25];
        for(int i=0; i<q; i++)
        {
            scanf("%d",&peo);
            scanf("%s",tmpp);
            party[0]=mymap[tmpp];
            int ans=party[0];
            int k=0;
            for(int j=1; j<peo; j++)
            {
                scanf("%s",tmpp);party[j]=mymap[tmpp];
                if(myset.find(make_pair(ans,party[j]))==myset.end()||myset.find(make_pair(party[j],ans))!=myset.end())
                {
                    ans=party[j];
                    k=j;
                }
            }
            bool flag=false;
            for(int r=0; r<k; r++)
            {
                if(ans!=party[r])
                    if(myset.find(make_pair(ans,party[r]))==myset.end()||myset.find(make_pair(party[r],ans))!=myset.end())
                    {
                        flag=true;
                    }
            }
            if(!flag)
            {
                cout<<"1 "<<nmymap[ans]<<endl;
            }
            else printf("0\n");
        }
        printf("\n");
    }
    return 0;
}
/*
2
2 1 4
BoyA 1 GirlC
BoyB 1 GirlC
GirlC 1 BoyA
2 BoyB GirlC
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值