ZOJ 3601 Unrequited Love

来源:互联网 发布:单片机解密公司 编辑:程序博客网 时间:2024/05/13 12:21

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
22 1 4BoyA 1 GirlCBoyB 1 GirlCGirlC 1 BoyA2 BoyA BoyB2 BoyA GirlC2 BoyB GirlC3 BoyA BoyB GirlC2 2 2H 2 O SHe 0O 1 HS 1 H3 H O S4 H He O S
Sample Output
001 BoyB000

用map可以轻松搞定
#include<map>#include<cmath>    #include<queue> #include<string>#include<vector>#include<cstdio>    #include<cstring>    #include<algorithm>    using namespace std;#define ms(x,y) memset(x,y,sizeof(x))    #define rep(i,j,k) for(int i=j;i<=k;i++)    #define per(i,j,k) for(int i=j;i>=k;i--)    #define loop(i,j,k) for (int i=j;i!=-1;i=k[i])    #define inone(x) scanf("%d",&x)    #define intwo(x,y) scanf("%d%d",&x,&y)    #define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)    typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 1e5 + 10;int T, n, m, x, a[N];char s[N], q[N][25];map<string, int> M;map<pair<int, int>, bool> G;int main(){for (inone(T); T--;){intwo(n, m); n += m; inone(m);M.clear(); G.clear();int cnt = 0;rep(i, 1, n){scanf("%s%d", s, &x);if (!M[s]) M[s] = ++cnt, memcpy(q[cnt], s, sizeof(q[cnt]));int k = M[s];rep(j, 1, x){scanf("%s", s);if (!M[s]) M[s] = ++cnt, memcpy(q[cnt], s, sizeof(q[cnt]));G[make_pair(k, M[s])] = true;}}rep(i, 1, m){inone(x);int k = 1;rep(j, 1, x){scanf("%s", s);a[j] = M[s];if (k == j || (G[make_pair(a[k], a[j])] && !G[make_pair(a[j], a[k])])) continue;k = j;}rep(j, 1, x){if (k == j || (G[make_pair(a[k], a[j])] && !G[make_pair(a[j], a[k])])) continue;k = 0; break;}if (k) printf("1 %s\n", q[a[k]]); else puts("0");}putchar(10);}return 0;}

0 0