結果
問題 | No.416 旅行会社 |
ユーザー | 草苺奶昔 |
提出日時 | 2024-09-08 16:58:22 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 428 ms / 4,000 ms |
コード長 | 3,097 bytes |
コンパイル時間 | 14,814 ms |
コンパイル使用メモリ | 219,368 KB |
実行使用メモリ | 16,296 KB |
最終ジャッジ日時 | 2024-09-08 16:58:43 |
合計ジャッジ時間 | 17,554 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 200 ms
11,636 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 3 ms
6,940 KB |
testcase_08 | AC | 6 ms
6,940 KB |
testcase_09 | AC | 23 ms
6,940 KB |
testcase_10 | AC | 208 ms
11,632 KB |
testcase_11 | AC | 201 ms
11,380 KB |
testcase_12 | AC | 204 ms
11,632 KB |
testcase_13 | AC | 197 ms
11,376 KB |
testcase_14 | AC | 422 ms
16,072 KB |
testcase_15 | AC | 420 ms
15,944 KB |
testcase_16 | AC | 428 ms
16,048 KB |
testcase_17 | AC | 419 ms
16,072 KB |
testcase_18 | AC | 416 ms
16,080 KB |
testcase_19 | AC | 282 ms
16,236 KB |
testcase_20 | AC | 280 ms
16,296 KB |
ソースコード
// No.416 旅行会社 (启发式合并+反向并查集删边) // https://yukicoder.me/problems/no/416 // 给定一个无向图. // q次断开两条边. // 对每个点,求第几次删边操作后,无法从该点到达0号点. // 如果一直可以到达0号点, 输出-1. package main import ( "bufio" "fmt" "os" ) func main() { in := bufio.NewReader(os.Stdin) out := bufio.NewWriter(os.Stdout) defer out.Flush() var n, m, q int32 fmt.Fscan(in, &n, &m, &q) edges := make([][2]int32, 0, m) for i := int32(0); i < m; i++ { var u, v int32 fmt.Fscan(in, &u, &v) edges = append(edges, [2]int32{u - 1, v - 1}) } queries := make([][2]int32, 0, q) for i := int32(0); i < q; i++ { var u, v int32 fmt.Fscan(in, &u, &v) queries = append(queries, [2]int32{u - 1, v - 1}) } res := make([]int32, n) uf := NewUnionFindArraySimple32(n) groups := make([][]int32, n) for i := int32(0); i < n; i++ { groups[i] = []int32{i} } unionAt := func(time int32, a, b int32) { ra, rb := uf.Find(a), uf.Find(b) if ra == rb { return } r0 := uf.Find(0) // 连接后,原来与0不连通的点,现在连通了,说明删除的这条边是关键的. if r0 == ra || r0 == rb { other := r0 ^ ra ^ rb for _, v := range groups[other] { res[v] = time } } uf.Union(ra, rb, func(big, small int32) { for _, v := range groups[small] { groups[big] = append(groups[big], v) } groups[small] = nil }) } // set(edges) - set(queries) keptEdges := func() map[[2]int32]struct{} { res, removed := make(map[[2]int32]struct{}), make(map[[2]int32]struct{}, q) for _, e := range queries { a, b := e[0], e[1] if a > b { a, b = b, a } removed[[2]int32{a, b}] = struct{}{} } for _, e := range edges { a, b := e[0], e[1] if a > b { a, b = b, a } if _, has := removed[[2]int32{a, b}]; !has { res[[2]int32{a, b}] = struct{}{} } } return res }() for e := range keptEdges { unionAt(-1, e[0], e[1]) } for qi := q - 1; qi >= 0; qi-- { unionAt(qi+1, queries[qi][0], queries[qi][1]) } for _, v := range res[1:] { fmt.Fprintln(out, v) } } type UnionFindArraySimple32 struct { Part int32 n int32 data []int32 } func NewUnionFindArraySimple32(n int32) *UnionFindArraySimple32 { data := make([]int32, n) for i := int32(0); i < n; i++ { data[i] = -1 } return &UnionFindArraySimple32{Part: n, n: n, data: data} } func (u *UnionFindArraySimple32) Union(key1, key2 int32, beforeMerge func(big, small int32)) bool { root1, root2 := u.Find(key1), u.Find(key2) if root1 == root2 { return false } if u.data[root1] > u.data[root2] { root1, root2 = root2, root1 } if beforeMerge != nil { beforeMerge(root1, root2) } u.data[root1] += u.data[root2] u.data[root2] = root1 u.Part-- return true } func (u *UnionFindArraySimple32) Find(key int32) int32 { root := key for u.data[root] >= 0 { root = u.data[root] } for key != root { key, u.data[key] = u.data[key], root } return root } func (u *UnionFindArraySimple32) GetSize(key int32) int32 { return -u.data[u.Find(key)] }