TOJ 4289 Unrequited Love
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
不会写,看似水题的东西没想到这么难。
假如一个人存在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的更多相关文章
- HDU 4289:Control(最小割)
http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可 ...
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
- zjuoj 3601 Unrequited Love
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3601 Unrequited Love Time Limit: 16 Sec ...
- hdu 4289 最大流拆点
大致题意: 给出一个又n个点,m条边组成的无向图.给出两个点s,t.对于图中的每个点,去掉这个点都需要一定的花费.求至少多少花费才能使得s和t之间不连通. 大致思路: 最基础的拆点最大 ...
- ZOJ 3601 Unrequited Love 浙江省第九届省赛
Unrequited Love Time Limit: 16 Seconds Memory Limit: 131072 KB There are n single boys and m si ...
- hdu 4289 Control(最小割 + 拆点)
http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others) Mem ...
- TOJ 1702.A Knight's Journey
2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...
- TOJ 1139.Compromise
2015-06-03 问题简述: 大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列. 简单的LCS问题,但是输入的是一段话了,而且公共部分比较是 ...
- 优先队列运用 TOJ 4123 Job Scheduling
链接:http://acm.tju.edu.cn/toj/showp4123.html 4123. Job Scheduling Time Limit: 1.0 Seconds Memory ...
随机推荐
- 使用java实现单链表----(java中的引用就是指针)
//一直以为java中没有指针,其实java的引用就是指针,只不过堆栈中的引用储存了在堆中的地址,可以看做java中的指针.public class sibgleLink<E> { // ...
- c#帮助类:发送邮件
private static string IsOpenSendMail = ConfigurationManager.AppSettings["IsOpenSendMail"]; ...
- c++最短路经典问题
一提起最短路,各位oier会想到什么呢? floyd,spfa,dij,或是bellman-ford? 其实,只要学会一种算法,大部分最短路问题就能很快解决了. 他就是堆优化的dijkstra. 首先 ...
- day08.4-samba共享网盘服务
1. 安装软件:yum install samba -y 2. 新建共享目录物理路径:mkdir /zizaijiapu 修改配置文件:vim /etc/samba/smb.con ...
- MVC进阶篇(一)——概览
前言 说到MVC,就得先说说框架是什么东西,MVC好多人都知道,是Model.view.controller,但是MVC到底是什么样的一个框架呢,好多人又说了是约定大于配置.下面我来说说我的理解. 内 ...
- BeanShell Processor_使用Java处理脚本
版权声明:本文为博主原创文章,未经博主允许不得转载. [try-catch] 建议使用Try----Catch块,这样Java语句出现问题时,日志更清晰: try { //java代码 } catch ...
- linux vmalloc和kmalloc
kmalloc是内核低端内存的分配,而vmalloc对应内核高端内存的分配.kmalloc()分配的内存处于3GB-high_memory之间,这一段内核空间与物理内存的映射. kmalloc保证分配 ...
- 如何在UITableViewController上添加一个固定的视图
最近在使用UITableViewController,想在上面添加一个固定的视图,不随ScrollView滑动而移动.最后找到2种解决办法,一种是计算TableView的偏移,调整视图的位置,不断更新 ...
- 忘记commit也会造成select查询的性能问题
今天遇到一个很有意思的问题,一个开发人员反馈在测试服务器ORACLE数据库执行的一条简单SQL语句非常缓慢,他写的一个SQL没有返回任何数据,但是耗费了几分钟的时间.让我检查分析一下原因,分析解决过后 ...
- 4、numpy+pandas速查手册
<Python数据分析常用手册>一.NumPy和Pandas篇 一.常用链接: 1.Python官网:https://www.python.org/2.各种库的whl离线安装包:http: ...