-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
var ( | ||
w = bufio.NewWriter(os.Stdout) | ||
sc = bufio.NewScanner(os.Stdin) | ||
) | ||
|
||
var ( | ||
N int | ||
check []int | ||
check2 []bool | ||
D []int | ||
G [][]int | ||
) | ||
|
||
func init() { | ||
sc.Split(bufio.ScanWords) | ||
} | ||
|
||
func main() { | ||
defer w.Flush() | ||
N = scanInt() | ||
check = make([]int, N+1) | ||
check2 = make([]bool, N+1) | ||
D = make([]int, N+1) | ||
G = make([][]int, N+1) | ||
for i := 0; i < N; i++ { | ||
u, v := scanInt(), scanInt() | ||
G[u] = append(G[u], v) | ||
G[v] = append(G[v], u) | ||
} | ||
|
||
// cycle부터 찾는다. | ||
solve(1, -1) | ||
|
||
// cycle에서 시작하여 연결된 노드를 찾는다. | ||
for i := 1; i <= N; i++ { | ||
if check[i] == 2 && !check2[i] { | ||
q := make([]int, 0) | ||
q = append(q, i) | ||
check2[i] = true | ||
for len(q) != 0 { | ||
cur := q[0] | ||
q = q[1:] | ||
for j := range G[cur] { | ||
next := G[cur][j] | ||
if check[next] != 2 && !check2[next] { | ||
check2[next] = true | ||
D[next] = D[cur] + 1 | ||
q = append(q, next) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
for i := 1; i <= N; i++ { | ||
fmt.Fprintf(w, "%d ", D[i]) | ||
} | ||
} | ||
|
||
func solve(node, prev int) int { | ||
if check[node] == 1 { | ||
return node | ||
} | ||
check[node] = 1 | ||
for i := range G[node] { | ||
next := G[node][i] | ||
if next == prev { | ||
continue | ||
} | ||
res := solve(next, node) | ||
if res == -2 { | ||
return -2 | ||
} | ||
if res >= 0 { | ||
check[node] = 2 | ||
if node == res { | ||
return -2 | ||
} | ||
return res | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func scanInt() int { | ||
sc.Scan() | ||
n, _ := strconv.Atoi(sc.Text()) | ||
return n | ||
} |