-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24479.cpp
More file actions
49 lines (37 loc) · 890 Bytes
/
24479.cpp
File metadata and controls
49 lines (37 loc) · 890 Bytes
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
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include <string>
#include <math.h>
using namespace std;
int N, M, R;
vector <int> adj[100001];
int visited[100001];
int Count = 1;
void dfs(int node) {
for (auto next : adj[node]) {
if (visited[next] != 0)continue;
visited[next] = Count++;
dfs(next);
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
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());
}
visited[R] = Count++;
dfs(R);
for (int i = 1; i <= N; i++) {
cout << visited[i] << "\n";
}
}