当前位置:网站首页>[unsolved] 7-15 shout mountain
[unsolved] 7-15 shout mountain
2022-07-06 16:45:00 【HBUcs2020】
Originally, I felt like 007 It's almost as deep into the tiger's den , Use vector and dfs Recursion is enough , Give it a try , It's my waste wood
#include<bits/stdc++.h>
using namespace std;
vector<int> q[10005];// use vector Array instead of adjacency table
int vis[10005], n, flag, mi, mi1;
queue<int> p;
void bfs(int u)
{
int i;
p.push(u);// Queue entry
vis[u] = 1;// The tag has been used ,1 On behalf of the first mountain left
while(!p.empty())// Not empty
{
u = p.front();// Take the first value of the team
p.pop();// Out of the team
if(vis[u] >= mi)// You have to walk at least two hills
{
if(vis[u] > mi)// to update mi, mi1
{
mi = vis[u];
mi1 = u;
}
else
{
mi1 = min(u, mi1);// Take the hill with the smallest number
}
}
for(i = 0; i < q[u].size(); i++)
{
if(!vis[q[u][i]])// by 0 It means you haven't walked
{
p.push(q[u][i]);// Queue entry
vis[q[u][i]] = vis[u] + 1;// to update vis value
}
}
}
}
int main()
{
int u, v, m, k;
while(~scanf("%d %d %d", &n, &m, &k))
{
while(m--)
{
scanf("%d %d", &u, &v);
q[u].push_back(v);// The subscript corresponding to the starting point , Deposit at destination
q[v].push_back(u);
}
while(k--)
{
mi = 2;// Number of hills
mi1 = 10005;// Number
memset(vis, 0, sizeof(vis));// Mark the mountain you have passed and judge how far you have gone
scanf("%d", &u);
bfs(u);
if(mi1 == 10005) printf("0\n");// There is no second mountain to walk
else printf("%d\n", mi1);
}
}
}
The first line of input is given 3 A positive integer n
、m
and k
, among n
(≤10000) It's the total number of hills ( So let's say that every mountain comes from 1 To n
Number ). Next m
That's ok , Each line gives 2 No more than one. n
The positive integer , Separate numbers with spaces , Each represents the number of two hills that can hear each other . It's guaranteed that each pair of hills is entered only once , There will be no duplicate relationship input . The last line gives k
(≤10) No more than one. n
The positive integer , Separate numbers with spaces , Represents the number of the mountain to be queried .
Output format :
In turn, for each queried hill in the input , The farthest mountain that can be reached by the chain transmission of the shout sent out in one line . Be careful : First of all, the output must be the one that can be sent by the query . If there is more than one mountain like this , Then output the one with the smallest number . If the outcry of this mountain can't reach any other mountain , The output 0.
sample input :
7 5 4
1 2
2 3
3 1
4 5
5 6
1 4 5 7
sample output :
2
6
4
0
边栏推荐
- LeetCode 1561. The maximum number of coins you can get
- 解决Intel12代酷睿CPU单线程只给小核运行的问题
- FLV格式详解
- Problem - 922D、Robot Vacuum Cleaner - Codeforces
- QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
- js时间函数大全 详细的讲解 -----阿浩博客
- Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
- Discussion on QWidget code setting style sheet
- QT realizes window topping, topping state switching, and multi window topping priority relationship
- JS time function Daquan detailed explanation ----- AHAO blog
猜你喜欢
随机推荐
< li> dot style list style type
Detailed explanation of FLV format
Market trend report, technological innovation and market forecast of China's double sided flexible printed circuit board (FPC)
LeetCode1556. Thousand separated number
Summary of game theory
Submit several problem records of spark application (sparklauncher with cluster deploy mode)
China tetrabutyl urea (TBU) market trend report, technical dynamic innovation and market forecast
提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
FLV格式详解
第五章 Yarn资源调度器
Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
Kubernetes集群部署
Discussion on QWidget code setting style sheet
Tert butyl hydroquinone (TBHQ) Industry Research Report - market status analysis and development prospect forecast
Codeforces Round #800 (Div. 2)AC
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
LeetCode 1641. Count the number of Lexicographic vowel strings
sublime text 代码格式化操作
QT implementation fillet window
LeetCode 1447. Simplest fraction