結果
問題 | No.2780 The Bottle Imp |
ユーザー | naut3 |
提出日時 | 2024-06-07 22:24:51 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,042 bytes |
コンパイル時間 | 12,792 ms |
コンパイル使用メモリ | 378,152 KB |
実行使用メモリ | 28,664 KB |
最終ジャッジ日時 | 2024-06-08 10:33:46 |
合計ジャッジ時間 | 15,451 ms |
ジャッジサーバーID (参考情報) |
judge5 / 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 | 60 ms
11,264 KB |
testcase_08 | AC | 52 ms
11,136 KB |
testcase_09 | AC | 54 ms
11,136 KB |
testcase_10 | AC | 52 ms
11,136 KB |
testcase_11 | AC | 52 ms
11,136 KB |
testcase_12 | AC | 117 ms
17,800 KB |
testcase_13 | AC | 110 ms
18,036 KB |
testcase_14 | AC | 16 ms
8,448 KB |
testcase_15 | AC | 15 ms
8,448 KB |
testcase_16 | AC | 15 ms
8,448 KB |
testcase_17 | AC | 15 ms
8,320 KB |
testcase_18 | AC | 16 ms
8,320 KB |
testcase_19 | AC | 15 ms
8,320 KB |
testcase_20 | AC | 15 ms
8,320 KB |
testcase_21 | AC | 15 ms
8,320 KB |
testcase_22 | AC | 16 ms
5,888 KB |
testcase_23 | AC | 15 ms
7,424 KB |
testcase_24 | AC | 43 ms
9,600 KB |
testcase_25 | AC | 80 ms
13,952 KB |
testcase_26 | AC | 28 ms
9,600 KB |
testcase_27 | AC | 16 ms
11,316 KB |
testcase_28 | AC | 16 ms
11,320 KB |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | AC | 35 ms
18,432 KB |
testcase_32 | AC | 9 ms
6,912 KB |
testcase_33 | AC | 48 ms
28,664 KB |
testcase_34 | AC | 91 ms
27,040 KB |
testcase_35 | AC | 6 ms
5,376 KB |
testcase_36 | AC | 1 ms
5,376 KB |
testcase_37 | WA | - |
testcase_38 | AC | 6 ms
5,376 KB |
testcase_39 | AC | 31 ms
19,100 KB |
testcase_40 | AC | 28 ms
19,228 KB |
testcase_41 | AC | 29 ms
19,096 KB |
testcase_42 | AC | 1 ms
5,376 KB |
testcase_43 | AC | 1 ms
5,376 KB |
ソースコード
#![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]; let mut deg_in = 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; deg_in[*zi.get(&scc[v]).unwrap()] += 1; } let ord = topological_sort( n, &edges_set.iter().map(|&(u, v)| (u, v)).collect::<Vec<_>>(), ); let s = *zi.get(&scc[0]).unwrap(); if ord[s] != 0 { println!("No"); return; } // 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; }