【dfs】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 di?erent 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 e?ectively 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,m,下面一行輸入u個名稱,其下一行輸入v個名稱,然后是m組說服關系,每行輸入兩個名稱s1,s2,若u中的一個人能夠說服v中的人投票給Dylan,則將v中此人記錄下來,最后將答案按照字典序輸出,同時有一點需注意,若假如某人即在u中又在v中,則他肯定能說服自己。

解題思路:

我們可以根據m行說服關系建一個有向圖(樹),不難發現,若以u中的一個人為根節點,那么他肯定能說服在以他為根的路徑上的所有在v中的人都能被他說服投票給Dylan,所以可以通過dfs尋找這樣的人,把人名存入set中,最后輸出即可。

代碼:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
map<string,int> m;
bool vis[200100];
set<string> ss;
vector<int> mapp[400100];
map<int,string> str;
int l,r;
void dfs(int x) {if(mapp[x].size()==0) return;for(int i=0;i<mapp[x].size();i++) {if(vis[mapp[x][i]]==false) {
/*			cout<<str[x]<<" "<<str[mapp[x][i]]<<endl;
*/			vis[mapp[x][i]]=true;if(mapp[x][i]>=l&&mapp[x][i]<=r) ss.insert(str[mapp[x][i]]);dfs(mapp[x][i]);}}}
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;cin>>t;while(t--) {int u,v,mm;rep(i,1,40000) mapp[i].clear();string s;m.clear();memset(vis,false,sizeof vis);ss.clear();int cnt=0;cin>>u>>v>>mm;rep(i,1,u) {cin>>s;cnt++;m[s]=cnt;str[cnt]=s;}l=cnt+1;rep(i,1,v) {cin>>s;if(m[s]!=0) {ss.insert(s);}else {cnt++;m[s]=cnt;str[cnt]=s;}}r=cnt;string s1,s2;rep(i,1,mm) {cin>>s1>>s2;if(m[s1]==0) {cnt++;m[s1]=cnt;str[cnt]=s1;}if(m[s2]==0) {cnt++;m[s2]=cnt;str[cnt]=s2;}mapp[m[s1]].push_back(m[s2]);}rep(i,1,u) {if(vis[i]==false) {vis[i]=true;dfs(i);}}set<string>::iterator it1;for(it1=ss.begin();it1!=ss.end();it1++) cout<<*it1<<" ";cout<<endl;}return 0;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536306.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536306.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536306.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【思維】Iranian ChamPions Cup

題目描述 The Iranian ChamPions Cup (ICPC), the most prestigious football league in Iran, is reaching its end, and people are eagerly waiting for the finals, which happened to be between the two most popular Iranian teams, Persepolis and Esteghlal. The ICP…

【數學】Chaarshanbegaan at Cafebazaar

題目描述 Chaarshanbegaan is a gathering event at Cafebazaar similar to TGIF events at Google. Some entertainment programs like pantomime, foosball, Xbox/PS4, and several board games are part of the event. You are going to set up a dart game in Chaarshanbe…

【思維】Congestion Charging Zone

題目描述 Tehran municipality has set up a new charging method for the Congestion Charging Zone (CCZ) which controls the passage of vehicles in Tehran’s high-congestion areas in the congestion period (CP) from 6:30 to 19:00. There are plate detection came…

【二分】LED

題目描述 A Light-Emitting Diode (LED) is a semiconductor light source, which emits light when an electric current of voltage higher than a threshhold is applied to its leads. ACM R&D recently reported that they have succesfully developed a new LED, na…

【差分數組】Master of GCD

題目描述 Hakase has n numbers in a line. At fi rst, they are all equal to 1. Besides, Hakase is interested in primes. She will choose a continuous subsequence [l, r] and a prime parameter x each time and for every l≤i≤r, she will change ai into ai*x. To…

【模擬】Ground Defense

題目描述 You are a denizen of Linetopia, whose n major cities happen to be equally spaced along an east-west line. In fact, they are often numbered in order from 1 to n, where 1 is the westmost city and n is the eastmost city. Linetopia was a lovely plac…

【模擬】Bulbs

