-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1260.cpp
59 lines (49 loc) · 850 Bytes
/
1260.cpp
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
58
59
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int N, M;
vector<int> graph[1001];
bool visited[1001];
void dfs(int x) {
visited[x] = true;
cout << x << " ";
for (int to : graph[x])
if (!visited[to])
dfs(to);
}
void bfs(int x) {
bool visited[1001] = {};
queue<int> q;
visited[x] = true;
q.push(x);
while (!q.empty()) {
int x = q.front();
q.pop();
cout << x << " ";
for (int to : graph[x]) {
if (!visited[to]) {
visited[to] = true;
q.push(to);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, M;
cin >> N >> M;
int a, b;
for (int x = 0; x < M; x++) {
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int x = 0; x < N; x++) {
sort(graph[x].begin(), graph[x].end());
}
return 0;
}