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.

样例输入

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.

题解

搜索大法好

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
typedef long long ll;
int n, m, k;
vector<string> q[maxn];
map<string, int> mp;
string s1[maxn];
string s[maxn];
string s2[maxn];
int cnt;
string a, b;
int vis1[maxn];
void dfs(string s)
{for (int i = 0; i < q[mp[s]].size(); i++){string t = q[mp[s]][i];if (vis1[mp[t]] == 0) {vis1[mp[t]] = 1;dfs(t);}}
}int main()
{int t;scanf("%d", &t);while (t--){cnt = 1;for (int i = 0; i < maxn; i++){q[i].clear();}memset(vis1, 0, sizeof(vis1));mp.clear();s1->clear();s2->clear();scanf("%d%d%d", &n, &m, &k);for (int i = 0; i < n; i++){cin >> s2[i];if (mp[s2[i]] == 0) {mp[s2[i]] = cnt++;}}for (int i = 0; i < m; i++){cin >> s1[i];if (mp[s1[i]] == 0) {mp[s1[i]] = cnt++;}}for (int i = 0; i < k; i++){cin >> a >> b;if (mp[a] == 0) {mp[a] = cnt++;}if (mp[b] == 0) {mp[b] = cnt++;}q[mp[a]].push_back(b);}for (int i = 0; i < n; i++){vis1[mp[s2[i]]] = 1;dfs(s2[i]);}int tot = 0;for (int i = 0; i < m; i++){if (vis1[mp[s1[i]]]) {s[tot++] = s1[i];}}sort(s, s + tot);for (int i = 0; i < tot; i++){cout << s[i] << (i == tot-1 ? '\n' : ' ');}}return (0);
}

转载于:https://www.cnblogs.com/llke/p/10800060.html

upc组队赛5 Election of Evil【搜索】相关推荐

  1. 【dfs】Election of Evil

    题目描述 Dylan is a corrupt politician trying to steal an election. He has already used a mind-control t ...

  2. upc组队赛1 小C的数学问题【单调栈】(POJ2796)

    小C的数学问题 题目描述 小C是个云南中医学院的大一新生,在某个星期二,他的高数老师扔给了他一个问题. 让他在1天的时间内给出答案. 但是小C不会这问题,现在他来请教你. 请你帮他解决这个问题. 有n ...

  3. upc组队赛5 Bulbs

    Bulbs 题目描述 Greg has an m × n grid of Sweet Lightbulbs of Pure Coolness he would like to turn on. Ini ...

  4. upc 组队赛18 STRENGTH【贪心模拟】

    STRENGTH 题目链接 题目描述 Strength gives you the confidence within yourself to overcome any fears, challeng ...

  5. upc组队赛15 Supreme Number【打表】

    Supreme Number 题目链接 题目描述 A prime number (or a prime) is a natural number greater than 1 that cannot ...

  6. upc组队赛16 GCDLCM 【Pollard_Rho大数质因数分解】

    GCDLCM 题目链接 题目描述 In FZU ACM team, BroterJ and Silchen are good friends, and they often play some int ...

  7. upc组队赛1 黑暗意志【stl-map】

    黑暗意志 题目描述 在数千年前潘达利亚从卡利姆多分离之时,迷雾笼罩着这块新形成的大陆,使它不被外来者发现.迷雾同样遮蔽着这片大陆古老邪恶的要塞--雷神的雷电王座.在雷神统治时期,他的要塞就是雷电之王力 ...

  8. upc组队赛1 不存在的泳池【GCD】

    不存在的泳池 题目描述 小w是云南中医学院的同学,有一天他看到了学校的百度百科介绍: 截止到2014年5月,云南中医学院图书馆纸本藏书74.8457万册,纸质期刊388种,馆藏线装古籍图书1.8万册, ...

  9. vscode不会自动生成html,vscode 快速生成html

    CoolTrayIcon4.0 CoolTrayIcon:在任务栏放置图标的控件,是同类空间中功能最为完善和强大的. 1.支持动态图标 2.交互式气球样式的提示框 3.支持bitmaps到icons的 ...

最新文章

  1. 网络推广外包——网络推广外包专员是如何发布软文外链的呢?
  2. python opencv 保存摄像头视频,以及fourc编码的介绍
  3. 【转】 Android新特性介绍,ConstraintLayout完全解析
  4. oracle ob 使用基础之基础
  5. 电动车式的爱情,有你有我有爱、青春不再,人生无悔!
  6. Epos消费管理系统复制迁移SQL SERVER 2005数据库
  7. 推荐时代的内容理解技术探索.pdf(附下载链接)
  8. 手机为什么取消了内存卡?
  9. (六)关于beetlsql版本(分支)的说明
  10. android 模拟器优化,Android模拟器大幅优化 为开发者谋福利
  11. usb计算机连接 不再弹出,电脑usb无法安全弹出问题解决办法
  12. LeetCode1156. 单字符重复子串的最大长度
  13. java获取指定日期当月和下个月的第一天
  14. 方差分析——单因素方差分析
  15. 真机测试无法验证应用
  16. zookeeper关闭默认管理端口admin.serverPort=8080
  17. intern string java_Java-String.intern的深入研究
  18. spacedesk安装失败2503/2502错误
  19. 【神经网络】人工神经网络学习方法
  20. python批量修改文件名称及文件属性

热门文章

  1. Redis笔记(六)Redis的消息通知
  2. Android -- 自动挂断电话
  3. 六大方法来对付网站出现的负面信息
  4. 来自 Dropbox 的可扩展性设计经验
  5. 屌丝也能开发安卓版2048(App Inventor)
  6. 开发者70行代码破解苹果OSX远程锁定安全功能
  7. 不吐不快之EJB演练——开篇概述
  8. SqlServer标识列、修改标识列值
  9. ELK-elasticsearch-kibana-logstash 报错问题集锦
  10. [转].net cookie版购物车