forked from Raj04/Coding-Ninjas
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSssp.cpp
70 lines (56 loc) · 1.1 KB
/
Sssp.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
60
61
62
63
64
65
66
67
68
69
70
// Single Source shortest path
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define f(i, n) for (i = 0; i < n; i++)
#define w(x) \
int x; \
cin >> x; \
while (x--)
#define mod 1000000007
#define ps(x, y) fixed << setprecision(y) << x
void sks()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
vector<int> adj[100001];
int vis[100001], dist[100001];
void sssp(int nodev, int d)
{
vis[nodev] = 1;
dist[nodev] = d;
for (int child : adj[nodev])
{
if (!vis[child])
sssp(child, d + 1);
}
}
int main()
{
sks();
int n, i, e, a, b;
cin >> n >> e;
for (i = 0; i < n; i++)
{
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
sssp(1, 0);
for (i = 1; i <= n; i++)
cout << i << " ";
cout << endl;
for (i = 1; i <= n; i++)
cout << dist[i] << " ";
return 0;
}