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 ...
随机推荐
- Android自动提示控件:AutoCompleteTextView和MultiAutoCompleteTextView
在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextView实现的. 一.AutoCompleteTextView:单一匹配 ...
- android 百度天气接口
百度天气接口 之前有篇随笔是说中国气象的天气接口的,但那个接口不是很稳定,用一段时间过后就会获取不到数据了. 随笔地址:http://www.cnblogs.com/liangstudyhome/p/ ...
- .net core MVC 通过 Filters 过滤器拦截请求及响应内容
前提: 需要nuget Microsoft.Extensions.Logging.Log4Net.AspNetCore 2.2.6: Swashbuckle.AspNetCore 我暂时用的是 ...
- Javascript中对数组处理的函数汇总
数组类型是JS中非常常见的类型,而且JS里的数组与其他多数语言中的数组有所不同,它的数组里每一项可以存放任何一种类型的数据,也就是说数组的第一项放的是字符串,第二项可以放数字或对象都没问题.而且JS中 ...
- RDD与DataFrame
RDD:分布式对象集合: 解决了:减少中间计算结果处理所需的开销 数据存在内存,提供一个通用的抽象的数据结构 惰性求值 DataFrame:分布式Row对象集合 服务于:SparkSQL
- Windows系统如何安装Redis?
转自 http://blog.csdn.net/lamp_yang_3533/article/details/52024744 一.Redis的下载地址 Redis官方并没有提供Redis的win ...
- PHP选项和运行
PHP运行模式 五大运行模式 1.cgi 通用网关接口 2.fast-cgi cgi升级 3.cli (Command Line Interface) 4.isapi 微软提供的面向Internet服 ...
- SDUT OJ 数据结构实验之图论四:迷宫探索
数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- (WIN7)delphi7运行关闭出现cannot creat file"C:\program file\borland\delphi7\bin\initcc32.exe拒绝访问
1. 程序右键,以管理权限运行.2. 将Delphi7目录只读取消.WIN7以上的操作系统正常运行程序不是以管理员方式运行的
- JavaWeb学习笔记(十)—— JavaBean总结【转】
一.什么是JavaBean JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个无参的构造函数 属性必须私有化. 私有化的属性必须通过public类型的方法 ...