題目描述 Greg has an m n grid of Sweet Lightbulbs of Pure Coolness he would like to turn on. Initially, some of the bulbs are on and some are off. Greg can toggle some bulbs by shooting his laser at them. When he shoots his laser at a bulb, it toggles th…

【模擬】Ingenious Lottery Tickets

題目描述 Your friend Superstitious Stanley is always getting himself into trouble. This time, in his Super Lotto Pick and Choose plan, he wants to get rich quick by choosing the right numbers to win the lottery. In this lottery, entries consist of six dis…

【數學】Hunter’s Apprentice

題目描述 When you were five years old, you watched in horror as a spiked devil murdered your parents. You would have died too, except you were saved by Rose, a passing demon hunter. She ended up adopting you and training you as her apprentice. Rose’s cur…

【模擬】Thanks, TuSimple!

題目鏈接&#xff1a;http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId5979 Thanks, TuSimple! Time Limit: 1 Second Memory Limit: 65536 KB In the very first sentence of the very first problem, we would like to give our sincere thanks to TuSimple,…

【二維差分】Monitor

Monitor 題目&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid6514 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others) Total Submission(s): 600 Accepted Submission(s): 190 Problem Description Xiaoteng has a la…

【數學】MORE XOR

Given a sequence of nnn numbers a1,a2,?&ThinSpace;,ana_1, a_2,\cdots, a_na1?,a2?,?,an? and three functions. Define a function f(l,r)f(l,r)f(l,r) which returns ⊕a[x](l≤x≤r)\oplus a[x] (l \le x \le r)⊕a[x](l≤x≤r). The \oplus⊕ represents excl…

【數學】Element Swapping

Element Swapping Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid has an integer sequence a1,a2,a3,…,ana_1,a_2,a_3,\dots,a_na1?,a2?,a3?,…,an? and he likes it very much. Unfortunately, his naughty roommate BaoBao swapped two elements aia_iai? an…

【二分+二維前綴和】Largest Allowed Area

Largest Allowed Area 時間限制: 1 Sec 內存限制: 128 MB 提交: 146 解決: 54 [提交] [狀態] [命題人:admin] 題目描述 A company is looking for land to build its headquarters. It has a lot of money and can buy as many land patches as it needs. Its goal, howev…

【數學】Floating-Point Hazard

Floating-Point Hazard 時間限制: 1 Sec 內存限制: 128 MB 提交: 106 解決: 42 [提交] [狀態] [命題人:admin] 題目描述 Given the value of low, high you will have to find the value of the following expression: If you try to find the value of the above express…

【manacher】Strings in the Pocket

Strings in the Pocket Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found two strings ss1s2…snss_1s_2\dots s_nss1?s2?…sn? and tt1t2…tntt_1t_2\dots t_ntt1?t2?…tn? in his left pocket, where sis_isi? indicates the iii-th character in…

【并查集+dp】Team

Team 時間限制: 1 Sec 內存限制: 128 MB 提交: 124 解決: 10 [提交] [狀態] [命題人:admin] 題目描述 ACM-ICPC is a interesting game. A team takes part in this game must consist of exactly (no more and no less) three players. Every year, many new members wil…

【線段樹】Segment Tree

Segment Tree 時間限制: 1 Sec 內存限制: 512 MB 提交: 107 解決: 23 [提交] [狀態] [命題人:admin] 題目描述 Mcginn opens the code which he wrote 25 years ago. Clever Mcginn wants to know how many positive interger n satis?ed that the maximum c can reach w…

UVA1025——A Spy in the Metro【dp】

題目鏈接&#xff1a;https://cn.vjudge.net/problem/UVA-1025 題目大意&#xff1a;Mario從第1站出發&#xff0c;目的是在時刻T會見車站 nnn 的一個間諜。由于在車站等待容易被抓&#xff0c;所以應盡量躲在開動的火車上&#xff0c;即在車站等待的時間最短&#xff0c;且Ma…

HDU1284——錢幣兌換問題【dp】

錢幣兌換問題 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14528 Accepted Submission(s): 8784 Problem Description 在一個國家僅有1分&#xff0c;2分&#xff0c;3分硬幣&#xff0c;將錢N兌換成硬…