题目描述

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.

题目大意:

先是输入样例的组数,对于每组样例,先输入三个整数u,v,m,下面一行输入u个名称,其下一行输入v个名称,然后是m组说服关系,每行输入两个名称s1,s2,若u中的一个人能够说服v中的人投票给Dylan,则将v中此人记录下来,最后将答案按照字典序输出,同时有一点需注意,若假如某人即在u中又在v中,则他肯定能说服自己。

解题思路:

我们可以根据m行说服关系建一个有向图(树),不难发现,若以u中的一个人为根节点,那么他肯定能说服在以他为根的路径上的所有在v中的人都能被他说服投票给Dylan,所以可以通过dfs寻找这样的人,把人名存入set中,最后输出即可。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
map<string,int> m;
bool vis[200100];
set<string> ss;
vector<int> mapp[400100];
map<int,string> str;
int l,r;
void dfs(int x) {if(mapp[x].size()==0) return;for(int i=0;i<mapp[x].size();i++) {if(vis[mapp[x][i]]==false) {
/*          cout<<str[x]<<" "<<str[mapp[x][i]]<<endl;
*/          vis[mapp[x][i]]=true;if(mapp[x][i]>=l&&mapp[x][i]<=r) ss.insert(str[mapp[x][i]]);dfs(mapp[x][i]);}}}
int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;cin>>t;while(t--) {int u,v,mm;rep(i,1,40000) mapp[i].clear();string s;m.clear();memset(vis,false,sizeof vis);ss.clear();int cnt=0;cin>>u>>v>>mm;rep(i,1,u) {cin>>s;cnt++;m[s]=cnt;str[cnt]=s;}l=cnt+1;rep(i,1,v) {cin>>s;if(m[s]!=0) {ss.insert(s);}else {cnt++;m[s]=cnt;str[cnt]=s;}}r=cnt;string s1,s2;rep(i,1,mm) {cin>>s1>>s2;if(m[s1]==0) {cnt++;m[s1]=cnt;str[cnt]=s1;}if(m[s2]==0) {cnt++;m[s2]=cnt;str[cnt]=s2;}mapp[m[s1]].push_back(m[s2]);}rep(i,1,u) {if(vis[i]==false) {vis[i]=true;dfs(i);}}set<string>::iterator it1;for(it1=ss.begin();it1!=ss.end();it1++) cout<<*it1<<" ";cout<<endl;}return 0;
}

【dfs】Election of Evil相关推荐

  1. Bailian2815 城堡问题【DFS】

    2815:城堡问题 总时间限制: 1000ms 内存限制: 65536kB 描述 1 2 3 4 5 6 7 ############################# 1 # | # | # | | ...

  2. Bailian2816 红与黑【DFS】

    2816:红与黑 总时间限制: 1000ms 内存限制: 65536kB 描述 有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相邻的黑色瓷砖移动.请写一 ...

  3. NUC1158 Lake Counting【DFS】

    Lake Counting 时间限制: 1000ms 内存限制: 65536KB 通过次数: 1总提交次数: 1 问题描述 Due to recent rains, water has pooled ...

  4. NUC1399 Sum It Up【DFS】

    Sum It Up 时间限制: 1000ms 内存限制: 65535KB 通过次数: 1总提交次数: 1 问题描述 Given a specified total t and a list of n ...

  5. HDU1181 变形课【DFS】(废除)

    新题解参见:HDU1181 变形课[DFS+关系闭包+bitset] 变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 13107 ...

  6. 【DFS】巧妙取量的倒油问题

    题目描述 [题目描述]  有三个容器,容量分别为 a,b,c(a> b > c ),一开始a装满油,现在问是否只靠abc三个容器量出k升油.如果能就输出"yes",并且 ...

  7. [kuangbin]专题三 Dancing Links Squiggly Sudoku HDU - 4069【DFS】【精确覆盖】

    [题目描述] Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that each ...

  8. 【DFS】不撞南墙不回头—深度优先搜索算法[Deep First Search]

    今天上午听到,那个非常6+1的李咏先生因癌症去世 DFS算法的基本模型 深度下,不撞南墙不回头,就是一直往后找,知道没有路了,向后返回. 想起一首民谣,<可能否>--木小雅 https:/ ...

  9. NUC1333 Knight Moves【DFS】

    Knight Moves 时间限制: 1000ms 内存限制: 65535KB 问题描述 A friend of you is doing research on the Traveling Knig ...

最新文章

  1. 修改ActiveProcessLinks链表隐藏进程
  2. Socket编程之简单介绍
  3. 【Win32汇编】复制字符串
  4. yaf mysql_Yaf框架的配置
  5. 基于单样本单统计推断-假设检验
  6. python控制流教程_Python入门教程之运算符与控制流
  7. Windows下的SQL Server备份文件BAK在Linux环境下还原遇到的问题
  8. 调试错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
  9. UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xb4 in position 176: in xxxx
  10. OpenCasCade将鼠标点映射到OCC三维视图中的三维点(鼠标点转换为OCC三维坐标)
  11. 16qam matlab 误码率,16qam的误码率公式
  12. 【笔记】感谢《梦幻模拟战》的Unity+Spine资源,同人模式开启
  13. 2021-6-26 激光的工业应用
  14. 新版标准日本语初级_第四十八课
  15. 网页上腾讯视频下载mp4格式到本地
  16. java exchange 日历_如何通过EWS-API 获取所有会议室的日历信息
  17. 一文带你吃透汉诺塔和其变形题
  18. raft协议--面试问答题
  19. PowerPoint2007无法将Excel图表转换为图形对象
  20. 应用在智能触摸遥控器中的触摸芯片

热门文章

  1. C++中关于隐藏的理解
  2. c语言变长参数 第一个参数必须吗,一种使用变长参数为C程序构造灵活回调函数的方法...
  3. opensips mysql 认证_基于ubuntu中使用mysql实现opensips用户认证的解决方法
  4. labuladong 的算法小抄_来自GitHub 68.8k star的硬核算法教程
  5. c语言截图代码,截图代码 哪位大神帮我找一下错,截出来的图是这样子的
  6. java https soap,Java Https Soap Server(Tomcat-Axis2)
  7. log4j日志 linux配置,Log4j 日志详细用法
  8. java赋值运算符_11.Java赋值运算符
  9. else应输入一个语句是什么意思_Python学习基础篇 -4: Python中的转弯---分支语句
  10. 查找空目录Linux,Linux中find批量删除空文件及空文件夹脚本