码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 3601 Unrequited Love 浙江省第九届省赛

时间:2015-05-02 21:54:58      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

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 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

看的人家的代码算是理解了,我的超时了,代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
#include<set>
using namespace std;
map<string, int >mymap ;
pair<int ,int >mypair;
set< pair<int,int > >myset;
map<string, int>::iterator it;
string nmymap[30010];
char s1[30];
int idx;
int match(char *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;
   int n,m,q,n0;
   int party[30005];
   scanf("%d",&T);
   while(T--)
   {
    mymap.clear();
    myset.clear();
    idx = 1;
       scanf("%d %d %d",&n,&m,&q);
       while(n--)
       {
         scanf("%s %d",s1,&n0);
           int tm = match(s1);
            while(n0--)
             {
               scanf("%s",s1);
                int b = match(s1);
                 myset.insert(make_pair(tm,b));//返回他和第几个构成一对;
             }
       }
       while(m--)
       {
        scanf("%s %d",s1,&n0);
           int tm = match(s1);
           while(n0--)
           {
               scanf("%s",s1);
              int b = match(s1);
              myset.insert(make_pair(tm,b));
           }
       }
      char s2[30];
       while(q--)
       {
           scanf("%d",&n0);
           scanf("%s",s2);
           party[0] = mymap[s2];
           int ans = party[0];
           int k = 0;
           for(int j=1;j<n0;j++)
           {
            scanf("%s",s2);
             party[j] = mymap[s2];
             //如果我爱的人不爱我,然后继续
             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)
           printf("1 %s\n",nmymap[ans].c_str());
               else printf("0\n");
     }
     printf("\n");
   }
  return 0;
}

 

ZOJ 3601 Unrequited Love 浙江省第九届省赛

标签:

原文地址:http://www.cnblogs.com/lovychen/p/4472494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!