Election of Evil 【STL+DFS】

本文探讨了一个选举操控场景,其中一名政客试图通过已控制的政府代表来说服其他代表投票给他,利用算法确定哪些代表可以被说服。文章提供了一个AC代码示例,展示了如何使用图遍历算法来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目传送门

题目描述

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.

[提交][状态]

题目大意:T组测试数据,一个集合U中有u个人,另一个集合V中有v个人,然后又m个说服关系。最后按字典序输出V集合中能被U集合中的人说服为Dylan投票的人。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define io ios::sync_with_stdio(0),cin.tie(0)
#define ms(arr) memset(arr,0,sizeof(arr))
#define mc(a,b) memcpy(a,b,sizeof(b))
#define inf 0x3f3f3f
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
typedef long long ll;
typedef unsigned long long ULL;
const int mod=1e9+7;
const int N=1e5+7;
int T,u,v,m;
map <string,int> m1;
map <int,string> m2;
set<string> q;
vector <int> g[N*4];
int vis[N*2];
void init()
{
    m1.clear();
    m2.clear();
    q.clear();
    ms(vis);
    for(int i=0; i<=N*4; i++)
        g[i].clear();
}
void dfs(int x)
{
    for(int i=0; i<g[x].size(); i++)
    {
        if(vis[g[x][i]]==0)
        {
            vis[g[x][i]]=1;
            dfs(g[x][i]);
        }
    }
    return ;
}
int main()
{
    io;
    string s,t;
    int cnt=0;
    cin>>T;
    while(T--)
    {
        init();
        cin>>u>>v>>m;
        cnt=1;
        for(int i=0; i<u; i++)
        {
            cin>>s;
            m1[s]=cnt;
            m2[cnt]=s;
            cnt++;
        }
        int l=cnt;
        for(int i=0; i<v; i++)
        {
            cin>>s;
            if(m1[s]!=0) q.insert(s);
            else
            {
                m1[s]=cnt;
                m2[cnt]=s;
                cnt++;
            }
        }
        int r=cnt;
        for(int i=0; i<m; i++)
        {
            cin>>s>>t;
            if(m1[s]==0)
            {
                m1[s]=cnt;
                m2[cnt]=s;
                cnt++;
            }
            if(m1[t]==0)
            {
                m1[t]=cnt;
                m2[cnt]=s;
                cnt++;
            }
            g[m1[s]].push_back(m1[t]);
        }
        for(int i=1; i<=u; i++)
        {
            if(vis[i]==0)
            {
                vis[i]=1;
                dfs(i);
            }
        }
        for(int i=l; i<r; i++)
            if(vis[i]==1) q.insert(  m2[i]);
        set <string>:: iterator it;
        for(it=q.begin(); it!=q.end(); it++)
        {
            cout<<*it<<" ";
        }
        cout<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值