結果

問題 No.1451 集団登校
ユーザー 草苺奶昔草苺奶昔
提出日時 2024-01-13 15:34:28
言語 Go
(1.22.1)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 3,699 bytes
コンパイル時間 13,360 ms
コンパイル使用メモリ 214,120 KB
実行使用メモリ 18,496 KB
最終ジャッジ日時 2024-01-13 15:34:45
合計ジャッジ時間 14,173 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 77 ms
14,136 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 36 ms
12,568 KB
testcase_11 AC 14 ms
14,016 KB
testcase_12 AC 38 ms
10,736 KB
testcase_13 AC 78 ms
14,716 KB
testcase_14 AC 124 ms
17,164 KB
testcase_15 AC 47 ms
10,932 KB
testcase_16 AC 72 ms
15,144 KB
testcase_17 AC 79 ms
7,728 KB
testcase_18 AC 32 ms
7,840 KB
testcase_19 AC 14 ms
9,704 KB
testcase_20 AC 130 ms
18,496 KB
testcase_21 AC 52 ms
6,676 KB
testcase_22 AC 29 ms
6,920 KB
testcase_23 AC 13 ms
6,676 KB
testcase_24 AC 30 ms
12,512 KB
testcase_25 AC 50 ms
8,952 KB
testcase_26 AC 74 ms
15,156 KB
testcase_27 AC 100 ms
13,992 KB
testcase_28 AC 32 ms
7,612 KB
testcase_29 AC 55 ms
10,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	yuki1451()
}

func yuki1451() {
	// https://yukicoder.me/problems/no/1451
	// 初始时有n个人
	// 给定m个操作,每次将i和j所在的班级合并,大小相等时随机选取班长,否则选取较大的班级的班长作为新班级的班长
	// 对每个人,问最后成为班长的概率

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	const MOD int = 1e9 + 7
	pow := func(base, exp, mod int) int {
		base %= mod
		res := 1 % mod
		for ; exp > 0; exp >>= 1 {
			if exp&1 == 1 {
				res = res * base % mod
			}
			base = base * base % mod
		}
		return res
	}

	var INV2 = pow(2, MOD-2, MOD)

	var n, m int
	fmt.Fscan(in, &n, &m)
	edges := make([]Edge, 0, m)
	for i := 0; i < m; i++ {
		var u, v int
		fmt.Fscan(in, &u, &v)
		u, v = u-1, v-1
		edges = append(edges, Edge{u, v, 1})
	}

	forest, roots, _ := KruskalTree(n, edges)
	subSize := make([]int, len(forest))
	var getSubSize func(int) int
	getSubSize = func(cur int) int {
		if cur < n { // 原始顶点
			subSize[cur] = 1
		}
		for _, to := range forest[cur] {
			subSize[cur] += getSubSize(to)
		}
		return subSize[cur]
	}
	for _, root := range roots {
		getSubSize(root)
	}

	res := make([]int, n)
	var run func(int, int)
	run = func(cur, p int) {
		if cur < n { // 原始顶点
			res[cur] = p
			return
		}

		if len(forest[cur]) == 1 { // 只有一个子节点
			run(forest[cur][0], p)
			return
		}

		left, right := forest[cur][0], forest[cur][1] // 两个子节点
		if subSize[left] > subSize[right] {
			run(left, p)
		} else if subSize[left] < subSize[right] {
			run(right, p)
		} else {
			run(left, p*INV2%MOD)
			run(right, p*INV2%MOD)
		}
	}

	for _, root := range roots {
		run(root, 1)
	}

	for _, v := range res {
		fmt.Fprintln(out, v)
	}
}

type Edge struct{ from, to, weight int }

// 表示合并过程的树,按照edges中边的顺序合并顶点.
//
//	返回:
//		forest: 森林的有向图邻接表
//		roots: 新图中的各个根节点
//		values: 每个辅助结点的权值(即对应边的权值,叶子结点权值为0).
func KruskalTree(n int, edges []Edge) (forest [][]int, roots []int, values []int) {
	parent := make([]int32, 2*n-1)
	for i := range parent {
		parent[i] = int32(i)
	}

	forest = make([][]int, 2*n-1)
	values = make([]int, 2*n-1)
	uf := NewUf(n)
	aux := int32(n)
	for i := range edges {
		e := &edges[i]
		from, to := int32(e.from), int32(e.to)
		f := func(big, small int32) {
			w, p1, p2 := e.weight, int(parent[big]), int(parent[small])
			forest[aux] = append(forest[aux], p1)
			forest[aux] = append(forest[aux], p2)
			parent[p1], parent[p2] = aux, aux
			parent[big], parent[small] = aux, aux
			values[aux] = w
			aux++
		}
		uf.Union(from, to, f)
	}

	forest = forest[:aux]
	values = values[:aux]
	for i := int32(0); i < aux; i++ {
		if parent[i] == i {
			roots = append(roots, int(i))
		}
	}
	return
}

type Uf struct {
	data []int32
}

func NewUf(n int) *Uf {
	data := make([]int32, n)
	for i := 0; i < n; i++ {
		data[i] = -1
	}
	return &Uf{data: data}
}

func (ufa *Uf) Union(key1, key2 int32, f func(big, small int32)) bool {
	root1, root2 := ufa.Find(key1), ufa.Find(key2)
	if root1 == root2 {
		return false
	}
	if ufa.data[root1] > ufa.data[root2] {
		root1, root2 = root2, root1
	}
	ufa.data[root1] += ufa.data[root2]
	ufa.data[root2] = int32(root1)
	f(root1, root2)
	return true
}

func (ufa *Uf) Find(key int32) int32 {
	if ufa.data[key] < 0 {
		return key
	}
	ufa.data[key] = ufa.Find(ufa.data[key])
	return ufa.data[key]
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
0