http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3601
Unrequited love Time Limit: Seconds Memory Limit: 131072 KB
There is a single boys and a n m girls. Each of them could love none, one or several of the other people unrequitedly and one-sidedly. qfor the coming days, each night some of them would come together to hold a single party. In the party, if someone loves all the others, but was not loved by anyone, then he/she is called king/queen of unrequited Love.
Input
There is multiple test cases. The first line of the input was an integer ≈50 indicating the number of the T test cases.
Each test case starts with three positive integers no more than 30000
-- n m q
. Then all n of the next 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. It consists of the number of people who attend this party and their names. All people has different names whose lengths is no more than 20
. But there is no restrictions that all of them is heterosexuals.
Output
For each query, print the number of kings/queens of unrequited love, followed by their names in lexicographical order, Sep Arated by a space. Print an empty line after each test case. See sample for more details.
Sample Input
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
Author: WU, Zejun
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest
Analysis:
Given some relationships, for everyone (boys or girls), list the people he likes (allow homosexuality), for each inquiry (party), ask such a person: he likes everyone, but everyone doesn't like him.
Analysis: Simple analysis shows that if there is a person, there is at most one. Because if there are 2 such people, they contradict test instructions with each other. So you can enumerate this person, how to quickly enumerate?
For a party, the first person is assumed to be such a person, the person who traverses the following, and the current person to judge the relationship, if it is found that this person can not be such a person, then the current traversal of the update to such a person.
After sweeping again, judge whether this person is really, as long as he and all the people in front of him to judge can
AC Code:
1#include <cstdio>2#include <string>3#include <Set>4#include <map>5 using namespacestd;6 Const intn=30005;7map<string,int>M;8map<string,int>:: iterator it;9 Set< pair<int,int> >S;Ten stringName[n]; One intTol,party[n]; A CharMax A]; - intHashChar*s) { -it=M.find (s); the if(It!=m.end ())returnIt->second; - Else { -name[++tol]=s; - returnm[s]=Tol; + } - } + voidCin (intx) { A inti,k,u,v; at for(i=0; i<x;i++){ -scanf"%s%d",na,&k); -u=hash (NA); - while(k--){ -scanf"%s", NA); -v=hash (NA); in S.insert (Make_pair (u,v)); - } to } + } - intMain () { the intT,n,m,q,i,k,ans; *scanf"%d",&T); $ while(t--){Panax Notoginsengscanf"%d%d%d",&n,&m,&q); -M.clear (), S.clear (), tol=0; the CIN (N), CIN (m); + while(q--){ Ascanf"%d%s",&K,na); theans=party[0]=M[na]; + intp=0; - for(i=1; i<k;i++){ $scanf"%s", NA), party[i]=M[na]; $ if(S.find (Make_pair (ans,party[i)) ==s.end () | | S.find (Make_pair (party[i],ans))! =S.end ()) { -ans=party[i],p=i; - } the } - for(i=0; i<p;i++){Wuyi if(S.find (Make_pair (ans,party[i)) ==s.end () | | S.find (Make_pair (Party[i],ans))!=s.end ()) Break; the } - if(i!=p) puts ("0"); Wu Elseprintf"1%s\n", Name[ans].c_str ()); - } AboutPuts""); $ } - return 0; -}
View Code
Zjuoj 3601 unrequited love