結果

問題 No.1813 Magical Stones
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-12-24 02:41:33
言語 Go
(1.22.1)
結果
AC  
実行時間 306 ms / 2,000 ms
コード長 4,745 bytes
コンパイル時間 11,804 ms
コンパイル使用メモリ 237,748 KB
実行使用メモリ 43,088 KB
最終ジャッジ日時 2024-09-27 13:34:48
合計ジャッジ時間 19,492 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 17 ms
13,352 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 47 ms
9,356 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 17 ms
13,220 KB
testcase_15 AC 302 ms
30,904 KB
testcase_16 AC 305 ms
30,916 KB
testcase_17 AC 295 ms
43,088 KB
testcase_18 AC 304 ms
30,920 KB
testcase_19 AC 300 ms
32,880 KB
testcase_20 AC 300 ms
30,912 KB
testcase_21 AC 305 ms
30,920 KB
testcase_22 AC 306 ms
30,912 KB
testcase_23 AC 300 ms
30,912 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 33 ms
6,944 KB
testcase_26 AC 10 ms
6,944 KB
testcase_27 AC 205 ms
22,580 KB
testcase_28 AC 252 ms
28,696 KB
testcase_29 AC 241 ms
26,772 KB
testcase_30 AC 223 ms
28,672 KB
testcase_31 AC 286 ms
28,804 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 10 ms
6,944 KB
testcase_35 AC 33 ms
6,940 KB
testcase_36 AC 4 ms
6,944 KB
testcase_37 AC 75 ms
11,880 KB
testcase_38 AC 30 ms
13,928 KB
testcase_39 AC 188 ms
20,168 KB
testcase_40 AC 6 ms
6,940 KB
testcase_41 AC 10 ms
6,940 KB
testcase_42 AC 34 ms
6,944 KB
testcase_43 AC 19 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 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 int
	fmt.Fscan(in, &n, &m)

	graph := make([][]int, n)
	for i := 0; i < m; i++ {
		var u, v int
		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([]int, count), make([]int, count)
	for i := 0; i < count; i++ {
		for _, next := range dag[i] {
			indeg[next]++
			outDeg[i]++
		}
	}

	in0, out0 := 0, 0
	for i := 0; i < count; i++ {
		if indeg[i] == 0 {
			in0++
		}
		if outDeg[i] == 0 {
			out0++
		}
	}

	fmt.Fprintln(out, max(in0, out0))
}

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

	var n, m int
	fmt.Fscan(in, &n, &m)

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

	count, belong := StronglyConnectedComponent(graph)
	group := make([][]int, count)
	for i := 0; i < n; i++ {
		group[belong[i]] = append(group[belong[i]], i)
	}
	fmt.Fprintln(out, count)
	for _, p := range group {
		fmt.Fprint(out, len(p))
		for _, v := range p {
			fmt.Fprint(out, " ", v)
		}
		fmt.Fprintln(out)
	}
}

func yuki1293() {
	// https://yukicoder.me/problems/no/1293
	// No.1293 2種類の道路-SCC
	// 无向图中有两种路径,各有road1,road2条
	// 求有多少个二元组(a,b),满足从a到b经过 '若干条第一种路径+若干条第二种路径'

	// !每个点i拆成点2*i和点2*i+1,2*i->2*i+1
	// !第一种路径: 2*i<->2*j
	// !第二种路径: 2*i+1<->2*j+1
	// 然后对每个顶点求出有多少个可以到达自己

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

	var n, road1, road2 int
	fmt.Fscan(in, &n, &road1, &road2)
	graph := make([][]int, 2*n)
	for i := 0; i < road1; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		a, b = a-1, b-1
		graph[2*a] = append(graph[2*a], 2*b)
		graph[2*b] = append(graph[2*b], 2*a)
	}
	for i := 0; i < road2; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		a, b = a-1, b-1
		graph[2*a+1] = append(graph[2*a+1], 2*b+1)
		graph[2*b+1] = append(graph[2*b+1], 2*a+1)
	}
	for i := 0; i < n; i++ {
		graph[2*i] = append(graph[2*i], 2*i+1)
	}

	count, belong := StronglyConnectedComponent(graph)
	dag := SCCDag(graph, count, belong)
	dp := make([]int, count)
	for i := 0; i < n; i++ {
		dp[belong[2*i]]++
	}
	for i := 0; i < count; i++ {
		for _, to := range dag[i] {
			dp[to] += dp[i]
		}
	}

	res := 0
	for i := 0; i < n; i++ {
		res += dp[belong[2*i+1]] - 1 // !减去自己到自己的路径1
	}
	fmt.Fprintln(out, res)
}

// 有向图强连通分量分解.
func StronglyConnectedComponent(graph [][]int) (count int, belong []int) {
	n := len(graph)
	belong = make([]int, n)
	low := make([]int, n)
	order := make([]int, n)
	for i := range order {
		order[i] = -1
	}
	now := 0
	path := []int{}

	var dfs func(int)
	dfs = func(v int) {
		low[v] = now
		order[v] = now
		now++
		path = append(path, v)
		for _, to := range graph[v] {
			if order[to] == -1 {
				dfs(to)
				low[v] = min(low[v], low[to])
			} else {
				low[v] = min(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 := 0; i < n; i++ {
		if order[i] == -1 {
			dfs(i)
		}
	}
	for i := 0; i < n; i++ {
		belong[i] = count - 1 - belong[i]
	}
	return
}

// 有向图的强连通分量缩点.
func SCCDag(graph [][]int, count int, belong []int) (dag [][]int) {
	dag = make([][]int, count)
	adjSet := make([]map[int]struct{}, count)
	for i := 0; i < count; i++ {
		adjSet[i] = make(map[int]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 := 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
}
0