-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24445.cpp
More file actions
57 lines (41 loc) · 1.01 KB
/
24445.cpp
File metadata and controls
57 lines (41 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include <string>
#include <math.h>
using namespace std;
vector<int> adj[100001];
int visited[100001];
int visCount = 1;
bool cmp(int a, int b) {
return a > b;
}
int main() {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int N, M, R; cin >> N >> M >> R;
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 0; i <= N; i++) {
sort(adj[i].begin(), adj[i].end(), cmp);
}
queue<int> q;
q.push(R);
visited[R] = visCount++;
while (!q.empty()) {
int curr = q.front(); q.pop();
for (auto now : adj[curr]) {
if (visited[now] != 0)continue;
visited[now] = visCount++;
q.push(now);
}
}
for (int i = 1; i <= N; i++) {
cout << visited[i] << '\n';
}
}