upc组队赛5 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.
输入
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【搜索】的更多相关文章
- Stanford Local 2016 E "Election of Evil"(搜索(正解)或并查集(划掉))
传送门 题意: 给出集合U,V,集合U有n个元素,集合V有m个元素: 有 m 个操作,mi : s1 s2 有一条s1指向s2的边(s1,s2可能属于第三个集合,暂且称之为K集合): 指向边具有传递性 ...
- upc组队赛5 Hunter’s Apprentice 【判断多边形边界曲线顺逆时针】
Hunter's Apprentice 题目描述 When you were five years old, you watched in horror as a spiked devil murde ...
- upc组队赛3 Chaarshanbegaan at Cafebazaar
Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...
- upc组队赛1 过分的谜题【找规律】
过分的谜题 题目描述 2060年是云南中医学院的百年校庆,于是学生会的同学们搞了一个连续猜谜活动:共有10个谜题,现在告诉所有人第一个谜题,每个谜题的答案就是下一个谜题的线索....成功破解最后一个谜 ...
- upc组队赛1 不存在的泳池【GCD】
不存在的泳池 题目描述 小w是云南中医学院的同学,有一天他看到了学校的百度百科介绍: 截止到2014年5月,云南中医学院图书馆纸本藏书74.8457万册,纸质期刊388种,馆藏线装古籍图书1.8万册, ...
- upc组队赛1 黑暗意志【stl-map】
黑暗意志 题目描述 在数千年前潘达利亚从卡利姆多分离之时,迷雾笼罩着这块新形成的大陆,使它不被外来者发现.迷雾同样遮蔽着这片大陆古老邪恶的要塞--雷神的雷电王座.在雷神统治时期,他的要塞就是雷电之王力 ...
- upc组队赛1 闪闪发光 【优先队列】
闪闪发光 题目描述 一所位于云南昆明的中医药本科院校--云南中医学院. 因为报考某专业的人数骤减,正面临着停招的危机. 其中有九名少女想到一条妙计--成为偶像, 只要她们成为偶像,学校的名气便会增加, ...
- upc组队赛1 流连人间的苏苏
流连人间的苏苏 题目描述 苏苏在做红尘仙的任务时,发现坐落于风景秀丽.四季如春的昆明市的云南中医学院. 没过多久,苏苏就喜欢上了这个学校.以致于苏苏忘了回涂山的时间,现在她只剩下d天的时间待在云南中医 ...
- upc组队赛18 THE WORLD【时间模拟】
THE WORLD 题目链接 题目描述 The World can indicate world travel, particularly on a large scale. You mau be l ...
随机推荐
- HBase最佳实践-读性能优化策略
任何系统都会有各种各样的问题,有些是系统本身设计问题,有些却是使用姿势问题.HBase也一样,在真实生产线上大家或多或少都会遇到很多问题,有些是HBase还需要完善的,有些是我们确实对它了解太少.总结 ...
- Java并发AtomicReference类
java.util.concurrent.atomic.AtomicReference类提供了可以原子读取和写入的底层对象引用的操作,还包含高级原子操作. AtomicReference支持对底层对象 ...
- 洛谷 P1197 [JSOI2008]星球大战——并查集
先上一波题目 https://www.luogu.org/problem/P1197 很明显删除的操作并不好处理 那么我们可以考虑把删边变成加边 只需要一波时间倒流就可以解决拉 储存删边顺序倒过来加边 ...
- vue 中router.go;router.push和router.replace的区别
vue 中router.go:router.push和router.replace的区别:https://blog.csdn.net/div_ma/article/details/79467165 t ...
- 蛋糕仙人的javascript笔记
蛋糕仙人的javascript笔记:https://www.w3cschool.cn/kesyi/kesyi-nqej24rv.html
- 如何使用Charles对手机进行抓包
步骤:(如下为ios步骤,安卓类似) 1)Mac安装Charles,保证手机与电脑在同一wifi(若没有WiFi时,Mac.MacBook,etc 自带热点功能) 2)在手机当前连接的wifi设置里配 ...
- 在centos上安装nodejs
之前在百度云上买了个服务器,选择的centos 64位系统. 买完之后一顿折腾,今天就来讲讲怎么安装node和npm,刚开始在Google上找了好多方法,都是费时.费力,最后还是没有安装成功,下面将介 ...
- Linux快速显示图片
首先在Ubuntu里面制作图片, ######################################################################## 1920x1080为 ...
- tensorflow 版本的mask rcnn 实践
1. https://www.jianshu.com/p/27e4dc070761 (tensorflow object detection API/Mask RCNN)
- Python自动补全缩写意义
自动补全的变量的类别p:parameter 参数 m:method 方法(类实例方法)调用方式classA aa.method()或者classA().method() c:class 类 v:var ...