結果

問題 No.1293 2種類の道路
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-12-24 02:37:56
言語 Go
(1.22.1)
結果
AC  
実行時間 386 ms / 2,000 ms
コード長 3,603 bytes
コンパイル時間 12,402 ms
コンパイル使用メモリ 237,232 KB
実行使用メモリ 45,656 KB
最終ジャッジ日時 2024-09-27 13:34:28
合計ジャッジ時間 17,394 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 1 ms
6,812 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 377 ms
39,284 KB
testcase_10 AC 377 ms
37,224 KB
testcase_11 AC 386 ms
39,288 KB
testcase_12 AC 374 ms
39,280 KB
testcase_13 AC 376 ms
39,284 KB
testcase_14 AC 253 ms
41,292 KB
testcase_15 AC 253 ms
42,652 KB
testcase_16 AC 297 ms
31,196 KB
testcase_17 AC 311 ms
39,488 KB
testcase_18 AC 210 ms
36,848 KB
testcase_19 AC 265 ms
45,656 KB
testcase_20 AC 256 ms
42,456 KB
testcase_21 AC 170 ms
10,256 KB
testcase_22 AC 173 ms
10,252 KB
testcase_23 AC 167 ms
12,404 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