标签:贪心
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.
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.
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.
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
0 0 1 BoyB 0 0 0
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4704
题意:n+m个人,各自喜欢num个人。 然后q个询问,每个询问给你num个人,问你在这num个人中,是否有个孤独者喜欢这num个人中的所有人,但是这num个人都没喜欢他。
做法:首先明确的是,这里只可能有一个人符合要求,否者 条件矛盾 。先假设这个人 是0号,从从小到大枚举 这num个人,假设第i个人是这个孤独者,那么i+j个人如果喜欢i,或者i不喜欢i+j,那么i就不符合条件,接下来就假设这个孤独者是i+j。i+j之前可以断定不是孤独者。 到最后就可以得到假设的孤独者,然后从0开始再循环一遍,判断他是否真是孤独者。然后就ok了。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #include <stack> #include <queue> #include <vector> #include <deque> #include <set> #include <map> using namespace std; #define maxn 505 map<int,set<int> > my; map<string,int> turn; int ID(string& str) { if(turn.count(str)==0) return turn[str]=turn.size(); return turn[str]; } string str[40000]; int main() { int t; int i,j; int n,m,q; int num; cin>>t; while(t--) { my.clear(); turn.clear(); cin>>n>>m>>q; for(i=0;i<n+m;i++) { string a,b;; int n; int numa,numb; cin>>a; numa=ID(a); cin>>num; for(j=0;j<num;j++) { cin>>b; numb=ID(b); my[numa].insert(numb); } } for(i=0;i<q;i++) { string a,b,nw; cin>>num; int id=1; for(j=0;j<num;j++) { cin>>str[j]; if(j==0) { nw=str[j]; continue; } else { int num_nw=turn[nw]; int num_a=turn[str[j]]; if(my[num_nw].count(num_a)==0||my[num_a].count(num_nw)) { nw=str[j]; } } } int num_nw=turn[nw]; for(j=0;j<num;j++) { if(str[j]==nw) continue; int num_a=turn[str[j]]; if(my[num_nw].count(num_a)==0||my[num_a].count(num_nw)) { nw=""; break; } } if(nw=="") cout<<0<<endl; else cout<<1<<' '<<nw<<endl; } cout<<endl; } return 0; }
zoj 4704 Unrequited Love 贪心 孤独者
标签:贪心
原文地址:http://blog.csdn.net/u013532224/article/details/45228801