Election of Evil(搜索)

题目描述

Dylan is a corrupt politician trying to steal an election. He has already used a mind-control technique to enslave some set U of government representatives. However, the representatives who will be choosing the winner of the election is a different set V . Dylan is hoping that he does not need to use his mind-control device again, so he is wondering which representatives from V can be convinced to vote for him by representatives from U.
Luckily, representatives can be persuasive people. You have a list of pairs (A, B) of represenatives, which indicate that A can convice B to vote for Dylan. These can work in chains; for instance, if Dylan has mind-controlled A, A can convince B, and B can convince C, then A can effectively convince C as well.

输入

The first line contains a single integer T (1 ≤ T ≤ 10), the number of test cases. The first line of each test case contains three space-separated integers, u, v, and m (1 ≤ u, v, m ≤ 10,000). The second line contains a space-separated list of the u names of representatives in U. The third line contains a space-separated list of the v names of representatives from V . Each of the next m lines contains a pair of the form A B, where A and B are names of two representatives such that A can convince B to vote for Dylan. Names are strings of length between 1 and 10 that only consists of lowercase letters (a to z).

输出

For each test case, output a space-separated list of the names of representatives from T who can be convinced to vote for Dylan via a chain from S, in alphabetical order.

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
map<string,ll>mp;
string str1[1008611],str2[1008611];
string ch1,ch2,ans[1008611];
vector<ll>ve[1008611];
ll vis[1008611];
void dfs(ll x){
    for(ll i=0;i<ve[x].size();i++){
        if(!vis[ve[x][i]]){
            vis[ve[x][i]]=1;
            dfs(ve[x][i]);
        }
    }
}
int main(){
    ll t,u,v,m,loc,x;
    scanf("%lld",&t);
    while(t--){
        memset(vis, 0, sizeof(vis));
        mp.clear();
        for(int i=0;i<10086;i++){
            ve[i].clear();
        }
        loc=0;
        scanf("%lld%lld%lld",&u,&v,&m);
        for(int i=0;i<u;i++){
            cin>>str1[i];
            if(!mp[str1[i]]){
                mp[str1[i]]=loc++;
            }
        }
        for(int i=0;i<v;i++){
            cin>>str2[i];
            if(!mp[str2[i]]){
                mp[str2[i]]=loc++;
            }
        }
        for(int i=0;i<m;i++){
            cin>>ch1>>ch2;
            if(!mp[ch1]){
                mp[ch1]=loc++;
            }
            if(!mp[ch2]){
                mp[ch2]=loc++;
            }
            ve[mp[ch1]].push_back(mp[ch2]);
        }
        for(int i=0;i<u;i++){
             x=mp[str1[i]];
            dfs(x);
        }
        for(int i=0;i<u;i++){
            vis[mp[str1[i]]]=1;;
        }
       ll k=0;
        for(int i=0;i<v;i++){
            x=mp[str2[i]];
            if(vis[x]){
                ans[k++]=str2[i];
            }
        }
        sort(ans, ans+k);
        for(int i=0;i<k;i++){
            if(i==0){
                cout<<ans[i];
            }
            else cout<<' '<<ans[i];
        }
        cout<<endl;
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/xiao_you_you/article/details/89300281