Gym - 101116E Election of Evil 【STL+dfs】


传送门:Election of Evil


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.

Input
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).

Output
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.

Simple Input
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

Simple Output
bob
peter saul



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


题解:
以u中的一个人为根节点,那么他肯定能说服在以他为根的路径上的所有在v中的人都能被他说服投票给Dylan,所以可以通过dfs寻找这样的人,把人名存入set中,最后输出即可。



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=11000;
int t,u,v,m;
string s,ss;
set<string> ans;
map<string,int> UV;
map<int,string> mp;
vector<int> G[4*N];
int vis[4*N];
void dfs(int x)
{
  int len=G[x].size();
  for(int i=0;i<len;i++)
  {
    if(!vis[G[x][i]])
    {
      vis[G[x][i]]=1;
      dfs(G[x][i]);
    }
  }
  return;
}
int main()
{
  cin>>t;
  while(t--)
  {
    UV.clear();
    ans.clear();
    mp.clear();
    for(int i=0;i<4*N;i++) G[i].clear();
    memset(vis,0,sizeof(vis));
    cin>>u>>v>>m;
    int cnt=1;
    for(int i=0;i<u;i++)//输入集合U
    {
      cin>>s;
      UV[s]=cnt;
      mp[cnt]=s;
      cnt++;
    }
    int l=cnt;//U集合的末尾,V集合的开始
    for(int i=0;i<v;i++)//输入集合V
    {
      cin>>s;
      if(UV[s]!=0) ans.insert(s);//集合V中元素在U中出现了,则可以说服
      else
      {
        UV[s]=cnt;
        mp[cnt]=s;
        cnt++;
      }
    }
    int r=cnt;//V集合的末尾
    for(int i=0;i<m;i++)
    {
      cin>>s>>ss;
      if(UV[s]==0)//UV集合中没有出现的人物,也加入集合
      {
        UV[s]=cnt;
        mp[cnt]=s;
        cnt++;
      }
      if(UV[ss]==0)
      {
        UV[ss]=cnt;
        mp[cnt]=ss;
        cnt++;
      }
      G[UV[s]].push_back(UV[ss]);//建图
    }
    for(int i=1;i<=u;i++)//遍历集合U
    {
      if(!vis[i])
      {
        vis[i]=1;
        dfs(i);
      }
    }
    for(int i=l;i<r;i++)
    {
      if(vis[i]) ans.insert(mp[i]);
    }
    set<string>::iterator it;
    for(it=ans.begin();it!=ans.end();it++)
    {
      cout<<*it<<" ";
    }
    cout<<endl;
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值