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. mysql FROM_UNIXTIME 时间不准确

    mysql 使用 FROM_UNIXTIME 函数计算出来的时间少了6个小时或者8个小时 解决办法: 添加 default-time_zone = '+8:00' 这个再配置文件中 vi /etc/m ...

  2. Series序列

    import pandas as pd '''Series序列:1.序列 的声明,指定index列标签2.查看列索引(index)和元素 (values)3.选择内部元素4.为元素赋值5.用Numpy ...

  3. HDU3461_Code Lock

    Description 一排可以转动的密码锁,现在给你很多个区间,你可以操作同时转动这个区间内的密码锁,能够通过转动使之一样的密码锁为一种密码 如可以操作2~4这个区间:那么ABBB 和AAAA是一个 ...

  4. Pikachu漏洞练习平台实验——CSRF(三)

    概述 CSRF 是 Cross Site Request Forgery 的 简称,中文名为跨域请求伪造 在CSRF的攻击场景中,攻击者会伪造一个请求(一般是一个链接) 然后欺骗目标用户进行点击,用户 ...

  5. 热修复设计之AOT/JIT&dexopt 与 dex2oat (一)

    阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680本篇文章将先从AOT/JIT&dexopt 与 dex2o ...

  6. python基础----以面向对象的思想编写游戏技能系统

    1. 许多程序员对面向对象的思想都很了解,并且也能说得头头是道,但是在工作运用中却用的并不顺手. 当然,我也是其中之一. 不过最近我听了我们老师的讲课,对于面向对象的思想有了更深的理解,今天决定用一个 ...

  7. Codefores 507D The Maths Lecture( 数位DP )

    D. The Maths Lecture time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. 关于数位dp的一些思考

    大致看完了claris的数位dp的pdf,感觉题目很厚实啊QAQ. 然后回过头再总结一下(感觉也不算总结啊,就是日常吐槽....) 首先数位dp这个东西是有格式的....所以明天早上再找道题来把模板联 ...

  9. js常用转义字符列表

    转义字符 含义 \n 换行 \t 制表符 \b 空格 \r 回车 \f 换页符 \ 反斜杠 ' 单引号 '' 双引号 \0nnn 八进制代码 nnn 表示的字符( n 是 0 到 7 中的一个八进制数 ...

  10. 【转载】MySQL count(*) count(1) count(column)区别

    MyISAM 引擎把一个表的总行数存在了磁盘上,因此执行 count(*) 的时候会直接返回这个数,效率很高 InnoDB 引擎执行 count(*) 的时候,需要把数据一行一行地从引擎里面读出来,然 ...