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);
}

upc组队赛5 Election of Evil【搜索】的更多相关文章

  1. Stanford Local 2016 E "Election of Evil"(搜索(正解)或并查集(划掉))

    传送门 题意: 给出集合U,V,集合U有n个元素,集合V有m个元素: 有 m 个操作,mi : s1 s2 有一条s1指向s2的边(s1,s2可能属于第三个集合,暂且称之为K集合): 指向边具有传递性 ...

  2. upc组队赛5 Hunter’s Apprentice 【判断多边形边界曲线顺逆时针】

    Hunter's Apprentice 题目描述 When you were five years old, you watched in horror as a spiked devil murde ...

  3. upc组队赛3 Chaarshanbegaan at Cafebazaar

    Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...

  4. upc组队赛1 过分的谜题【找规律】

    过分的谜题 题目描述 2060年是云南中医学院的百年校庆,于是学生会的同学们搞了一个连续猜谜活动:共有10个谜题,现在告诉所有人第一个谜题,每个谜题的答案就是下一个谜题的线索....成功破解最后一个谜 ...

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

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

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

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

  7. upc组队赛1 闪闪发光 【优先队列】

    闪闪发光 题目描述 一所位于云南昆明的中医药本科院校--云南中医学院. 因为报考某专业的人数骤减,正面临着停招的危机. 其中有九名少女想到一条妙计--成为偶像, 只要她们成为偶像,学校的名气便会增加, ...

  8. upc组队赛1 流连人间的苏苏

    流连人间的苏苏 题目描述 苏苏在做红尘仙的任务时,发现坐落于风景秀丽.四季如春的昆明市的云南中医学院. 没过多久,苏苏就喜欢上了这个学校.以致于苏苏忘了回涂山的时间,现在她只剩下d天的时间待在云南中医 ...

  9. upc组队赛18 THE WORLD【时间模拟】

    THE WORLD 题目链接 题目描述 The World can indicate world travel, particularly on a large scale. You mau be l ...

随机推荐

  1. BootStrap 轮播插件(carousel)支持左右手势滑动的方法(三种)

    原生的 Bootstrap 的 carousel.js 插件并没有支持手势,有下面3种解决方案 : 1. jQuery Mobile (http://jquerymobile.com/download ...

  2. JavaScript Stack

    function Stack() { var items = []; this.push = function(item) { items.push(item) } this.pop = functi ...

  3. 通过URL传参数,然后第二个页面需要获取参数

    /** * 方法说明:通过url参数键值名称获取参数的值 * @method getQueryString * @param name 要获取的参数键值 * @return * @remark */ ...

  4. 55.Top K Frequent Elements(出现次数最多的k个元素)

    Level:   Medium 题目描述: Given a non-empty array of integers, return the k most frequent elements. Exam ...

  5. 浅析Mysql事务传播行为

    传播行为 1.PROPAGATION_REQUIRED:如果当前没有事务,就创建一个新事务,如果当前存在事务,就加入该事务,该设置是最常用的设置. 2.PROPAGATION_SUPPORTS:支持当 ...

  6. 记一次Xshell配置ssh免密登录时的问题

    问题: 今天在配置SSH免密登录连接自己的阿里云服务器,在将RSA加密生成的公钥放到服务器后,用Xshell连接服务,出现所选的用户密钥未在远程主机上注册这样的提示,一时懵逼,不知所措,后面终于找到了 ...

  7. css识别空格回车符

    新闻发布系统文字需要换行,后台返回数据包含空格.回车符号需要默认显示出来.在父元素加上css样式 :white-space:pre:即可. 例:<div class="white-sp ...

  8. MAC如何与linux服务器传递文件

    scp命令可以从本地拷贝文件到Linux服务器,也可以将Linux服务器文件下载到本地 将远程/root/articaleFetch/dist目录下文件和文件夹拷贝到dist文件夹 scp root@ ...

  9. 2018-2-13-win10-uwp-绑定密码

    title author date CreateTime categories win10 uwp 绑定密码 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...

  10. 转载:tomcat过程原理

    基于Java的Web 应用程序是 servlet.JSP 页面.静态页面.类和其他资源的集合,它们可以用标准方式打包,并运行在来自多个供应商的多个容器.Web 应用程序存在于结构化层次结构的目录中,该 ...