[BOJ] 16947 서울 지하철 2호선

Time Lapse :39min 20sec

16947.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
#include <algorithm>
#include <vector>
#include <queue>
#include <memory.h>
#include <iostream>
using namespace std;
vector<int> g[3001];
int ans[3001];
bool v[3001];
int n, f, e;
bool loop(int f, int e) {
if(!ans[e]) return true;
for(int nt : g[e]) {
if(nt == f) continue;
if(loop(e, nt)) {
ans[e] = 0;
return true;
}
}
return false;
}
void bfs() {
queue<pair<int, int>> q;
q.push(make_pair(-1, 1));
v[1] = true;
while(!q.empty()) {
pair<int,int> val = q.front();
q.pop();
for(int nt : g[val.second]) {
if(val.first == nt) continue;
if(!v[nt]) {
if(g[nt].size() >= 2)
q.push(make_pair(val.second,nt));
v[nt] = true;
}
else{
ans[nt] = 0;
for(int lnt : g[nt])
loop(nt, lnt);
return;
}
}
}
}
int dfs(int f, int e) {
if(ans[e] != -1) return ans[e] + 1;
for(int nt : g[e]) {
if(nt == f) continue;
int ret = dfs(e, nt);
if(ret >= 1)
return (ans[e] = ret) + 1;
}
return -987654321;
}
int main(void) {
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d %d",&f, &e);
g[f].push_back(e);
g[e].push_back(f);
}
memset(ans, -1, sizeof(ans));
bfs();
for(int i = 1; i <= n; ++i) {
if(ans[i]<0) dfs(-1, i);
printf("%d ", ans[i]);
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/25/PS/BOJ/16947/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.