結果
問題 | No.2780 The Bottle Imp |
ユーザー | naut3 |
提出日時 | 2024-06-07 22:14:45 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,853 bytes |
コンパイル時間 | 17,848 ms |
コンパイル使用メモリ | 378,460 KB |
実行使用メモリ | 28,800 KB |
最終ジャッジ日時 | 2024-06-08 10:32:22 |
合計ジャッジ時間 | 18,440 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 66 ms
11,136 KB |
testcase_08 | AC | 66 ms
11,264 KB |
testcase_09 | AC | 67 ms
11,264 KB |
testcase_10 | AC | 68 ms
11,136 KB |
testcase_11 | AC | 64 ms
11,136 KB |
testcase_12 | AC | 143 ms
17,348 KB |
testcase_13 | AC | 137 ms
17,328 KB |
testcase_14 | AC | 19 ms
8,320 KB |
testcase_15 | AC | 19 ms
8,320 KB |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | AC | 19 ms
8,320 KB |
testcase_19 | AC | 19 ms
8,448 KB |
testcase_20 | AC | 19 ms
8,320 KB |
testcase_21 | WA | - |
testcase_22 | AC | 18 ms
6,016 KB |
testcase_23 | AC | 18 ms
7,424 KB |
testcase_24 | AC | 53 ms
9,600 KB |
testcase_25 | AC | 100 ms
13,952 KB |
testcase_26 | AC | 38 ms
9,600 KB |
testcase_27 | AC | 20 ms
11,320 KB |
testcase_28 | AC | 20 ms
11,448 KB |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | AC | 41 ms
18,304 KB |
testcase_32 | AC | 11 ms
6,912 KB |
testcase_33 | AC | 57 ms
28,800 KB |
testcase_34 | AC | 110 ms
27,008 KB |
testcase_35 | AC | 7 ms
5,376 KB |
testcase_36 | AC | 1 ms
5,376 KB |
testcase_37 | WA | - |
testcase_38 | AC | 7 ms
5,376 KB |
testcase_39 | AC | 36 ms
19,116 KB |
testcase_40 | AC | 36 ms
19,092 KB |
testcase_41 | AC | 37 ms
19,224 KB |
testcase_42 | AC | 1 ms
5,376 KB |
testcase_43 | WA | - |
ソースコード
#![allow(non_snake_case, unused_imports)] use proconio::{fastout, input, marker::*}; #[fastout] fn main() { input! { N: usize, } let mut edges = vec![]; for i in 0..N { input! { M: usize, A: [Usize1; M], } for a in A { edges.push((i, a)); } } let scc = strongly_connected_components(N, &edges); let (z, zi) = coordinate_compression(&scc); let n = z.len(); let mut edges_set = std::collections::HashSet::new(); let mut deg_out = vec![0; n]; for (u, v) in edges { if scc[u] == scc[v] { continue; } edges_set.insert((*zi.get(&scc[u]).unwrap(), *zi.get(&scc[v]).unwrap())); deg_out[*zi.get(&scc[u]).unwrap()] += 1; } let ord = topological_sort( n, &edges_set.iter().map(|&(u, v)| (u, v)).collect::<Vec<_>>(), ); // eprintln!( // "scc: {}", // scc.iter() // .map(|x| x.to_string()) // .collect::<Vec<_>>() // .join(" ") // ); // eprintln!( // "ord: {}", // ord.iter() // .map(|x| x.to_string()) // .collect::<Vec<_>>() // .join(" ") // ); for i in 0..n - 1 { if deg_out[ord[i]] == 0 { println!("No"); return; } } println!("Yes"); } fn coordinate_compression<T: std::cmp::Ord + Clone + Copy>( values: &[T], ) -> (Vec<T>, std::collections::BTreeMap<T, usize>) { let s: std::collections::BTreeSet<T> = values.iter().cloned().collect(); let z: Vec<T> = s.iter().cloned().collect(); let zinv: std::collections::BTreeMap<T, usize> = z.iter().enumerate().map(|(i, &v)| (v, i)).collect(); (z, zinv) } fn topological_sort(N: usize, E: &[(usize, usize)]) -> Vec<usize> { let mut graph = vec![vec![]; N]; let mut out_degree = vec![0; N]; let mut ord = vec![]; for &(u, v) in E { graph[v].push(u); out_degree[u] += 1; } for i in 0..N { graph[i].sort(); } let mut q = std::collections::VecDeque::new(); for i in 0..N { if out_degree[i] == 0 { q.push_back(i); } } while let Some(now) = q.pop_front() { ord.push(now); for &nxt in graph[now].iter() { out_degree[nxt] -= 1; if out_degree[nxt] == 0 { q.push_back(nxt); } } } ord.reverse(); return ord; } fn strongly_connected_components(N: usize, E: &Vec<(usize, usize)>) -> Vec<usize> { struct Graph { list: Vec<Vec<usize>>, seen: Vec<bool>, stop: Vec<usize>, } impl Graph { fn dfs(&mut self, now: usize) { self.seen[now] = true; for &nxt in self.list[now].clone().iter() { if self.seen[nxt] { continue; } self.dfs(nxt); } self.stop.push(now); } } let mut g = vec![vec![]; N]; let mut g_inv = vec![vec![]; N]; for &(u, v) in E.iter() { g[u].push(v); g_inv[v].push(u); } let mut G = Graph { list: g, seen: vec![false; N], stop: vec![], }; for i in 0..N { if !G.seen[i] { G.dfs(i); } } let mut stop = G.stop; stop.reverse(); let mut G_inv = Graph { list: g_inv, seen: vec![false; N], stop: vec![], }; let mut ret = vec![0; N]; let mut cnt = 0; for i in stop { if !G_inv.seen[i] { G_inv.dfs(i); for &v in G_inv.stop.iter() { ret[v] = cnt; } G_inv.stop = vec![]; cnt += 1; } } return ret; }