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 ...
随机推荐
- 面试题57:数组中2个数的和(也是leetcode题目)
题目:给定一个整数数列,找出其中和为特定值的那两个数. 你可以假设每个输入都只会有一种答案,同样的元素不能被重用. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因 ...
- 带有headers的urllib库爬取
#请求头 #1.引入模块 from urllib import request #2.操作 #(1)定义目标url base_url = "http://www.langlang2017.c ...
- Numpy基础之创建与属性
import numpy as np ''' 1.数组的创建:np.array([]) 2.数组对象的类型:type() 3.数据类型:a.dtype 4.数组的型shape:(4,2,3) 5.定义 ...
- Cocos2d Box2D之浮动刚体
| 版权声明:本文为博主原创文章,未经博主允许不得转载. b2_kinematicBody 运动学物体在模拟环境中根据自身的速度进行移动.运动学物体自身不受力的作用.虽然用户可以手动移动它,但是通 ...
- 计算两个日期之间相差的天数(带带负数) 支持格式YYYY-mm-dd和YYYY-mm-dd HH:mm:ss
/** * 计算两个日期之间相差的天数(带带负数) 支持格式YYYY-mm-dd比较 * @param higDate 减数 * @param lowDate 被减数 * @returns 差值天数 ...
- 一、python基础之字符串的处理
最近开始重新回过头来巩固一下python的基础知识,并在此做一些记录以便未来更好的回顾 一.字符串的大小写转换 title() 使用title()方法可以将字符串中每个单词的首字母大写 name = ...
- MVC使用Area:CS0234: 命名空间“System.Web”中不存在类型或命名空间名称“Optimization”(是否缺少程序集引用?)
一,如图: 解决方法是:将区域生成的的文件夹下的web.config中的using System.Web.Optimization删掉 如下,Area文件目录找到Web.config Web.conf ...
- Apache之默认配置文件解释
一.默认配置文件 # 定义apache运行的目录,即程序所在的位置 ServerRoot "/usr/local/apache2" # 定义Apache服务监听的端口 Listen ...
- 扩展阿里巴巴Java开发规约插件(转)
转自:https://blog.csdn.net/u014513883/article/details/79186893 1.前言 工作中难免会遇到维护别人代码的情况,那么首先就得看懂别人写的代码.如 ...
- python 常用技巧 — 列表(list)
目录: 1. 嵌套列表对应位置元素相加 (add the corresponding elements of nested list) 2. 多个列表对应位置相加(add the correspond ...