結果
問題 | No.1254 補強への架け橋 |
ユーザー |
|
提出日時 | 2023-01-08 15:26:25 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 115 ms / 2,000 ms |
コード長 | 2,143 bytes |
コンパイル時間 | 25,733 ms |
コンパイル使用メモリ | 402,944 KB |
実行使用メモリ | 29,056 KB |
最終ジャッジ日時 | 2024-12-15 18:26:47 |
合計ジャッジ時間 | 37,115 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 123 |
コンパイルメッセージ
warning: field `n` is never read --> src/main.rs:3:5 | 2 | struct UnionFind { | --------- field in this struct 3 | n: usize, | ^ | = note: `#[warn(dead_code)]` on by default
ソースコード
struct UnionFind {n: usize,parents: Vec<usize>,}impl UnionFind {fn new(n: usize) -> Self {UnionFind {n: n,parents: (0..n).collect(),}}fn equiv(&mut self, a: usize, b: usize) -> bool {self.find(a) == self.find(b)}fn unite(&mut self, a: usize, b: usize) {if self.equiv(a, b) { return; }let (a, b) = (a.min(b), a.max(b));let x = self.parents[a];let y = self.parents[b];self.parents[y] = self.parents[x];}fn find(&mut self, a: usize) -> usize {if self.parents[a] == a { return a; }let p = self.find(self.parents[a]);self.parents[a] = p;p}}fn dfs(cur: usize, prev: usize, goal: usize, paths: &Vec<Vec<(usize, usize)>>, used: &mut Vec<usize>) -> bool {if cur == goal {return true;}for &(v, bidx) in paths[cur].iter() {if v == prev { continue; }used.push(bidx);if dfs(v, cur, goal, paths, used) {return true;}used.pop();}false}fn main() {let mut n = String::new();std::io::stdin().read_line(&mut n).ok();let n: usize = n.trim().parse().unwrap();let mut paths = vec![vec![]; n];let mut uf = UnionFind::new(n);let mut start = 0usize;let mut end = 0usize;let mut bridge = 0usize;for i in 0..n {let mut temp = String::new();std::io::stdin().read_line(&mut temp).ok();let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();let u = temp[0]-1;let v = temp[1]-1;if uf.find(u) == uf.find(v) {start = u;end = v;bridge = i;} else {paths[u].push((v, i));paths[v].push((u, i));uf.unite(u, v);}}let mut used = vec![];dfs(start, start, end, &paths, &mut used);used.push(bridge);used.sort();println!("{}", used.len());println!("{}", used.iter().map(|v| (v+1).to_string()).collect::<Vec<_>>().join(" "));}