結果

問題 No.1293 2種類の道路
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-12-24 02:37:56
言語 Go
(1.22.1)
結果
AC  
実行時間 349 ms / 2,000 ms
コード長 3,603 bytes
コンパイル時間 12,076 ms
コンパイル使用メモリ 228,056 KB
実行使用メモリ 46,540 KB
最終ジャッジ日時 2023-12-24 02:38:13
合計ジャッジ時間 16,649 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 328 ms
39,592 KB
testcase_10 AC 322 ms
39,580 KB
testcase_11 AC 349 ms
39,548 KB
testcase_12 AC 328 ms
39,580 KB
testcase_13 AC 315 ms
39,592 KB
testcase_14 AC 203 ms
39,948 KB
testcase_15 AC 227 ms
39,948 KB
testcase_16 AC 281 ms
31,472 KB
testcase_17 AC 271 ms
39,416 KB
testcase_18 AC 180 ms
37,248 KB
testcase_19 AC 213 ms
44,972 KB
testcase_20 AC 225 ms
46,540 KB
testcase_21 AC 164 ms
10,200 KB
testcase_22 AC 168 ms
10,204 KB
testcase_23 AC 157 ms
10,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// StronglyConnectedComponent-有向图SCC

package main

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

func main() {
	// yosupo()
	yuki1293()
}

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
}
0