結果
問題 | No.1293 2種類の道路 |
ユーザー | 草苺奶昔 |
提出日時 | 2023-03-15 13:56:49 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 617 ms / 2,000 ms |
コード長 | 3,424 bytes |
コンパイル時間 | 11,772 ms |
コンパイル使用メモリ | 235,632 KB |
実行使用メモリ | 88,180 KB |
最終ジャッジ日時 | 2024-09-18 08:43:52 |
合計ジャッジ時間 | 20,222 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 617 ms
85,564 KB |
testcase_10 | AC | 581 ms
83,968 KB |
testcase_11 | AC | 597 ms
84,480 KB |
testcase_12 | AC | 588 ms
86,016 KB |
testcase_13 | AC | 601 ms
85,120 KB |
testcase_14 | AC | 375 ms
63,872 KB |
testcase_15 | AC | 385 ms
64,512 KB |
testcase_16 | AC | 437 ms
82,816 KB |
testcase_17 | AC | 437 ms
88,180 KB |
testcase_18 | AC | 273 ms
59,400 KB |
testcase_19 | AC | 364 ms
69,376 KB |
testcase_20 | AC | 360 ms
69,376 KB |
testcase_21 | AC | 220 ms
43,876 KB |
testcase_22 | AC | 218 ms
46,208 KB |
testcase_23 | AC | 213 ms
44,160 KB |
ソースコード
package main import ( "bufio" "fmt" "os" ) func main() { in := bufio.NewReader(os.Stdin) out := bufio.NewWriter(os.Stdout) defer out.Flush() var n, road1, road2 int fmt.Fscan(in, &n, &road1, &road2) scc := NewStronglyConnectedComponents(2 * n) for i := 0; i < road1; i++ { var a, b int fmt.Fscan(in, &a, &b) a, b = a-1, b-1 scc.AddEdge(2*a, 2*b, 1) scc.AddEdge(2*b, 2*a, 1) } for i := 0; i < road2; i++ { var a, b int fmt.Fscan(in, &a, &b) a, b = a-1, b-1 scc.AddEdge(2*a+1, 2*b+1, 1) scc.AddEdge(2*b+1, 2*a+1, 1) } for i := 0; i < n; i++ { scc.AddEdge(2*i, 2*i+1, 1) } scc.Build() v := len(scc.Group) dp := make([]int, v) for i := 0; i < n; i++ { dp[scc.CompId[2*i]]++ } for i := 0; i < v; i++ { for _, to := range scc.Dag[i] { dp[to] += dp[i] } } res := 0 for i := 0; i < n; i++ { res += dp[scc.CompId[2*i+1]] - 1 // !减去自己到自己的路径1 } fmt.Fprintln(out, res) } type WeightedEdge struct{ from, to, cost, index int } type StronglyConnectedComponents struct { G [][]WeightedEdge // 原图 Dag [][]int // 强连通分量缩点后的DAG(有向图邻接表) CompId []int // 每个顶点所属的强连通分量的编号 Group [][]int // 每个强连通分量所包含的顶点 rg [][]WeightedEdge order []int used []bool eid int } func NewStronglyConnectedComponents(n int) *StronglyConnectedComponents { return &StronglyConnectedComponents{G: make([][]WeightedEdge, n)} } func (scc *StronglyConnectedComponents) AddEdge(from, to, cost int) { scc.G[from] = append(scc.G[from], WeightedEdge{from, to, cost, scc.eid}) scc.eid++ } func (scc *StronglyConnectedComponents) Build() { scc.rg = make([][]WeightedEdge, len(scc.G)) for i := range scc.G { for _, e := range scc.G[i] { scc.rg[e.to] = append(scc.rg[e.to], WeightedEdge{e.to, e.from, e.cost, e.index}) } } scc.CompId = make([]int, len(scc.G)) for i := range scc.CompId { scc.CompId[i] = -1 } scc.used = make([]bool, len(scc.G)) for i := range scc.G { scc.dfs(i) } for i, j := 0, len(scc.order)-1; i < j; i, j = i+1, j-1 { scc.order[i], scc.order[j] = scc.order[j], scc.order[i] } ptr := 0 for _, v := range scc.order { if scc.CompId[v] == -1 { scc.rdfs(v, ptr) ptr++ } } dag := make([][]int, ptr) visited := make(map[int]struct{}) // 边去重 for i := range scc.G { for _, e := range scc.G[i] { x, y := scc.CompId[e.from], scc.CompId[e.to] if x == y { continue // 原来的边 x->y 的顶点在同一个强连通分量内,可以汇合同一个 SCC 的权值 } hash := x*len(scc.G) + y if _, ok := visited[hash]; !ok { dag[x] = append(dag[x], y) visited[hash] = struct{}{} } } } scc.Dag = dag scc.Group = make([][]int, ptr) for i := range scc.G { scc.Group[scc.CompId[i]] = append(scc.Group[scc.CompId[i]], i) } } // 获取顶点k所属的强连通分量的编号 func (scc *StronglyConnectedComponents) Get(k int) int { return scc.CompId[k] } func (scc *StronglyConnectedComponents) dfs(idx int) { tmp := scc.used[idx] scc.used[idx] = true if tmp { return } for _, e := range scc.G[idx] { scc.dfs(e.to) } scc.order = append(scc.order, idx) } func (scc *StronglyConnectedComponents) rdfs(idx int, cnt int) { if scc.CompId[idx] != -1 { return } scc.CompId[idx] = cnt for _, e := range scc.rg[idx] { scc.rdfs(e.to, cnt) } }