zoj 4704 Unrequited Love 贪心 孤独者

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  

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4704

题意:n+m个人,各自喜欢num个人。  然后q个询问,每个询问给你num个人,问你在这num个人中,是否有个孤独者喜欢这num个人中的所有人,但是这num个人都没喜欢他。

做法:首先明确的是,这里只可能有一个人符合要求,否者 条件矛盾 。先假设这个人 是0号,从从小到大枚举 这num个人,假设第i个人是这个孤独者,那么i+j个人如果喜欢i,或者i不喜欢i+j,那么i就不符合条件,接下来就假设这个孤独者是i+j。i+j之前可以断定不是孤独者。 到最后就可以得到假设的孤独者,然后从0开始再循环一遍,判断他是否真是孤独者。然后就ok了。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>
using namespace std;
#define maxn 505

map<int,set<int> > my;
map<string,int> turn;

int ID(string& str)
{
	if(turn.count(str)==0)
		return turn[str]=turn.size();
	return turn[str];

}
string str[40000];
int main()
{
	int t;
	int i,j;
	int n,m,q;
	int num;
	cin>>t;
	while(t--)
	{
		my.clear();
		turn.clear();
		cin>>n>>m>>q;
		for(i=0;i<n+m;i++)
		{
			string a,b;;
			int n;
			int numa,numb;
			cin>>a;
			numa=ID(a);
			cin>>num;
			for(j=0;j<num;j++)
			{
				cin>>b;
				numb=ID(b);
				my[numa].insert(numb);
			}
		}

		for(i=0;i<q;i++)
		{
			string a,b,nw;
			cin>>num;
			int id=1;
			for(j=0;j<num;j++)
			{
				cin>>str[j];
				if(j==0)
				{
					nw=str[j];
					continue;
				}
				else
				{
					int num_nw=turn[nw];
					int num_a=turn[str[j]];
					if(my[num_nw].count(num_a)==0||my[num_a].count(num_nw))
					{
						nw=str[j];
					}
				}
			}

			int num_nw=turn[nw];
			for(j=0;j<num;j++)
			{
				if(str[j]==nw)
					continue;
				int num_a=turn[str[j]];
				if(my[num_nw].count(num_a)==0||my[num_a].count(num_nw))
				{
					nw="";
					break;
				}
			}
			if(nw=="")
				cout<<0<<endl;
			else
				cout<<1<<' '<<nw<<endl;
		}
		cout<<endl;
	}

	return 0;
}
时间: 2024-12-26 07:20:02

zoj 4704 Unrequited Love 贪心 孤独者的相关文章

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

zoj 3778 Talented Chef 贪心

zoj 3778 Talented Chef 题意: 有n个饼,给出完成每个饼所需要的时间t1,t2,...,tn,现在有m个锅(也就是说可以同时煎m个饼),问完成所有饼至少需要多少时间. 限制: 1 <= n,m,ti <= 40000 思路: 贪心 ans=max(ceil(sigma(1~n,ti)/m),max(ti)) /*zoj 3778 Talented Chef 题意: 有n个饼,给出完成每个饼所需要的时间t1,t2,...,tn,现在有m个锅(也就是说可以同时煎m个饼),问完

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc

ZOJ 2702 Unrhymable Rhymes 贪心

贪心,能凑成一组就算一组 Unrhymable Rhymes Time Limit: 10 Seconds      Memory Limit: 32768 KB      Special Judge An amateur poet Willy is going to write his first abstract poem. Since abstract art does not give much care to the meaning of the poem, Willy is plan

ZOJ 3689 Digging(贪心+dp)

Digging Time Limit: 2 Seconds      Memory Limit: 65536 KB When it comes to the Maya Civilization, we can quickly remind of a term called the end of the world. It's not difficult to understand why we choose to believe the prophecy (or we just assume i

ZOJ 1025. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some

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

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 wi

ZOJ 3829 Known Notation 贪心

主要的贪心思想就是,如果有一个不合法的*,那么再他前面加1或者2个数字的花费是不可能小于把它和后面的数字交换的,所以把不合法星号尽可能的往后放即可. 这里我因为懒得特判,把每个情况都算了,不过n只有1000,n^2也是可以接受的. #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <map&

POJ 1862 &amp; ZOJ 1543 Stripies(贪心 | 优先队列)

题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=543 Description Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian -