http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=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 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

Author: WU, Zejun
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest

分析:

给定一些关系,对于每个人(男孩或女孩),列出他所喜欢的人(允许同性恋),对于每次询问(聚会),求这样一种人:他喜欢所有人,但所有人都不喜欢他

分析:简单分析可知,这种人假如存在,最多只有一个。因为假设有2个这样的人,他们彼此就与题意矛盾。故可以枚举这个人,如何快速枚举?

对于一次聚会,先把第一个人假设为这种人,遍历其后的人,与当前这个人判断关系,若发现这个人不可能是这种人,则把当前遍历的更新为这种人。

扫一遍后,再判断这个人是否真的是,只要和他前面所有的人判断一下即可

AC代码:

 #include<cstdio>
#include<string>
#include<set>
#include<map>
using namespace std;
const int N=;
map<string,int> M;
map<string,int>::iterator it;
set< pair<int,int> > S;
string name[N];
int tol,party[N];
char na[];
int hash(char *s){
it=M.find(s);
if(it!=M.end())return it->second;
else {
name[++tol]=s;
return M[s]=tol;
}
}
void Cin(int x){
int i,k,u,v;
for(i=;i<x;i++){
scanf("%s%d",na,&k);
u=hash(na);
while(k--){
scanf("%s",na);
v=hash(na);
S.insert(make_pair(u,v));
}
}
}
int main(){
int T,n,m,q,i,k,ans;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&q);
M.clear(),S.clear(),tol=;
Cin(n),Cin(m);
while(q--){
scanf("%d%s",&k,na);
ans=party[]=M[na];
int p=;
for(i=;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;
}
}
for(i=;i<p;i++){
if(S.find(make_pair(ans,party[i]))==S.end()||S.find(make_pair(party[i],ans))!=S.end())break;
}
if(i!=p)puts("");
else printf("1 %s\n",name[ans].c_str());
}
puts("");
}
return ;
}

zjuoj 3601 Unrequited Love的更多相关文章

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

    Unrequited Love Time Limit: 16 Seconds      Memory Limit: 131072 KB There are n single boys and m si ...

  2. ZOJ 3601 Unrequited Love 【STL__pair_的应用】

    下面这个例子就是 STL:pair 的用法 #include <iostream> #include <utility> #include <string> usi ...

  3. BZOJ 3601: 一个人的数论

    题目链接:www.lydsy.com/JudgeOnline/problem.php?id=3601 题意: 思路: 因此可以用高斯消元得到ai. const int mod=1000000007; ...

  4. TOJ 4289 Unrequited Love

    Description There are n single boys and m single girls. Each of them may love none, one or several o ...

  5. 尺取法 POJ 3601 Subsequence

    题目传送门 /* 题意:求连续子序列的和不小于s的长度的最小值 尺取法:对数组保存一组下标(起点,终点),使用两端点得到答案 1. 记录前i项的总和,求[i, p)长度的最小值,用二分找到sum[p] ...

  6. zjuoj 3773 Paint the Grid

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3773 Paint the Grid Time Limit: 2 Secon ...

  7. zjuoj 3780 Paint the Grid Again

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...

  8. zjuoj 3602 Count the Trees

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602 Count the Trees Time Limit: 2 Seco ...

  9. zjuoj 3608 Signal Detection

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3608 Signal Detection Time Limit: 2 Sec ...

随机推荐

  1. Linux进程间通信:IPC对象——信号灯集详解

    作者:倪老师,华清远见嵌入式学院讲师. 一.信号灯概述 信号灯与其他进程间通信方式不大相同,它主要提供对进程间共享资源访问控制机制.相当于内存中的标志,进程可以根据它判定是否能够访问某些共享资源,同时 ...

  2. 如何在WORD2010中取消自动编号?

    如何在WORD2010中取消自动编号? 使用WORD2010有一个很大的问题就是WORD2010的自动编号问题,WORD2010的自动编号是符合外国人的写作习惯的,对中国人来说不适用. WORD201 ...

  3. POI Word 模板 文字 图片 替换

    实验环境:POI3.7+Word2007 Word模板: 替换后效果: 代码: 1.入口文件 public class Test { public static void main(String[] ...

  4. 纯css3绘制扇形

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 关于网上常见的几种MD5加密的区别

    (1) using System.Security.Cryptography; byte[] result = Encoding.Default.GetBytes(yps); //tbPass为输入密 ...

  6. hdu Sudoku Killer

    简单的dfs,主要就是每个?处填的值是否满足条件的判断.这道题感觉考察的是输出格式的控制. #include"iostream" #include"stdio.h&quo ...

  7. Webform购物车(用Session存储,页面传值)

    购物车主要实现的功能: ①在主页面可以将所有商品显示出来,包括价格,库存. ②点击购买可以累加产品,如果是同一种产品,只会累加每种产品的数量. ③查看购物车,可以查看明细,包括所购物品的名称,价格,数 ...

  8. MarkMan – 马克鳗,让设计更有爱!

    scavin(Google+) on 2010.11.16. MarkMan – 马克鳗 是一款方便高效的标注工具,极大节省设计师在设计稿上添加和修改标注的时间,让设计更有爱.Adobe AIR 平台 ...

  9. 使用Fiddler的X5S插件查找XSS漏洞

    OWASP top 10的安全威胁中的CrossSite Scripting(跨站脚本攻击),允许攻击者通过浏览器往网站注入恶意脚本.这种漏洞经常出现在web应用中需要用户输入的地方,如果网站有XSS ...

  10. 了解C++默认编写并调用哪些函数

    概念:编译器可以暗自为class创建default构造函数.copy构造函数.copy assignmengt构造函数,以及析构函数. 比如你写下:struct Node { }; 这就好像你写下这样 ...