Unrequited Love ZOJ - 3601

点击打开链接

一个聚会中 存在某个人 其所有喜欢的人都出现了 且不被所有出现的人喜欢

其实这样的人最多只能有一个 对于题目所给的每一个集合 从前往后扫一遍 找出一个符合条件的 再针对其判断一遍

从前往后 是判断每个人与之后的人的关系 只有之后的满足了 才需要判断之前的

#include <bits/stdc++.h>
using namespace std;

map <string,int> mp;
set <string> st[30010];
string str[30010];
int n,m;

int main()
{
    string s;
    int t,q,l,i,j,p,flag;
    scanf("%d",&t);
    while(t--)
    {
        cin>>n>>m>>q;
        mp.clear();
        for(i=1;i<=n+m;i++) st[i].clear();
        for(i=1;i<=n+m;i++)
        {
            cin>>s>>l;
            mp[s]=i;
            for(j=1;j<=l;j++)
            {
                cin>>s;
                st[i].insert(s);
            }
        }
        while(q--)
        {
            cin>>l;
            p=0;
            for(i=1;i<=l;i++)
            {
                cin>>str[i];
                if(p==0) p=1;
                else
                {
                    if(!st[mp[str[p]]].count(str[i])||st[mp[str[i]]].count(str[p]))
                    {
                        p=i;
                    }
                }
            }
            flag=1;
            for(i=1;i<=p-1;i++)
            {
                if(!st[mp[str[p]]].count(str[i])||st[mp[str[i]]].count(str[p]))
                {
                    flag=0;
                    break;
                }
            }
            if(flag)
            {
                cout<<1<<" "<<str[p]<<endl;
            }
            else
            {
                cout<<0<<endl;
            }
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sunyutian1998/article/details/80077596
ZOJ