题目连接:https://atcoder.jp/contests/abc168/tasks/abc168_d
Time Limit: 2 sec / Memory Limit: 1024 MB
Score: 400400 points
Problem Statement
There is a cave.
The cave has NN rooms and MM passages. The rooms are numbered 11 to NN, and the passages are numbered 11 to MM. Passage ii connects Room AiAi and Room BiBibidirectionally. One can travel between any two rooms by traversing passages. Room 11 is a special room with an entrance from the outside.
It is dark in the cave, so we have decided to place a signpost in each room except Room 11. The signpost in each room will point to one of the rooms directly connected to that room with a passage.
Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 11.
- If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 11 after traversing the minimum number of passages possible.
Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.
Constraints
- All values in input are integers.
- 2≤N≤1052≤N≤105
- 1≤M≤2×1051≤M≤2×105
- 1≤Ai,Bi≤N (1≤i≤M)1≤Ai,Bi≤N (1≤i≤M)
- Ai≠Bi (1≤i≤M)Ai≠Bi (1≤i≤M)
- One can travel between any two rooms by traversing passages.
Input
Input is given from Standard Input in the following format:
NN MM A1A1 B1B1 :: AMAM BMBM
Output
If there is no way to place signposts satisfying the objective, print No
.
Otherwise, print NN lines. The first line should contain Yes
, and the ii-th line (2≤i≤N)(2≤i≤N) should contain the integer representing the room indicated by the signpost in Room ii.
Sample Input 1 Copy
Copy
4 4 1 2 2 3 3 4 4 2
Sample Output 1 Copy
Copy
Yes 1 2 2
If we place the signposts as described in the sample output, the following happens:
- Starting in Room 22, you will reach Room 11 after traversing one passage: (2)→1(2)→1. This is the minimum number of passages possible.
- Starting in Room 33, you will reach Room 11 after traversing two passages: (3)→2→1(3)→2→1. This is the minimum number of passages possible.
- Starting in Room 44, you will reach Room 11 after traversing two passages: (4)→2→1(4)→2→1. This is the minimum number of passages possible.
Thus, the objective is satisfied.
Sample Input 2 Copy
Copy
6 9 3 4 6 1 2 4 5 3 4 6 1 5 6 2 4 5 5 6
Sample Output 2 Copy
Copy
Yes 6 5 5 1 1
If there are multiple solutions, any of them will be accepted.
题意:除第一个房间之外都存在一个指示牌,让你求每个房间的指示牌指向哪个房间才能使从每个房间都能到达房间1的方法,并且每个房间到达房间一的距离最小,这个体现在需要你记录一下路径
思路:最短路dijkstra ,首先肯定的是如果有一个房间不能到达房间1,直接就输出NO,另外需要建一个前驱数组pre存储最短路的路径
code:
#include<iostream>
#include<map>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#define PII pair<int,int>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n,m;
const int N=2e5+10;
int head[N],ne[N<<1],e[N<<1];
int cnt;
bool st[N];
int pre[N];//存放路径
int dis[N];
priority_queue<PII,vector<PII>,greater<PII> > heap;
void add(int u,int v){
e[cnt]=v,ne[cnt]=head[u],head[u]=cnt++;
}
void dijkstra(){
heap.push({0,1});
dis[1]=0;
while(heap.size()){
auto t=heap.top();//每次取最短的路
heap.pop();
int ver=t.second,distance=t.first;
if(st[ver]) continue;
st[ver]=1;
for(int i=head[ver];i!=-1;i=ne[i]){
int v=e[i];
if(dis[v]>distance+1){//更新最短路径,
pre[v]=ver; //更新这个点的前驱节点
dis[v]=distance+1;
heap.push({dis[v],v});
}
}
}
}
int main()
{
memset(dis,INF,sizeof(dis));
memset(head,-1,sizeof(head));
cin>>n>>m;
int u,v;
while(m--){
cin>>u>>v;
add(u,v);
add(v,u);
}
dijkstra();
for(int i=1;i<=n;i++){//特判达不到的情况
if(dis[i]==INF){
cout<<"No"<<endl;
return 0;
}
}
cout<<"Yes"<<endl;
for(int i=2;i<=n;i++){
cout<<pre[i]<<endl;
}
}