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. 关于vsftp出现Restarting vsftpd (via systemctl): Job for vsftpd.service failed because the control 的解决办法

    转载于:http://blog.csdn.net/it_dream_er/article/details/50783111 刚刚在搭建ftp服务器时,在配置好一切的参数之后,在我重启时,出现了无法启动 ...

  2. drf 分页,获取fk,choise,m2m等字段数据(序列化)

    1.什么是restful规范 是一套规则,用于程序之间进行数据交换的约定. 他规定了一些协议,对我们感受最直接的的是,以前写增删改查需要写4个接口,restful规范的就是1个接口,根据method的 ...

  3. HttpServletRequest 对文件上传的支持

    此前,对于处理上传文件的操作一直是让开发者头疼的问题,因为 Servlet 本身没有对此提供直接的支持,需要使用第三方框架来实现,而且使用起来也不够简单.Servlet 3.0 已经提供了这个功能,而 ...

  4. django 视图与网址

    我是一个新手,内容粗糙,望大家多多指点.在这里我只是总结自身所学. 视图与网址 操作文件:urls.py.views.py urls.py 作用:用于处理前台的链接(如前台访问:127.0.0.1:8 ...

  5. 模拟用户登录含注册——python第8天

    print('欢迎登录尚雅梦想python学习系统'.center(30)) print('******' * 8) flag = True while flag: order = input(''' ...

  6. 关于ExtJS对javascript中的Object的扩展

    关于ExtJS对javascript中的Object的扩展,可以参考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 下 ...

  7. soket通信

    import socket #socket采用cs结构,client and Server B/S browser and server sock = socket.socket(socket.AF_ ...

  8. vue 引入阿里图标

    1.去阿里图标矢量图标库将想要的图标添加入库 2.再去库中将图标添加到项目. 3.再到我的项目中,选择,我这里采用的是将图标代码包下载到本地再引入到vue项目中. 4.在vue项目的assets文件夹 ...

  9. Qt 在相同的线程中可以在信号中传递未注册的元对象,在非相同线程中则不能传递未测试的对象,为什么呢?

    有兄台知道可以在留言告诉我,万分感谢!!! 需求:需要在多线程中传递未注册的非元对象数据,时间紧急,无法及时更改该传递的数据为元对象,非继承 QObject 这里采用指针方式传递,同时把传递的局部变量 ...

  10. android pull 解析器解析xml文档

    person.xml <?xml version="1.0" encoding="UTF-8"?> <persons> <pers ...