// ---------- begin Strongly Connected Components ---------- struct SCC { size: usize, edge: Vec<(u32, u32)>, id: Vec, } impl SCC { pub fn new(size: usize) -> Self { SCC { size: size, edge: vec![], id: Vec::with_capacity(size), } } pub fn add_edge(&mut self, from: usize, to: usize) { assert!(from < self.size && to < self.size); self.edge.push((from as u32, to as u32)); } fn fill_buffer(&self, buf: &mut [u32], cnt: &[u32], inv: bool) { let mut index = vec![0; self.size]; let mut sum = 0; for (index, c) in index.iter_mut().zip(cnt.iter()) { *index = sum; sum += *c; } for e in self.edge.iter() { let (f, t) = if inv { (e.1 as usize, e.0) } else { (e.0 as usize, e.1) }; buf[index[f] as usize] = t; index[f] += 1; } } fn build_graph<'a>(&self, buf: &'a mut [u32], cnt: &'a [u32], inv: bool) -> Vec<&'a mut [u32]> { let size = self.size; self.fill_buffer(buf, cnt, inv); let mut buf = Some(buf); let mut ans = Vec::with_capacity(size); for c in cnt.iter() { let len = *c as usize; let x = buf.take().unwrap(); let (x, y) = x.split_at_mut(len); ans.push(x); buf = Some(y); } ans } fn dfs1(&self, buf: &mut [u32], cnt: &[u32], q: &mut Vec) { let size = self.size; let graph = self.build_graph(buf, cnt, false); let mut visited = vec![false; size]; let mut stack = vec![]; for v in 0..size { if visited[v] { continue; } visited[v] = true; stack.push((v, graph[v].iter())); while let Some((v, mut it)) = stack.pop() { let mut finish = true; while let Some(&u) = it.next() { let u = u as usize; if !visited[u] { visited[u] = true; finish = false; stack.push((v, it)); stack.push((u, graph[u].iter())); break; } } if finish { q.push(v as u32); } } } } fn dfs2(&mut self, buf: &mut [u32], cnt: &[u32], q: &[u32]) { let size = self.size; let inv_graph = self.build_graph(buf, cnt, true); self.id.clear(); self.id.resize(size, size); let mut counter = 0; let mut stack = vec![]; for &v in q.iter().rev() { let v = v as usize; if self.id[v] < size { continue; } stack.push(v); while let Some(v) = stack.pop() { self.id[v] = counter; for &u in inv_graph[v].iter() { let u = u as usize; if self.id[u] == size { self.id[u] = counter; stack.push(u); } } } counter += 1; } } pub fn build(&mut self) { let size = self.size; let mut cnt = vec![0; size]; let mut inv_cnt = vec![0; size]; for e in self.edge.iter() { cnt[e.0 as usize] += 1; inv_cnt[e.1 as usize] += 1; } let mut buf = vec![0; self.edge.len()]; let buf = buf.as_mut_slice(); let mut q = Vec::with_capacity(size); self.dfs1(buf, &cnt, &mut q); self.dfs2(buf, &inv_cnt, &q); } pub fn get_array(&self) -> Vec { self.id.clone() } } // ---------- end Strongly Connected Components ---------- //---------- begin union_find ---------- #[allow(dead_code)] mod union_find { pub struct UF { p: Vec, } impl UF { pub fn new(n: usize) -> UF { UF {p: vec![-1; n] } } pub fn init(&mut self) { for p in self.p.iter_mut() { *p = -1; } } pub fn root(&self, mut x: usize) -> usize { while self.p[x] >= 0 { x = self.p[x] as usize; } x } pub fn same(&self, x: usize, y: usize) -> bool { self.root(x) == self.root(y) } pub fn unite(&mut self, mut x: usize, mut y: usize) -> Option<(usize, usize)> { x = self.root(x); y = self.root(y); if x == y { return None; } if self.p[x] > self.p[y] { let s = x; x = y; y = s; } self.p[x] += self.p[y]; self.p[y] = x as i32; Some((x, y)) } pub fn get_size(&self, x: usize) -> usize { let r = self.root(x); (-self.p[r]) as usize } } } //---------- end union_find ---------- //https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // fn run() { input! { n: usize, m: usize, e: [(usize1, usize1, usize); m], } let mut u = union_find::UF::new(n); for &(a, b, c) in e.iter() { if c == 1 { if u.unite(a, b).is_none() { println!("Yes"); return; } } } let mut id = vec![n; n]; let mut k = 0; for i in 0..n { let r = u.root(i); if id[r] == n { id[r] = k; k += 1; } } let mut scc = SCC::new(k); for &(a, b, c) in e.iter() { if c == 2 { let a = u.root(a); let b = u.root(b); scc.add_edge(id[a], id[b]); } } scc.build(); if *scc.get_array().iter().max().unwrap() == k - 1 { println!("No"); } else { println!("Yes"); } } fn main() { run(); }