Election of Evil dfs

题目描述

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.

题意:

给你一行 U的集合 和 一行 V的集合, 你可以让U集合的人去说服V集合的人,然后会给你一些AB关系,A能说服B,看最后能说服多少个,按字典序输出人名。 把每个人的名字都标上点,然后将说服关系建立有向图,然后从U做起点开始dfs,经过的点都标记,最后看看V集合那些被标记,注意两点 一个人可以同时出现在U,V集合。

AC代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
#include<string>
#include<vector>
#pragma warning(disable:4996)
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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值