当前位置:网站首页>[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
边栏推荐
- Hbuilder x format shortcut key settings
- CMake速成
- 原生js实现全选和反选的功能 --冯浩的博客
- Codeforces Global Round 19
- Market trend report, technological innovation and market forecast of double door and multi door refrigerators in China
- 图像处理一百题(11-20)
- Chapter 6 datanode
- Kubernetes cluster deployment
- Acwing: the 56th weekly match
- (POJ - 1458) common subsequence (longest common subsequence)
猜你喜欢
Solve the single thread scheduling problem of intel12 generation core CPU (II)
Spark独立集群动态上线下线Worker节点
Anaconda下安装Jupyter notebook
Submit several problem records of spark application (sparklauncher with cluster deploy mode)
MariaDB的安装与配置
Solve the problem that intel12 generation core CPU single thread only runs on small cores
Tree of life (tree DP)
第三章 MapReduce框架原理
Chapter 5 yarn resource scheduler
Problem - 922D、Robot Vacuum Cleaner - Codeforces
随机推荐
Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
Chapter III principles of MapReduce framework
Solve the single thread scheduling problem of intel12 generation core CPU (II)
Codeforces Global Round 19
MP4格式详解
China double brightening film (dbef) market trend report, technical dynamic innovation and market forecast
7-5 blessing arrived
MariaDB的安装与配置
JS encapsulates the method of array inversion -- Feng Hao's blog
Chapter 6 datanode
LeetCode 1552. Magnetic force between two balls
Educational Codeforces Round 122 (Rated for Div. 2)
QT simulates mouse events and realizes clicking, double clicking, moving and dragging
Codeforces Round #799 (Div. 4)A~H
LeetCode 1641. Count the number of Lexicographic vowel strings
Codeforces Round #802(Div. 2)A~D
QT realizes window topping, topping state switching, and multi window topping priority relationship
< li> dot style list style type
Summary of game theory
LeetCode 1562. Find the latest group of size M