Election of Evil 【STL+DFS】

版权声明:本文为博主原创文章,转载请注明出处( • ̀ω•́ )✧ https://blog.csdn.net/wangws_sb/article/details/89281434

题目传送门

题目描述

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.

样例输入

复制样例数据

2
1 1 1
alice
bob
alice bob
5 5 5
adam bob joe jill peter
rob peter nicole eve saul
harry ron
eve adam
joe chris
jill jack
jack saul

样例输出

bob
peter saul

提示

In the second test case, Jill can convince Saul via Jack, and Peter was already mind-controlled.

[提交][状态]

题目大意:T组测试数据,一个集合U中有u个人,另一个集合V中有v个人,然后又m个说服关系。最后按字典序输出V集合中能被U集合中的人说服为Dylan投票的人。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define io ios::sync_with_stdio(0),cin.tie(0)
#define ms(arr) memset(arr,0,sizeof(arr))
#define mc(a,b) memcpy(a,b,sizeof(b))
#define inf 0x3f3f3f
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
typedef long long ll;
typedef unsigned long long ULL;
const int mod=1e9+7;
const int N=1e5+7;
int T,u,v,m;
map <string,int> m1;
map <int,string> m2;
set<string> q;
vector <int> g[N*4];
int vis[N*2];
void init()
{
    m1.clear();
    m2.clear();
    q.clear();
    ms(vis);
    for(int i=0; i<=N*4; i++)
        g[i].clear();
}
void dfs(int x)
{
    for(int i=0; i<g[x].size(); i++)
    {
        if(vis[g[x][i]]==0)
        {
            vis[g[x][i]]=1;
            dfs(g[x][i]);
        }
    }
    return ;
}
int main()
{
    io;
    string s,t;
    int cnt=0;
    cin>>T;
    while(T--)
    {
        init();
        cin>>u>>v>>m;
        cnt=1;
        for(int i=0; i<u; i++)
        {
            cin>>s;
            m1[s]=cnt;
            m2[cnt]=s;
            cnt++;
        }
        int l=cnt;
        for(int i=0; i<v; i++)
        {
            cin>>s;
            if(m1[s]!=0) q.insert(s);
            else
            {
                m1[s]=cnt;
                m2[cnt]=s;
                cnt++;
            }
        }
        int r=cnt;
        for(int i=0; i<m; i++)
        {
            cin>>s>>t;
            if(m1[s]==0)
            {
                m1[s]=cnt;
                m2[cnt]=s;
                cnt++;
            }
            if(m1[t]==0)
            {
                m1[t]=cnt;
                m2[cnt]=s;
                cnt++;
            }
            g[m1[s]].push_back(m1[t]);
        }
        for(int i=1; i<=u; i++)
        {
            if(vis[i]==0)
            {
                vis[i]=1;
                dfs(i);
            }
        }
        for(int i=l; i<r; i++)
            if(vis[i]==1) q.insert(  m2[i]);
        set <string>:: iterator it;
        for(it=q.begin(); it!=q.end(); it++)
        {
            cout<<*it<<" ";
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wangws_sb/article/details/89281434