結果
問題 | No.1813 Magical Stones |
ユーザー |
|
提出日時 | 2024-08-19 03:55:52 |
言語 | Go (1.23.4) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
// StronglyConnectedComponent-有向图SCCpackage mainimport ("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 int32fmt.Fscan(in, &n, &m)graph := make([][]int32, n)for i := int32(0); i < m; i++ {var u, v int32fmt.Fscan(in, &u, &v)u, v = u-1, v-1graph[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] = noworder[v] = nownow++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] = nbelong[u] = countif 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}