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

  1. 2
  2. 2 1 4
  3. BoyA 1 GirlC
  4. BoyB 1 GirlC
  5. GirlC 1 BoyA
  6. 2 BoyA BoyB
  7. 2 BoyA GirlC
  8. 2 BoyB GirlC
  9. 3 BoyA BoyB GirlC
  10. 2 2 2
  11. H 2 O S
  12. He 0
  13. O 1 H
  14. S 1 H
  15. 3 H O S
  16. 4 H He O S

Sample Output

  1. 0
  2. 0
  3. 1 BoyB
  4. 0
  5.  
  6. 0
  7. 0
  8.  
  9. 看的人家的代码算是理解了,我是直接比较的明显要超时(n^2),换个思路后时间复杂度变为(2*n)代码:
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. #include<map>
  5. #include<set>
  6. using namespace std;
  7. char s1[],s2[],s3[];
  8. int main()
  9. {
  10. int T;
  11. int n,m,q,n0;
  12. scanf("%d",&T);
  13. while(T--)
  14. {
  15. string lovy,lovyed;
  16. map< pair<string,string> ,int >love;
  17. love.clear();
  18. scanf("%d %d %d",&n,&m,&q);
  19. lovy = lovyed = "";
  20. while(n--)
  21. {
  22. scanf("%s %d",s1,&n0);
  23. lovy = s1;
  24. while(n0--)
  25. {
  26. scanf("%s",s2);
  27. lovyed = s2;
  28. love[make_pair(lovy,lovyed)] = ;
  29. }
  30. lovy = lovyed = "";
  31. }
  32.  
  33. while(m--)
  34. {
  35. scanf("%s %d",s1,&n0);
  36. lovy = s1;
  37. while(n0--)
  38. {
  39. scanf("%s",s2);
  40. lovyed = s2;
  41. love[make_pair(lovy,lovyed)] = ;
  42. }
  43. lovy = lovyed = "";
  44. }
  45. while(q--)
  46. { bool flag = true;
  47. int k =;
  48. string mm[];
  49. scanf("%d",&n0);
  50. scanf("%s",s1);
  51. mm[]=lovy = s1;
  52. for(int i=;i<n0;i++)
  53. {
  54. scanf("%s",s1);
  55. lovyed = mm[i] = s1;
  56. //关键在于下面的判断
  57. if(love[make_pair(lovy,lovyed)] == || love[make_pair(lovyed,lovy)] == )
  58. {
  59. lovy = mm[i];
  60. k = i;
  61. }
  62. }
  63. for(int j=;j<k;j++)
  64. {
  65. if(lovy != mm[j])
  66. {
  67. if(love[make_pair(lovy,mm[j])] == || love[make_pair(mm[j],lovy)] == )
  68. {
  69. flag = false;
  70. }
  71. }
  72. }
  73. if(flag)
  74. printf("1 %s\n",lovy.c_str());
  75. else printf("0\n");
  76. }
  77. printf("\n");
  78. }
  79. return ;
  80. }

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. centos启动流程[转]

    启动流程概览 在硬件驱动成功后,Kernel 会主动呼叫 init 程序,而 init 会取得 run-level 资讯: init 运行 /etc/rc.d/rc.sysinit 文件来准备软件运行 ...

  2. python文件操作--字符串替换

    如把test.txt文件的 所有 AAA 字符串 替换成 aaaaa with open('test.txt','+r') as f: t = f.read() t = d.replace('AAA' ...

  3. C# 正则表达式类 Match类和Group类

    @"\b(\S+)://(\S+)\b"; //匹配URL的模式foreach (Match match in mc){ Console.WriteLine(match.Value ...

  4. background-position 用法详细介绍

    语法: background-position : length || length background-position : position || position 取值: length  : ...

  5. easyui datagrid 添删改(纪录)

    var groups;//用户组为另一个表,所以需先加载,用于编辑时下拉框 var editindex = undefined; var action; $(function () { $.ajax( ...

  6. 非root模式下安装mysql php小记

    假设你的home目录为/home/work mysql-server 安装 1. 下载mysql.tar.gz wget http://dev.mysql.com/get/Downloads/MySQ ...

  7. js 触摸类库

    A javascript library for multi-touch gestures :// You can touch this  http://hammerjs.github.io     ...

  8. 【转】C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值(1)

    C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值(1) #region 截获消息        /// 截获消息  处理XP不能关机问题        protected ...

  9. Java中的定时器Timer

    java.util.Timer是一个实用工具类,该类用来调度一个线程,使线程可以在将来某一时刻开始执行. Java的Timer类可以调度一个线程运行一次,或定期运行. java.util.TimerT ...

  10. 导入和导出表空间:复制文件比mysqldump&restore快

    #生成测试数据server1:mysql> CREATE TABLE test (c1 INT PRIMARY KEY)engine = innodb;Query OK, 0 rows affe ...