当前位置:网站首页>[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
边栏推荐
- 解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
- LeetCode 1560. The sector with the most passes on the circular track
- js封装数组反转的方法--冯浩的博客
- JS time function Daquan detailed explanation ----- AHAO blog
- QT implementation fillet window
- 图像处理一百题(1-10)
- Study notes of Tutu - process
- Detailed explanation of FLV format
- VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
- SQL快速入门
猜你喜欢
ByteDance new programmer's growth secret: those glittering treasures mentors
新手必会的静态站点生成器——Gridsome
LeetCode 1020. Number of enclaves
Click QT button to switch qlineedit focus (including code)
Chapter 1 overview of MapReduce
7-4 harmonic average
Chapter 6 rebalance details
Remove the border when input is focused
Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
Codeforces Round #802(Div. 2)A~D
随机推荐
LeetCode 1640. Can I connect to form an array
Two weeks' experience of intermediate software designer in the crash soft exam
7-4 harmonic average
(lightoj - 1370) Bi shoe and phi shoe (Euler function tabulation)
Click QT button to switch qlineedit focus (including code)
业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
Detailed explanation of FLV format
第2章 HFDS的Shell操作
CMake速成
China double brightening film (dbef) market trend report, technical dynamic innovation and market forecast
Li Kou - 298th weekly match
Submit several problem records of spark application (sparklauncher with cluster deploy mode)
LeetCode 1020. Number of enclaves
(lightoj - 1349) Aladdin and the optimal invitation (greed)
Market trend report, technological innovation and market forecast of China's double sided flexible printed circuit board (FPC)
7-5 blessing arrived
Market trend report, technical innovation and market forecast of China's desktop capacitance meter
Solve the single thread scheduling problem of intel12 generation core CPU (II)
视频压缩编码和音频压缩编码基本原理