B - Unrequited Love ZOJ - 3601
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
0 0 1 BoyB 0 0 0
AC代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<climits>
using namespace std;
const int maxn=30090;
map<string,int> mp;
set<int> s[maxn];
int sum,n,m,q,t;
string x[maxn];
int get(string ch)
{
if(!mp.count(ch))
mp[ch]=sum++;
return mp[ch];
}
int main()
{
scanf("%d",&t);
int i,j;
while(t--)
{
scanf("%d%d%d",&n,&m,&q);
mp.clear();//清零
for(i=0;i<maxn;i++)
s[i].clear();//清零
string ch;
int a,k;
sum=0;
for(i=0;i<n+m;i++)
{
cin>>ch>>a;
k=get(ch);
for(j=0;j<a;j++)
{
cin>>ch;
s[k].insert(get(ch));
}
}
while(q--)
{
int o;
scanf("%d",&o);
for(i=0;i<o;i++)
cin>>x[i];
string p=x[0];
k=0;
for(i=1;i<o;i++)
{
if(!s[get(x[k])].count(get(x[i]))||s[get(x[i])].count(get(x[k])))
{
k=i;
p=x[i];
}
}
for(i=0;i<k;i++)
{
if(!s[get(x[k])].count(get(x[i]))||s[get(x[i])].count(get(x[k])))
{
p=" ";
break;
}
}
if(p==" ")
printf("0\n");
else
cout<<1<<" "<<p<<endl;
}
printf("\n");
}
return 0;
}
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<climits>
using namespace std;
const int maxn=30090;
map<string,int> mp;
set<int> s[maxn];
int sum,n,m,q,t;
string x[maxn];
int get(string ch)
{
if(!mp.count(ch))
mp[ch]=sum++;
return mp[ch];
}
int main()
{
scanf("%d",&t);
int i,j;
while(t--)
{
scanf("%d%d%d",&n,&m,&q);
mp.clear();//清零
for(i=0;i<maxn;i++)
s[i].clear();//清零
string ch;
int a,k;
sum=0;
for(i=0;i<n+m;i++)
{
cin>>ch>>a;
k=get(ch);
for(j=0;j<a;j++)
{
cin>>ch;
s[k].insert(get(ch));
}
}
while(q--)
{
int o;
scanf("%d",&o);
for(i=0;i<o;i++)
cin>>x[i];
string p=x[0];
k=0;
for(i=1;i<o;i++)
{
if(!s[get(x[k])].count(get(x[i]))||s[get(x[i])].count(get(x[k])))
{
k=i;
p=x[i];
}
}
for(i=0;i<k;i++)
{
if(!s[get(x[k])].count(get(x[i]))||s[get(x[i])].count(get(x[k])))
{
p=" ";
break;
}
}
if(p==" ")
printf("0\n");
else
cout<<1<<" "<<p<<endl;
}
printf("\n");
}
return 0;
}