-
Notifications
You must be signed in to change notification settings - Fork 0
/
131D. Subway.cpp
93 lines (74 loc) · 1.98 KB
/
131D. Subway.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> edges;
vector<pair<int,int>> es;
int n;
int find(vector<int>& fa, int x) {
return fa[x]==x ? x : fa[x]=find(fa,fa[x]);
}
bool dfs(vector<int>& path, vector<bool>& vis, int now, int start, int aim) {
path.push_back(now);
vis[now] = true;
if (now==aim) return true;
for (auto nxt : edges[now])
if (!(now==start && nxt==aim || now==aim && nxt==start)
&& !vis[nxt] && dfs(path, vis, nxt, start, aim))
return true;
path.pop_back();
return false;
}
vector<int> find_loop() {
vector<int> fa = {0};
for (int i=1; i<=n; ++i) fa.push_back(i);
// union set find extra edge
int xl, yl;
bool found = false;
for (auto& edge: es) {
int x = edge.first, y = edge.second;
int fx = find(fa, x), fy = find(fa, y);
if (fx==fy) {
xl = x;
yl = y;
found = true;
break;
} else
fa[fx] = fy;
if (found) break;
}
// dfs find loop
vector<int> path;
vector<bool> vis(n+1, false);
dfs(path, vis, xl, xl, yl);
return path;
}
void dfs1(vector<int>& dis, int now, int len) {
dis[now] = len;
for (auto nxt : edges[now])
if (dis[nxt]==INT_MAX)
dfs1(dis, nxt, len+1);
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
edges = vector<vector<int>>(n+1, vector<int>{});
for (int x,y,i=0; i<n; ++i) {
cin >> x >> y;
es.push_back({x, y});
edges[x].push_back(y);
edges[y].push_back(x);
}
auto loop = find_loop();
vector<int> dis(n+1, INT_MAX);
for (auto station : loop)
dis[station] = 0;
// dfs find distance
for (auto station : loop)
for (auto nxt : edges[station])
if (dis[nxt] != 0)
dfs1(dis, nxt, 1);
for (int i=1; i<n; ++i)
cout << dis[i] << ' ';
cout << dis.back() << endl;
return 0;
}