結果
問題 | No.1098 LCAs |
ユーザー | sgsw |
提出日時 | 2021-08-17 22:09:40 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 534 ms / 2,000 ms |
コード長 | 2,228 bytes |
コンパイル時間 | 11,848 ms |
コンパイル使用メモリ | 224,664 KB |
実行使用メモリ | 83,584 KB |
最終ジャッジ日時 | 2024-10-10 22:17:43 |
合計ジャッジ時間 | 19,679 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,820 KB |
testcase_03 | AC | 2 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,820 KB |
testcase_05 | AC | 1 ms
6,820 KB |
testcase_06 | AC | 2 ms
6,816 KB |
testcase_07 | AC | 2 ms
6,816 KB |
testcase_08 | AC | 1 ms
6,816 KB |
testcase_09 | AC | 2 ms
6,820 KB |
testcase_10 | AC | 2 ms
6,820 KB |
testcase_11 | AC | 1 ms
6,816 KB |
testcase_12 | AC | 2 ms
6,816 KB |
testcase_13 | AC | 3 ms
6,820 KB |
testcase_14 | AC | 3 ms
6,820 KB |
testcase_15 | AC | 4 ms
6,816 KB |
testcase_16 | AC | 3 ms
6,820 KB |
testcase_17 | AC | 3 ms
6,820 KB |
testcase_18 | AC | 452 ms
21,472 KB |
testcase_19 | AC | 446 ms
21,120 KB |
testcase_20 | AC | 449 ms
21,120 KB |
testcase_21 | AC | 445 ms
21,120 KB |
testcase_22 | AC | 451 ms
21,120 KB |
testcase_23 | AC | 405 ms
24,064 KB |
testcase_24 | AC | 411 ms
24,064 KB |
testcase_25 | AC | 392 ms
22,016 KB |
testcase_26 | AC | 404 ms
25,216 KB |
testcase_27 | AC | 397 ms
24,360 KB |
testcase_28 | AC | 532 ms
81,152 KB |
testcase_29 | AC | 533 ms
83,584 KB |
testcase_30 | AC | 534 ms
80,768 KB |
ソースコード
package main import ( "bufio" "fmt" "os" "strconv" ) var wg = bufio.NewScanner(os.Stdin) const ( inf = int(1e18) initialBufSize = int(1e6) maxBufSize = int(1e6) ) var buf []byte = make([]byte, initialBufSize) type Graph struct { n int edges [][]int } func NewGraph(n int) *Graph { g := &Graph{ n: n, edges: make([][]int, n), } for i := 0; i < n; i++ { g.edges[i] = make([]int, 0) } return g } func (g *Graph) AddEdge(u, v int) { g.edges[u] = append(g.edges[u], v) } func main() { n := nextInt() g := NewGraph(n) for i := 0; i < n-1; i++ { u, v := nextInt(), nextInt() u-- v-- g.AddEdge(u, v) g.AddEdge(v, u) } //visited dist := make([]int, n) child := make([]int, n) par := make([]int, n) prev_sum := make([]int, n) dp := make([]int, n) for i := 0; i < n; i++ { par[i] = -1 dist[i] = inf } S := 0 dist[S] = 0 var dfs func(int) dfs = func(u int) { for _, v := range g.edges[u] { if dist[v] == inf { dist[v] = dist[u] + 1 par[v] = u dfs(v) } } child[u]++ for _, v := range g.edges[u] { if v != par[u] { child[u] += child[v] dp[u] -= prev_sum[v] } } dp[u] += child[u] * child[u] prev_sum[u] = child[u] * child[u] } dfs(S) for _, v := range dp { fmt.Printf("%d\n", v) } } // greatest common divisor (GCD) via Euclidean algorithm func GCD(a, b int) int { for b != 0 { t := b b = a % b a = t } return a } func modInv(a, modulo int) int { b := modulo u, v := 1, 0 for b > 0 { t := a / b a, b = b, a-t*b u, v = v, u-t*v } u %= modulo if u < 0 { u += modulo } return u } func powMod(x, n, mod int) int { retval := 1 for mul := x; n > 0; { if n&1 == 1 { retval = retval * mul % mod } mul = mul * mul % mod n >>= 1 } return retval } func init() { wg.Split(bufio.ScanWords) wg.Buffer(buf, maxBufSize) } func reverse_array(array []int) []int { //reverse the slice for i, j := 0, len(array)-1; i < j; i, j = i+1, j-1 { array[i], array[j] = array[j], array[i] } return array } func nextInt() int { wg.Scan() i, e := strconv.Atoi(wg.Text()) if e != nil { panic(e) } return i } func nextStr() string { wg.Scan() i := wg.Text() return i }