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;
}

猜你喜欢

转载自blog.csdn.net/Prince_NYing/article/details/89194932
dfs