結果
問題 | No.1813 Magical Stones |
ユーザー | 草苺奶昔 |
提出日時 | 2024-08-19 03:55:52 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 234 ms / 2,000 ms |
コード長 | 3,001 bytes |
コンパイル時間 | 16,447 ms |
コンパイル使用メモリ | 239,760 KB |
実行使用メモリ | 40,820 KB |
最終ジャッジ日時 | 2024-08-19 03:56:16 |
合計ジャッジ時間 | 21,712 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 12 ms
11,972 KB |
testcase_04 | AC | 1 ms
6,944 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 4 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,944 KB |
testcase_10 | AC | 2 ms
6,940 KB |
testcase_11 | AC | 41 ms
10,004 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 2 ms
6,940 KB |
testcase_14 | AC | 12 ms
11,972 KB |
testcase_15 | AC | 215 ms
28,740 KB |
testcase_16 | AC | 222 ms
28,784 KB |
testcase_17 | AC | 211 ms
40,820 KB |
testcase_18 | AC | 218 ms
28,780 KB |
testcase_19 | AC | 220 ms
28,764 KB |
testcase_20 | AC | 234 ms
28,776 KB |
testcase_21 | AC | 213 ms
28,764 KB |
testcase_22 | AC | 221 ms
28,776 KB |
testcase_23 | AC | 218 ms
28,772 KB |
testcase_24 | AC | 3 ms
6,944 KB |
testcase_25 | AC | 21 ms
7,708 KB |
testcase_26 | AC | 7 ms
6,944 KB |
testcase_27 | AC | 142 ms
19,928 KB |
testcase_28 | AC | 199 ms
26,544 KB |
testcase_29 | AC | 190 ms
24,500 KB |
testcase_30 | AC | 195 ms
26,568 KB |
testcase_31 | AC | 208 ms
26,696 KB |
testcase_32 | AC | 1 ms
6,940 KB |
testcase_33 | AC | 1 ms
6,940 KB |
testcase_34 | AC | 7 ms
6,940 KB |
testcase_35 | AC | 27 ms
6,940 KB |
testcase_36 | AC | 4 ms
6,944 KB |
testcase_37 | AC | 56 ms
9,740 KB |
testcase_38 | AC | 22 ms
10,928 KB |
testcase_39 | AC | 136 ms
15,332 KB |
testcase_40 | AC | 5 ms
6,944 KB |
testcase_41 | AC | 7 ms
6,940 KB |
testcase_42 | AC | 27 ms
6,940 KB |
testcase_43 | AC | 15 ms
6,940 KB |
ソースコード
// StronglyConnectedComponent-有向图SCC package main import ( "bufio" "fmt" "os" ) func main() { yuki1813() // yosupo() // yuki1293() } func yuki1813() { // https://yukicoder.me/problems/no/1813 // 不等关系:有向边; 全部相等:强连通(环) // 给定一个DAG 求将DAG变为一个环(强连通分量)的最少需要添加的边数 // !答案为 `max(入度为0的点的个数, 出度为0的点的个数)` in := bufio.NewReader(os.Stdin) out := bufio.NewWriter(os.Stdout) defer out.Flush() var n, m int32 fmt.Fscan(in, &n, &m) graph := make([][]int32, n) for i := int32(0); i < m; i++ { var u, v int32 fmt.Fscan(in, &u, &v) u, v = u-1, v-1 graph[u] = append(graph[u], v) } count, belong := StronglyConnectedComponent(graph) if count == 1 { // 缩成一个点了,说明是强连通的 fmt.Fprintln(out, 0) return } dag := SCCDag(graph, count, belong) indeg, outDeg := make([]int32, count), make([]int32, count) for i := int32(0); i < count; i++ { for _, next := range dag[i] { indeg[next]++ outDeg[i]++ } } in0, out0 := int32(0), int32(0) for i := int32(0); i < count; i++ { if indeg[i] == 0 { in0++ } if outDeg[i] == 0 { out0++ } } fmt.Fprintln(out, max32(in0, out0)) } // 有向图强连通分量分解. func StronglyConnectedComponent(graph [][]int32) (count int32, belong []int32) { n := int32(len(graph)) belong = make([]int32, n) low := make([]int32, n) order := make([]int32, n) for i := range order { order[i] = -1 } now := int32(0) path := []int32{} var dfs func(int32) dfs = func(v int32) { low[v] = now order[v] = now now++ path = append(path, v) for _, to := range graph[v] { if order[to] == -1 { dfs(to) low[v] = min32(low[v], low[to]) } else { low[v] = min32(low[v], order[to]) } } if low[v] == order[v] { for { u := path[len(path)-1] path = path[:len(path)-1] order[u] = n belong[u] = count if u == v { break } } count++ } } for i := int32(0); i < n; i++ { if order[i] == -1 { dfs(i) } } for i := int32(0); i < n; i++ { belong[i] = count - 1 - belong[i] } return } // 有向图的强连通分量缩点. func SCCDag(graph [][]int32, count int32, belong []int32) (dag [][]int32) { dag = make([][]int32, count) adjSet := make([]map[int32]struct{}, count) for i := int32(0); i < count; i++ { adjSet[i] = make(map[int32]struct{}) } for cur, nexts := range graph { for _, next := range nexts { if bid1, bid2 := belong[cur], belong[next]; bid1 != bid2 { adjSet[bid1][bid2] = struct{}{} } } } for i := int32(0); i < count; i++ { for next := range adjSet[i] { dag[i] = append(dag[i], next) } } return } func min(a, b int) int { if a < b { return a } return b } func max(a, b int) int { if a > b { return a } return b } func min32(a, b int32) int32 { if a < b { return a } return b } func max32(a, b int32) int32 { if a > b { return a } return b }