結果
問題 | No.1254 補強への架け橋 |
ユーザー | 草苺奶昔 |
提出日時 | 2023-02-13 02:24:05 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 222 ms / 2,000 ms |
コード長 | 4,215 bytes |
コンパイル時間 | 14,246 ms |
コンパイル使用メモリ | 233,680 KB |
実行使用メモリ | 45,264 KB |
最終ジャッジ日時 | 2024-07-16 08:19:17 |
合計ジャッジ時間 | 26,933 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,944 KB |
testcase_09 | AC | 1 ms
6,940 KB |
testcase_10 | AC | 1 ms
6,944 KB |
testcase_11 | AC | 1 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 1 ms
6,944 KB |
testcase_14 | AC | 1 ms
6,944 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 1 ms
6,944 KB |
testcase_17 | AC | 1 ms
6,940 KB |
testcase_18 | AC | 1 ms
6,940 KB |
testcase_19 | AC | 1 ms
6,940 KB |
testcase_20 | AC | 1 ms
6,940 KB |
testcase_21 | AC | 1 ms
6,940 KB |
testcase_22 | AC | 1 ms
6,944 KB |
testcase_23 | AC | 1 ms
6,944 KB |
testcase_24 | AC | 2 ms
6,940 KB |
testcase_25 | AC | 2 ms
6,944 KB |
testcase_26 | AC | 1 ms
6,940 KB |
testcase_27 | AC | 1 ms
6,944 KB |
testcase_28 | AC | 2 ms
6,940 KB |
testcase_29 | AC | 1 ms
6,940 KB |
testcase_30 | AC | 1 ms
6,940 KB |
testcase_31 | AC | 1 ms
6,940 KB |
testcase_32 | AC | 1 ms
6,944 KB |
testcase_33 | AC | 2 ms
6,944 KB |
testcase_34 | AC | 1 ms
6,940 KB |
testcase_35 | AC | 1 ms
6,940 KB |
testcase_36 | AC | 1 ms
6,944 KB |
testcase_37 | AC | 1 ms
6,944 KB |
testcase_38 | AC | 1 ms
6,940 KB |
testcase_39 | AC | 2 ms
6,940 KB |
testcase_40 | AC | 1 ms
6,944 KB |
testcase_41 | AC | 2 ms
6,940 KB |
testcase_42 | AC | 2 ms
6,940 KB |
testcase_43 | AC | 2 ms
6,940 KB |
testcase_44 | AC | 2 ms
6,940 KB |
testcase_45 | AC | 2 ms
6,944 KB |
testcase_46 | AC | 2 ms
6,940 KB |
testcase_47 | AC | 2 ms
6,940 KB |
testcase_48 | AC | 3 ms
6,940 KB |
testcase_49 | AC | 1 ms
6,944 KB |
testcase_50 | AC | 2 ms
6,940 KB |
testcase_51 | AC | 3 ms
6,940 KB |
testcase_52 | AC | 2 ms
6,940 KB |
testcase_53 | AC | 2 ms
6,944 KB |
testcase_54 | AC | 2 ms
6,940 KB |
testcase_55 | AC | 2 ms
6,944 KB |
testcase_56 | AC | 2 ms
6,940 KB |
testcase_57 | AC | 2 ms
6,944 KB |
testcase_58 | AC | 2 ms
6,944 KB |
testcase_59 | AC | 1 ms
6,940 KB |
testcase_60 | AC | 2 ms
6,940 KB |
testcase_61 | AC | 2 ms
6,940 KB |
testcase_62 | AC | 1 ms
6,944 KB |
testcase_63 | AC | 15 ms
7,724 KB |
testcase_64 | AC | 4 ms
6,944 KB |
testcase_65 | AC | 8 ms
6,944 KB |
testcase_66 | AC | 8 ms
6,940 KB |
testcase_67 | AC | 4 ms
6,944 KB |
testcase_68 | AC | 8 ms
6,940 KB |
testcase_69 | AC | 9 ms
6,940 KB |
testcase_70 | AC | 5 ms
6,940 KB |
testcase_71 | AC | 4 ms
6,944 KB |
testcase_72 | AC | 10 ms
6,944 KB |
testcase_73 | AC | 4 ms
6,944 KB |
testcase_74 | AC | 11 ms
6,944 KB |
testcase_75 | AC | 7 ms
6,940 KB |
testcase_76 | AC | 3 ms
6,940 KB |
testcase_77 | AC | 6 ms
6,940 KB |
testcase_78 | AC | 13 ms
7,736 KB |
testcase_79 | AC | 13 ms
7,736 KB |
testcase_80 | AC | 12 ms
6,940 KB |
testcase_81 | AC | 13 ms
7,752 KB |
testcase_82 | AC | 11 ms
6,944 KB |
testcase_83 | AC | 163 ms
31,104 KB |
testcase_84 | AC | 163 ms
28,996 KB |
testcase_85 | AC | 96 ms
18,652 KB |
testcase_86 | AC | 138 ms
24,876 KB |
testcase_87 | AC | 150 ms
27,040 KB |
testcase_88 | AC | 21 ms
7,864 KB |
testcase_89 | AC | 151 ms
29,052 KB |
testcase_90 | AC | 97 ms
20,796 KB |
testcase_91 | AC | 79 ms
16,584 KB |
testcase_92 | AC | 38 ms
10,268 KB |
testcase_93 | AC | 127 ms
24,796 KB |
testcase_94 | AC | 115 ms
22,744 KB |
testcase_95 | AC | 108 ms
20,640 KB |
testcase_96 | AC | 148 ms
26,612 KB |
testcase_97 | AC | 61 ms
14,424 KB |
testcase_98 | AC | 157 ms
29,092 KB |
testcase_99 | AC | 90 ms
18,648 KB |
testcase_100 | AC | 166 ms
29,116 KB |
testcase_101 | AC | 34 ms
10,080 KB |
testcase_102 | AC | 19 ms
6,944 KB |
testcase_103 | AC | 36 ms
10,276 KB |
testcase_104 | AC | 55 ms
12,336 KB |
testcase_105 | AC | 122 ms
22,784 KB |
testcase_106 | AC | 69 ms
15,284 KB |
testcase_107 | AC | 168 ms
31,180 KB |
testcase_108 | AC | 152 ms
29,032 KB |
testcase_109 | AC | 126 ms
22,724 KB |
testcase_110 | AC | 109 ms
20,064 KB |
testcase_111 | AC | 118 ms
22,828 KB |
testcase_112 | AC | 51 ms
12,380 KB |
testcase_113 | AC | 108 ms
20,768 KB |
testcase_114 | AC | 70 ms
18,584 KB |
testcase_115 | AC | 23 ms
8,000 KB |
testcase_116 | AC | 79 ms
16,120 KB |
testcase_117 | AC | 52 ms
14,436 KB |
testcase_118 | AC | 151 ms
26,980 KB |
testcase_119 | AC | 90 ms
18,680 KB |
testcase_120 | AC | 153 ms
26,916 KB |
testcase_121 | AC | 43 ms
12,352 KB |
testcase_122 | AC | 77 ms
15,880 KB |
testcase_123 | AC | 2 ms
6,940 KB |
testcase_124 | AC | 216 ms
45,264 KB |
testcase_125 | AC | 222 ms
45,252 KB |
ソースコード
package main import ( "bufio" "fmt" "os" "sort" ) // int main() { // int N; // cin >> N; // NamoriGraph< int > g(N); // g.read(N); // g.build(); // vector< int > ans; // for(auto &e : g.loop_edges) { // ans.emplace_back(e.idx + 1); // } // sort(begin(ans), end(ans)); // cout << ans.size() << "\n"; // cout << ans << "\n"; // } func main() { const INF int = int(1e18) const MOD int = 998244353 in := bufio.NewReader(os.Stdin) out := bufio.NewWriter(os.Stdout) defer out.Flush() var n int fmt.Fscan(in, &n) g := make([][]Edge, n) for i := 0; i < n; i++ { var a, b int fmt.Fscan(in, &a, &b) a-- b-- g[a] = append(g[a], Edge{a, b, 1, i}) g[b] = append(g[b], Edge{b, a, 1, i}) } ng := NewNamoriGraph(g) ng.Build() res := []int{} for _, e := range ng.LoopEdges { res = append(res, e.index+1) } sort.Ints(res) fmt.Fprintln(out, len(res)) for _, v := range res { fmt.Fprint(out, v, " ") } } type Edge = struct{ from, to, cost, index int } type Graph = [][]Edge type NamoriGraph struct { Forest []Graph LoopEdges []Edge g Graph iv [][]int markId, id []int } func NewNamoriGraph(g Graph) *NamoriGraph { return &NamoriGraph{g: g} } func (ng *NamoriGraph) Build() { n := len(ng.g) deg := make([]int, n) used := make([]bool, n) que := []int{} for i := 0; i < n; i++ { deg[i] = len(ng.g[i]) if deg[i] == 1 { que = append(que, i) used[i] = true } } for len(que) > 0 { idx := que[0] que = que[1:] for _, e := range ng.g[idx] { if used[e.to] { continue } deg[e.to]-- if deg[e.to] == 1 { que = append(que, e.to) used[e.to] = true } } } mx := 0 for _, edges := range ng.g { for _, e := range edges { mx = max(mx, e.index) } } edgeUsed := make([]bool, mx+1) loop := []int{} for i := 0; i < n; i++ { if used[i] { continue } for update := true; update; { update = false loop = append(loop, i) for _, e := range ng.g[i] { if used[e.to] || edgeUsed[e.index] { continue } edgeUsed[e.index] = true ng.LoopEdges = append(ng.LoopEdges, Edge{i, e.to, e.cost, e.index}) i = e.to update = true break } } break } loop = loop[:len(loop)-1] ng.markId = make([]int, n) ng.id = make([]int, n) for i := 0; i < len(loop); i++ { pre := loop[(i+len(loop)-1)%len(loop)] nxt := loop[(i+1)%len(loop)] sz := 0 ng.markId[loop[i]] = i ng.iv = append(ng.iv, []int{}) ng.id[loop[i]] = sz sz++ ng.iv[len(ng.iv)-1] = append(ng.iv[len(ng.iv)-1], loop[i]) for _, e := range ng.g[loop[i]] { if e.to != pre && e.to != nxt { ng.markDfs(e.to, loop[i], i, &sz) } } tree := make(Graph, sz) for _, e := range ng.g[loop[i]] { if e.to != pre && e.to != nxt { tree[ng.id[loop[i]]] = append(tree[ng.id[loop[i]]], Edge{ng.id[loop[i]], ng.id[e.to], e.cost, e.index}) tree[ng.id[e.to]] = append(tree[ng.id[e.to]], Edge{ng.id[e.to], ng.id[loop[i]], e.cost, e.index}) ng.buildDfs(e.to, loop[i], tree) } } ng.Forest = append(ng.Forest, tree) } } // 頂点 kについて, サイクルの tree_id, 振り直された木の頂点番号 id を返す。 func (ng *NamoriGraph) GetId(k int) (treeId, id int) { return ng.markId[k], ng.id[k] } // サイクルの tree_id に付属する頂点 kの`もとの頂点番号`を返す。 // 特に GetInvId(tree_id, 0) はサイクルに含まれていたもとの頂点番号を指す。 func (ng *NamoriGraph) GetInvId(treeId, k int) int { return ng.iv[treeId][k] } // markDfs func (ng *NamoriGraph) markDfs(idx, par, k int, l *int) { ng.markId[idx] = k ng.id[idx] = *l *l++ ng.iv[len(ng.iv)-1] = append(ng.iv[len(ng.iv)-1], idx) for _, e := range ng.g[idx] { if e.to != par { ng.markDfs(e.to, idx, k, l) } } } // buildDfs func (ng *NamoriGraph) buildDfs(idx, par int, tree Graph) { for _, e := range ng.g[idx] { if e.to != par { tree[ng.id[idx]] = append(tree[ng.id[idx]], Edge{ng.id[idx], ng.id[e.to], e.cost, e.index}) tree[ng.id[e.to]] = append(tree[ng.id[e.to]], Edge{ng.id[e.to], ng.id[idx], e.cost, e.index}) ng.buildDfs(e.to, idx, tree) } } } func max(a, b int) int { if a > b { return a } return b }