Description

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

Source

ZJCPC 2012

不会写,看似水题的东西没想到这么难。

假如一个人存在unrequited love,那么表示这个结点没有入度,且这个点到所有的点一定是有路径的。

来自大牛的解题思路,数据比较的大用vector数组来建图,并且要使用二分找来判断是否存在通路。

 #include <stdio.h>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#define MAXN 60005
using namespace std; vector< int > V[MAXN];
vector< int > VP;
map< string , int > M;
string Name[MAXN];
int cnt; void initVP(){
VP.clear();
} void initGraph(){
for(int i=; i<MAXN; i++){
V[i].clear();
}
} void addString(string s){
if(M.find(s)==M.end()){
M[s]=cnt;
Name[cnt++]=s;
}
} void createMap(int n){
char ch[];
string s;
int c,u,v;
for(int i=; i<n; i++){
scanf("%s" ,ch);
s=string(ch);
addString(s);
u=M[s];
scanf("%d" ,&c);
for(int j=; j<c; j++){
scanf("%s" ,ch);
s=string(ch);
addString(s);
v=M[s];
V[u].push_back(v);
}
sort(V[u].begin(),V[u].end());
}
} int main()
{
int c,t;
int n,m,q;
scanf("%d" ,&t);
while( t-- ){
M.clear();
cnt=;
initGraph();
scanf("%d %d %d" ,&n ,&m ,&q);
createMap(n);
createMap(m);
while( q-- ){
initVP();
char ch[];
string s;
scanf("%d" ,&c);
while( c-- ){
scanf("%s" ,ch);
s=string(ch);
VP.push_back(M[s]);
}
int size=VP.size();
int i,j;
for(i=; i<size; i++){
for(j=; j<size; j++){
if(i==j)continue;
if( !binary_search( V[VP[i]].begin() , V[VP[i]].end(), VP[j])
|| binary_search( V[VP[j]].begin(), V[VP[j]].end(), VP[i]))
break;
}
if(j==size){
printf("1 %s\n",Name[VP[i]].c_str());
break;
}
}
if(i==size){
puts("");
}
}
puts("");
}
return ;
}

TOJ 4289 Unrequited Love的更多相关文章

  1. HDU 4289:Control(最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可 ...

  2. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  3. zjuoj 3601 Unrequited Love

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3601 Unrequited Love Time Limit: 16 Sec ...

  4. hdu 4289 最大流拆点

    大致题意:     给出一个又n个点,m条边组成的无向图.给出两个点s,t.对于图中的每个点,去掉这个点都需要一定的花费.求至少多少花费才能使得s和t之间不连通. 大致思路:     最基础的拆点最大 ...

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

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

  6. hdu 4289 Control(最小割 + 拆点)

    http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  7. TOJ 1702.A Knight's Journey

    2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...

  8. TOJ 1139.Compromise

    2015-06-03 问题简述: 大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列. 简单的LCS问题,但是输入的是一段话了,而且公共部分比较是 ...

  9. 优先队列运用 TOJ 4123 Job Scheduling

    链接:http://acm.tju.edu.cn/toj/showp4123.html 4123.   Job Scheduling Time Limit: 1.0 Seconds   Memory ...

随机推荐

  1. SignalR 跨域解决方案全面

    SignalR 分:PersistentConnection和Hub 2种模式. 跨域又分:UseCors和JsonP  2种方法 所以例子写了4种. 核心代码: UseCors //Persiste ...

  2. wp8 与wp7.5图标规格说明

    wp8 小图标 159*159 中图标 336*336 大图标 691*336 wp7.5 173*173

  3. 多态实现的原理------新标准c++程序设计

    “多态”的关键在于通过基类指针或引用调用一个虚函数时,编译时不确定到底调用的是基类还是派生类的函数,运行时才确定.例子: #include<iostream> using namespac ...

  4. 《spring 攻略》笔记1

    chapter1 spring简介 两种spring ioc容器实现类型: BeanFactory ApplicationContext 应用程序上下文 DI技巧: @Autowired(requir ...

  5. Charles设置可抓https的包

    一.help 二.proxy

  6. 洛谷P4724 【模板】三维凸包

    题面 传送门 题解 先理一下关于立体几何的基本芝士好了--顺便全都是从\(xzy\)巨巨的博客上抄来的 加减 三维向量加减和二维向量一样 模长 \(|a|=\sqrt{x^2+y^2+z^2}\) 点 ...

  7. [原创]ObjectARX开发环境搭建之VS2010+ObjectARX2012Wizard+Addin工具条问题修复

    目前ObjectARX版本越来越高,也越来越简化开发,如果需要同时开发低版本和高版本的ARX程序,就需要搭建批量编译环境,以满足ARX开发的需要. 批量编译的搭建网络上已经有了很多的教程,基本上都是基 ...

  8. 【spring源码】spring web 启动与关闭

    web.xml中有这么一段声明 <context-param> <param-name>contextConfigLocation</param-name> < ...

  9. React学习笔记一

    我是通过script src的方式引入的react的相关文件,本次学习顺序按照<React快速上手开发>一书的顺序学习的, 本篇博客笔记地址:http://note.youdao.com/ ...

  10. Python3之requests模块

    Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务. 发送G ...