結果

問題 No.277 根掘り葉掘り
ユーザー hama_duhama_du
提出日時 2015-11-20 18:38:39
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 1,726 bytes
コンパイル時間 11,289 ms
コンパイル使用メモリ 223,748 KB
実行使用メモリ 29,184 KB
最終ジャッジ日時 2024-04-19 03:33:22
合計ジャッジ時間 15,429 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 WA -
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 227 ms
29,184 KB
testcase_10 AC 192 ms
15,360 KB
testcase_11 AC 197 ms
15,872 KB
testcase_12 AC 205 ms
17,268 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
	"io"
)

var depth []int
var rdepth []int
var graph [][]int
const INF = 114514

func dfs(now, par, dep int) int {
	depth[now] = dep
	max := INF
	for _, to := range graph[now] {
		if to == par {
			continue
		}
		cn := dfs(to, now, dep+1) + 1
		if max > cn {
			max = cn
		}
	}
	if max == INF {
		max = 0
	}
	rdepth[now] = max
	return max
}

func main() {
	n := nextInt()
	edges := make([][]int, n-1)
	deg := make([]int, n)
	for i := 0 ; i < n-1 ; i++ {
		edges[i] = make([]int, 2)
		a := nextInt()-1
		b := nextInt()-1
		deg[a]++
		deg[b]++
		edges[i][0] = a
		edges[i][1] = b
	}
	graph = make([][]int, n)
	for i := 0 ; i < n ; i++ {
		graph[i] = make([]int, deg[i])
	}
	for i := 0 ; i < n-1 ; i++ {
		a := edges[i][0]
		b := edges[i][1]
		deg[a]--
		deg[b]--
		graph[a][deg[a]] = b
		graph[b][deg[b]] = a
	}

	depth = make([]int, n)
	rdepth = make([]int, n)
	dfs(0, -1, 0)

	for i := 0 ; i < n ; i++ {
		if depth[i] < rdepth[i] {
			fmt.Println(depth[i])
		} else {
			fmt.Println(rdepth[i])
		}
	}
}

// ====

var rdr = bufio.NewReader(os.Stdin)

func nextInt() int {
	i, e := strconv.Atoi(readToken(20))
	if e != nil {
		panic(e)
	}
	return i
}

func nextString(limit int) string {
	return readToken(limit)
}

func readToken(limit int) string {
	buf := make([]byte, 0, limit)

	for {
		byte, err := rdr.ReadByte()
		if err != nil {
			if err == io.EOF {
				break
			}
		}
		if byte != 10 && byte != 13 && byte != 32 {
			buf = append(buf, byte)
			break
		}
	}
	for {
		byte, err := rdr.ReadByte()
		if err != nil {
			if err == io.EOF {
				break
			}
		}
		if byte == 10 || byte == 13 || byte == 32 {
			break
		}
		buf = append(buf, byte)
	}
	return string(buf)
}
0