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 看的人家的代码算是理解了,我是直接比较的明显要超时(n^2),换个思路后时间复杂度变为(2*n)代码:
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
#include<set>
using namespace std;
char s1[],s2[],s3[];
int main()
{
int T;
int n,m,q,n0;
scanf("%d",&T);
while(T--)
{
string lovy,lovyed;
map< pair<string,string> ,int >love;
love.clear();
scanf("%d %d %d",&n,&m,&q);
lovy = lovyed = "";
while(n--)
{
scanf("%s %d",s1,&n0);
lovy = s1;
while(n0--)
{
scanf("%s",s2);
lovyed = s2;
love[make_pair(lovy,lovyed)] = ;
}
lovy = lovyed = "";
} while(m--)
{
scanf("%s %d",s1,&n0);
lovy = s1;
while(n0--)
{
scanf("%s",s2);
lovyed = s2;
love[make_pair(lovy,lovyed)] = ;
}
lovy = lovyed = "";
}
while(q--)
{ bool flag = true;
int k =;
string mm[];
scanf("%d",&n0);
scanf("%s",s1);
mm[]=lovy = s1;
for(int i=;i<n0;i++)
{
scanf("%s",s1);
lovyed = mm[i] = s1;
//关键在于下面的判断
if(love[make_pair(lovy,lovyed)] == || love[make_pair(lovyed,lovy)] == )
{
lovy = mm[i];
k = i;
}
}
for(int j=;j<k;j++)
{
if(lovy != mm[j])
{
if(love[make_pair(lovy,mm[j])] == || love[make_pair(mm[j],lovy)] == )
{
flag = false;
}
}
}
if(flag)
printf("1 %s\n",lovy.c_str());
else printf("0\n");
}
printf("\n");
}
return ;
}

ZOJ 3601 Unrequited Love 浙江省第九届省赛的更多相关文章

  1. ZOJ 3606 Lazy Salesgirl 浙江省第九届省赛

    Lazy Salesgirl Time Limit: 5 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who ma ...

  2. nyoj1273 河南省第九届省赛_"宣传墙"、状压DP+矩阵幂加速

    宣传墙 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 ALPHA 小镇风景美丽,道路整齐,干净,到此旅游的游客特别多.CBA 镇长准备在一条道路南 面 4*N 的墙上做 ...

  3. NYOJ 1272 表达式求值 第九届省赛 (字符串处理)

    title: 表达式求值 第九届省赛 nyoj 1272 tags: [栈,数据结构] 题目链接 描述 假设表达式定义为: 1. 一个十进制的正整数 X 是一个表达式. 2. 如果 X 和 Y 是 表 ...

  4. 河南省acm第九届省赛--《表达式求值》--栈和后缀表达式的变形--手速题

    表达式求值 时间限制:1000 ms | 内存限制:65535 KB 难度:3   描述 假设表达式定义为:1. 一个十进制的正整数 X 是一个表达式.2. 如果 X 和 Y 是 表达式,则 X+Y, ...

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

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

  6. NYOJ--1276--机器设备(河南省第九届省赛,简单的bfs)

    机器设备 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Alpha 公司设计出一种节能的机器设备.它的内部结构是由 N 个齿轮组成.整个机器设备有 一个驱动齿轮,当 ...

  7. SD第九届省赛B题 Bullet

    Bullet Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Description In G ...

  8. 蓝桥杯第九届省赛 sscanf(),str.c_str()函数的使用

    标题:航班时间 [问题背景]小h前往美国参加了蓝桥杯国际赛.小h的女朋友发现小h上午十点出发,上午十二点到达美国,于是感叹到“现在飞机飞得真快,两小时就能到美国了”. 小h对超音速飞行感到十分恐惧.仔 ...

  9. 【二分图最大匹配】Bullet @山东省第九届省赛 B

    时间限制: 6 Sec 内存限制: 128 MB 题目描述 In GGO, a world dominated by gun and steel, players are fighting for t ...

随机推荐

  1. poj3259 最短路判环

    题意:有一些点.一些道路和一些虫洞,道路是双向的,连接两点,花费正的时间,而虫洞是单向的,连接两点,可以使时间倒退,求是否能够回到过去. 只要明确回到过去其实就是当出现一个负环的时候,不断沿这个环走, ...

  2. hdu1158 dp经典题

    题意:已知雇佣员工花费(h).解雇员工花费(f).员工每月薪水(s),员工未被解雇的话即使未工作也要付薪水,现知道每个月需要几名员工,求最低花费. 很显然,刷 DP 专题的我早早地就意识到这是一道 D ...

  3. Java——java多态

     /* * 多态: * 成员的特点: * 1.成员变量. *  编译时:参开引用型变量所属类中的是否有调用的成员变量,  有:编译通过,  没有  编译失败. *  运行时: 参考引用变量所属的类 ...

  4. 两篇很牛的vim使用技巧

    读本文之前请注意: 1. 本文的目标是提供一些vim的使用技巧,利用这些技巧可以提高vim的操作效率.部分技巧在vi上也可以使用,但是现在基本上都是用vim了. 2. 本文是整理和总结使用技巧,而非讲 ...

  5. 用360安全浏览器控制网速,调试loading

    360安全浏览器 按f12 两个按钮的意思分别为禁止缓存,网络设置,这样就能控制网速了,调试loading了

  6. Questions?

    http://www.datastax.com/wp-content/themes/datastax-2014-08/files/NoSQL_Benchmarks_EndPoint.pdf http: ...

  7. QQ登入(2)获取用户信息

    private void initView() { mUserInfo = (TextView) findViewById(R.id.user_info); mUserLogo = (ImageVie ...

  8. C#→关于System.Data.Linq下的Table<TEntity> 泛型类 的问题

    Table<TEntity>表示表格记录,它是一个泛型集合类,它的元素就是表格实体对象.它提供一组方法,对元素进行添加删除操作,并可以通过DataContext将这些操作保存到数据库. 表 ...

  9. WCF入门教程二[WCF应用的通信过程]

    一.概述 WCF能够建立一个跨平台的安全.可信赖.事务性的解决方案,是一个WebService,.Net Remoting,Enterprise Service,WSE,MSMQ的并集,有一副很经典的 ...

  10. 二十四种设计模式:适配器模式(Adapter Pattern)

    适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...