結果
問題 | No.1983 [Cherry 4th Tune C] 南の島のマーメイド |
ユーザー | ArcAki |
提出日時 | 2024-08-13 03:12:40 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,172 bytes |
コンパイル時間 | 14,720 ms |
コンパイル使用メモリ | 377,696 KB |
実行使用メモリ | 40,192 KB |
最終ジャッジ日時 | 2024-08-13 03:13:24 |
合計ジャッジ時間 | 41,257 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 9 ms
5,376 KB |
testcase_09 | AC | 12 ms
5,376 KB |
testcase_10 | AC | 22 ms
5,376 KB |
testcase_11 | AC | 20 ms
5,376 KB |
testcase_12 | AC | 9 ms
5,376 KB |
testcase_13 | AC | 296 ms
15,488 KB |
testcase_14 | AC | 780 ms
18,560 KB |
testcase_15 | AC | 273 ms
19,712 KB |
testcase_16 | AC | 176 ms
13,184 KB |
testcase_17 | AC | 1,236 ms
19,584 KB |
testcase_18 | AC | 209 ms
19,072 KB |
testcase_19 | AC | 474 ms
24,576 KB |
testcase_20 | AC | 227 ms
18,304 KB |
testcase_21 | AC | 517 ms
20,352 KB |
testcase_22 | AC | 2,645 ms
23,808 KB |
testcase_23 | AC | 1,388 ms
28,928 KB |
testcase_24 | AC | 1,396 ms
28,928 KB |
testcase_25 | AC | 1,445 ms
28,928 KB |
testcase_26 | AC | 1,422 ms
28,928 KB |
testcase_27 | AC | 1,401 ms
28,928 KB |
testcase_28 | AC | 1,419 ms
29,056 KB |
testcase_29 | WA | - |
testcase_30 | AC | 1,435 ms
28,928 KB |
testcase_31 | AC | 1,413 ms
28,928 KB |
testcase_32 | AC | 1,430 ms
28,928 KB |
testcase_33 | AC | 1 ms
5,376 KB |
testcase_34 | AC | 261 ms
13,568 KB |
testcase_35 | AC | 326 ms
40,192 KB |
testcase_36 | AC | 311 ms
26,112 KB |
testcase_37 | AC | 1 ms
5,376 KB |
testcase_38 | AC | 238 ms
5,376 KB |
testcase_39 | AC | 328 ms
40,064 KB |
testcase_40 | AC | 322 ms
27,520 KB |
ソースコード
use std::io::{self, BufRead}; use std::mem::swap; struct UnionFind{ parent: Vec<usize>, num: Vec<i64>, } impl UnionFind{ fn new(n: usize)->Self{ let parent = (0..n).into_iter().collect(); let num = vec![1; n]; UnionFind{ parent, num, } } fn find(&mut self, x: usize)->usize{ if x==self.parent[x]{return x;} self.parent[x] = self.find(self.parent[x]); self.parent[x] } fn union(&mut self, u: usize, v: usize){ let (mut pu, mut pv) = (self.find(u), self.find(v)); if pu==pv{return;} if self.num[pu] < self.num[pv]{ swap(&mut pu, &mut pv); } self.num[pu] += self.num[pv]; self.parent[pv] = pu; } fn same(&mut self, u: usize, v: usize)->bool{ self.find(u)==self.find(v) } } struct CycleFinder { n: usize, edge: Vec<Vec<usize>>, went: Vec<bool>, used: Vec<bool>, cycle: Vec<bool>, way: Vec<usize>, } impl CycleFinder{ fn new(n: usize)->Self{ let edge = vec![Vec::new(); n]; let went = vec![false; n]; let used = vec![false; n]; let cycle = vec![false; n]; let way = Vec::new(); CycleFinder{n, edge, went, used, cycle, way} } fn add_edge(&mut self, u: usize, v: usize){ assert!(u < self.n && v < self.n); self.edge[u].push(v); self.edge[v].push(u); } fn build(&mut self){ for i in 0..self.n{ if self.used[i]{continue} self._dfs(i, !0); } } fn _dfs(&mut self, x: usize, pre: usize) { self.went[x] = true; self.way.push(x); let edges_ptr = self.edge[x].as_ptr(); let edges_len = self.edge[x].len(); for i in 0..edges_len { let nex = unsafe { *edges_ptr.add(i) }; if self.used[nex] || nex==pre{ continue; } if self.went[nex] { let t = nex; let mut p = self.way.len() - 1; while self.way[p] != t { self.cycle[self.way[p]] = true; p -= 1; } self.cycle[t] = true; } else { self._dfs(nex, x); } } self.used[x] = true; self.way.pop(); } fn include_cycle(&self)->Vec<bool>{ self.cycle.clone() } } fn main() { //input! { // n: usize, m: usize, q: usize, // e: [(usize, usize); m], // query: [(usize, usize); q] //} //ここからの入力はgptです…やりたいことはただ以上のようなものです。 let stdin = io::stdin(); let mut iterator = stdin.lock().lines(); let first_line = iterator.next().unwrap().unwrap(); let mut iter = first_line.split_whitespace(); let n: usize = iter.next().unwrap().parse().unwrap(); let m: usize = iter.next().unwrap().parse().unwrap(); let q: usize = iter.next().unwrap().parse().unwrap(); let mut e = Vec::with_capacity(m); for _ in 0..m { let line = iterator.next().unwrap().unwrap(); let mut iter = line.split_whitespace(); let u: usize = iter.next().unwrap().parse().unwrap(); let v: usize = iter.next().unwrap().parse().unwrap(); e.push((u, v)); } let mut query = Vec::with_capacity(q); for _ in 0..q { let line = iterator.next().unwrap().unwrap(); let mut iter = line.split_whitespace(); let u: usize = iter.next().unwrap().parse().unwrap(); let v: usize = iter.next().unwrap().parse().unwrap(); query.push((u, v)); } //↑ここまでの入力はgptです。 let mut cf = CycleFinder::new(n); for &(u, v) in &e{ cf.add_edge(u-1, v-1); } cf.build(); let cycle = cf.include_cycle(); let mut uf = UnionFind::new(n); for &(u, v) in &e{ if !(cycle[u-1]&&cycle[v-1]){ uf.union(u-1, v-1); } } for &(u, v) in &query{ if uf.same(u-1, v-1){ println!("Yes"); } else { println!("No"); } } }