結果

問題 No.629 グラフの中に眠る門松列
ユーザー aimyaimy
提出日時 2018-01-06 09:25:24
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 63 ms / 4,000 ms
コード長 9,242 bytes
コンパイル時間 19,101 ms
コンパイル使用メモリ 377,740 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-02 11:24:38
合計ジャッジ時間 16,600 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 0 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 0 ms
5,376 KB
testcase_21 AC 55 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 55 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 63 ms
5,376 KB
testcase_26 AC 41 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 10 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 4 ms
5,376 KB
testcase_38 AC 3 ms
5,376 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 3 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: use of deprecated method `core::str::<impl str>::trim_right`: superseded by `trim_end`
   --> src/main.rs:259:9
    |
259 |     buf.trim_right().parse::<T>().ok().unwrap()
    |         ^^^^^^^^^^
    |
    = note: `#[warn(deprecated)]` on by default
help: replace the use of the deprecated method
    |
259 |     buf.trim_end().parse::<T>().ok().unwrap()
    |         ~~~~~~~~

warning: use of deprecated method `core::str::<impl str>::trim_right`: superseded by `trim_end`
   --> src/main.rs:275:22
    |
275 |     let mut it = buf.trim_right().split_whitespace();
    |                      ^^^^^^^^^^
    |
help: replace the use of the deprecated method
    |
275 |     let mut it = buf.trim_end().split_whitespace();
    |                      ~~~~~~~~

warning: use of deprecated method `core::str::<impl str>::trim_right`: superseded by `trim_end`
   --> src/main.rs:294:22
    |
294 |     let mut it = buf.trim_right().split_whitespace();
    |                      ^^^^^^^^^^
    |
help: replace the use of the deprecated method
    |
294 |     let mut it = buf.trim_end().split_whitespace();
    |                      ~~~~~~~~

warning: use of deprecated method `core::str::<impl str>::trim_right`: superseded by `trim_end`
   --> src/main.rs:314:9
    |
314 |     buf.trim_right().split_whitespace().map(|t| t.parse::<T>().ok().unwrap()).collect()
    |         ^^^^^^^^^^
    |
help: replace the use of the deprecated method
    |
314 |     buf.trim_end().split_whitespace().map(|t| t.parse::<T>().ok().unwrap()).collect()
    |         ~~~~~~~~

warning: use of deprecated method `core::str::<impl str>::trim_right`: superseded by `trim_end`
   --> src/main.rs:330:9
    |
330 |     buf.trim_right().chars().collect()
    |         ^^^^^^^^^^
    |
help: replace the use of the deprecated method
    |
330 |     buf.trim_end().chars().collect()
    |         ~~~~~~~~

ソースコード

diff #

#![allow(unused_imports, unused_macros, unknown_lints)]

use std::collections::*;
use std::f64::*;
use std::cmp::*;
use std::cmp::Ordering::*;

macro_rules! dump {($($e:expr), +) => {println!(concat!($(stringify!($e), " = {:?}\n"), +), $($e), +)}}
macro_rules! max {($e0:expr, $($e:expr), +) => {{let mut r = $e0; $(r = max(r, $e);)+ r}}}
macro_rules! min {($e0:expr, $($e:expr), +) => {{let mut r = $e0; $(r = min(r, $e);)+ r}}}
macro_rules! freq {($v:expr) => {{let mut h = HashMap::new(); for x in $v {*h.entry(x).or_insert(0) += 1} h}}}

fn main() {
  let (n,m) = get::tuple::<usize,usize>();
  let xs = get::vec::<usize>();
  let es = get::tuples::<usize,usize>(m);

  let g = Graph::new_nonlabeled(n, &es);

  let is_kadomatsu = |v| {
    let ps = g.path(v, 2);
    ps.into_iter().any(|p| {
      let (v1,v2,v3) = (xs[p[0]-1], xs[p[1]-1], xs[p[2]-1]);
      v1!=v2&&v2!=v3&&v1!=v3 && (v2<v1&&v2<v3 || v2>v1&&v2>v3)
    })
  };

  let ans = if (1..n).into_iter().any(is_kadomatsu) {"YES"} else {"NO"};
  println!("{}", ans)
}

struct Graph {
  size: usize,
  adj: Vec<Vec<(usize, isize)>>
}

impl Clone for Graph {
  fn clone(&self) -> Graph {
    Graph {
      size: self.size,
      adj: self.adj.clone()
    }
  }
}

#[allow(dead_code)]
impl Graph {
  fn new_nonlabeled(n: usize, edges: &[(usize, usize)]) -> Graph {
    let edges = edges.iter().map(|&(a,b)|(a,b,1)).collect::<Vec<_>>();
    Graph::new_labeled(n, &edges)
  }

  fn new_labeled(n: usize, edges: &[(usize, usize, isize)]) -> Graph {
    let mut g: Vec<Vec<(usize, isize)>> = vec![vec![]; n+1];
    for &(a, b, c) in edges {
      g[a].push((b,c));
      g[b].push((a,c));  // delete for digraph
    }
    Graph {size: n, adj: g}
  }

  fn edges(&self) -> Vec<(usize, usize, isize)> {
    let mut buf = vec![];
    for (i, next) in self.adj.iter().enumerate() {
      for &(j, x) in next {
        buf.push((i, j, x))
      }
    }
    buf
  }

  fn extract<F>(&self, p: F) -> Vec<usize> where F: Fn(usize) -> bool {
    (1..self.size+1).into_iter().filter(|&v|p(v)).collect()
  }

  fn degree(&self, v: usize) -> usize {
    self.adj[v].len()
  }

  fn is_adjacent(&self, u: usize, v: usize) -> bool {
    self.adj[u].iter().any(|&(w,_)|w==v) || self.adj[v].iter().any(|&(w,_)|w==u)
  }

  fn is_connected(&self) -> bool {
    self.dfs(1).len() == self.size
  }

  fn is_hitofude(&self) -> bool {
    let deg_odd = self.adj.iter().filter(|vs|vs.len()%2==1).count();
    deg_odd == 0 || deg_odd == 2
  }

  fn path(&self, v: usize, n: usize) -> Vec<Vec<usize>> {
    fn go(g: &Graph, v: usize, n: usize, mut acc: Vec<usize>, buf: &mut Vec<Vec<usize>>) {
      if n == 0 {buf.push(acc.clone()); return}
      for &(w,_) in &g.adj[v] {
        acc.push(w);
        go(g, w, n-1, acc.clone(), buf);
        let _ = acc.pop();
      }
    }
    
    let mut buf = vec![];
    let acc = vec![v];
    go(self, v, n, acc.clone(), &mut buf);
    buf
  }

  fn bellman_ford(&self, s: usize) -> Vec<isize> {
    const INF: isize = std::isize::MAX >> 1;
    let edges = self.edges();
    let mut bf: Vec<isize> = vec![INF; self.size];
    bf[s] = 0;

    for _ in 1 .. self.size {
      for &(v, w, c) in &edges {
        bf[w] = std::cmp::min(bf[w], bf[v]+c)
      }
    }
    bf
  }

  fn dijkstra(&self, s: usize) -> Vec<isize> {
    use std::collections::BinaryHeap;

    const INF: isize = std::isize::MAX;
    let mut dk: Vec<isize> = vec![INF; self.size];
    dk[s] = 0;

    let mut pq = BinaryHeap::new();
    pq.push((0,s));

    while let Some((acc,v)) = pq.pop() {
      let acc = -acc;
      for &(w,c) in &self.adj[v] {
        let cand = acc + c;
        if cand < dk[w] {
          dk[w] = cand;
          pq.push((-cand,w));
        }
      }
    }
    dk
  }

  fn warshall_floyd(&self) -> Vec<Vec<isize>> {
    const INF: isize = std::isize::MAX >> 1;
    let mut wf: Vec<Vec<isize>> = vec![vec![INF; self.size+1]; self.size+1];
    for i in 1 .. self.size+1 {wf[i][i] = 0}

    for (next, i) in self.adj.iter().zip(0..) {
      for &(j, x) in next {
        wf[i][j] = x
      }
    }
    for k in 1 .. self.size+1 {
      for i in 1 .. self.size+1 {
        for j in 1 .. self.size+1 {
          wf[i][j] = std::cmp::min(wf[i][j], wf[i][k] + wf[k][j]);
        }
      }
    }
    wf
  }

  fn coloring2(&self) -> Option<(Vec<usize>, Vec<usize>)> {
    fn paint(v: usize, p: bool, g: &Graph, cv: &mut Vec<Option<bool>>) -> bool {
      match cv[v] {
        None => {
          let next = &g.adj[v];
          cv[v] = Some(p);
          next.iter().all(|&(w,_)|paint(w,!p,g,cv))},
        Some(q) => {q == p}
      }
    }

    let mut canvas: Vec<Option<bool>> = vec![None; self.size+1];
    let ans = paint(1, false, self, &mut canvas);
    let bs = canvas.iter().enumerate().filter(|&(_,&v)|v==Some(false)).map(|(i,_)|i).collect::<Vec<_>>();
    let ws = canvas.iter().enumerate().filter(|&(_,&v)|v==Some(true)).map(|(i,_)|i).collect::<Vec<_>>();
    if ans {Some((bs,ws))} else {None}
  }

  fn dfs(&self, s: usize) -> Vec<usize> {
    fn go(g: &Graph, current: usize, mut path: &mut Vec<usize>, mut visited: &mut Vec<bool>) {
      for &(next, _) in &g.adj[current] {
        if visited[next] {
          continue
        } else {
          visited[next] = true;
          path.push(next);
          go(&g, next, &mut path, &mut visited)
        }
      }
    }

    let mut path = vec![s];
    let mut visited = vec![false; self.size+1];
    visited[s] = true;
    go(&self, s, &mut path, &mut visited);
    path
  }

  fn bfs(&self, v: usize) -> Vec<usize> {
    use std::collections::VecDeque;
    let mut q = VecDeque::new();
    let mut buf = vec![];
    let mut vd = vec![false; self.size+1];
    vd[v] = true;
    q.push_back(v);
    while let Some(w) = q.pop_front() {
      buf.push(w);
      for &(next,_) in self.adj[w].iter() {
        if !vd[next] {
          q.push_back(next);
          vd[next] = true
        }
      }
    }
    buf
  }

  fn ultimate(&self, v: usize) -> usize {
    use std::collections::VecDeque;
    let mut q = VecDeque::new();
    let mut dist = 0;
    let mut vd = vec![false; self.size+1];
    vd[v] = true;
    q.push_back((v,dist));
    while let Some((w, d)) = q.pop_front() {
      dist = std::cmp::max(dist, d);
      for &(next,_) in self.adj[w].iter() {
        if !vd[next] {
          q.push_back((next, d+1));
          vd[next] = true
        }
      }
    }
    dist
  }

  fn cut(&mut self, v: usize, w: usize) {
    self.adj[v].retain(|&(t,_)| t != w);
    self.adj[w].retain(|&(t,_)| t != v);
  }
}

#[allow(dead_code)]
mod get {
  use std::io::*;
  use std::str::*;
  use std::process::*;

  pub fn val<T: FromStr>() -> T {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    if buf.is_empty() {println!("No input"); exit(0)}
    buf.trim_right().parse::<T>().ok().unwrap()
  }

  pub fn vals<T: FromStr>(n: usize) -> Vec<T> {
    let mut vec: Vec<T> = vec![];
    for _ in 0 .. n {
      vec.push(val());
    }
    vec
  }

  pub fn tuple<T1: FromStr, T2: FromStr>() -> (T1, T2) {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    if buf.is_empty() {println!("No input"); exit(0)}
    let mut it = buf.trim_right().split_whitespace();
    let x = it.next().unwrap().parse::<T1>().ok().unwrap();
    let y = it.next().unwrap().parse::<T2>().ok().unwrap();
    (x, y)
  }

  pub fn tuples<T1: FromStr, T2: FromStr>(n: usize) -> Vec<(T1, T2)> {
    let mut vec: Vec<(T1, T2)> = vec![];
    for _ in 0 .. n {
      vec.push(tuple());
    }
    vec
  }

  pub fn tuple3<T1: FromStr, T2: FromStr, T3: FromStr>() -> (T1, T2, T3) {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    if buf.is_empty() {println!("No input"); exit(0)}
    let mut it = buf.trim_right().split_whitespace();
    let x = it.next().unwrap().parse::<T1>().ok().unwrap();
    let y = it.next().unwrap().parse::<T2>().ok().unwrap();
    let z = it.next().unwrap().parse::<T3>().ok().unwrap();
    (x, y, z)
  }

  pub fn tuple3s<T1: FromStr, T2: FromStr, T3: FromStr>(n: usize) -> Vec<(T1, T2, T3)> {
    let mut vec: Vec<(T1, T2, T3)> = vec![];
    for _ in 0 .. n {
      vec.push(tuple3());
    }
    vec
  }

  pub fn vec<T: FromStr>() -> Vec<T> {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    if buf.is_empty() {println!("No input"); exit(0)}
    buf.trim_right().split_whitespace().map(|t| t.parse::<T>().ok().unwrap()).collect()
  }

  pub fn mat<T: FromStr>(h: usize) -> Vec<Vec<T>> {
    let mut mat = vec![];
    for _ in 0 .. h {
      mat.push(vec());
    }
    mat
  }

  pub fn chars() -> Vec<char> {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    if buf.is_empty() {println!("No input"); exit(0)}
    buf.trim_right().chars().collect()
  }
}

#[allow(dead_code)]
mod put {
  use std::string::*;

  pub fn vec<T: ToString>(vec: &Vec<T>, sep: &str) {
    let out = vec.iter().map(|e| e.to_string()).collect::<Vec<_>>().as_slice().join(sep);
    println!("{}", out);
  }

  pub fn mat<T: ToString>(mat: &Vec<Vec<T>>, sep: &str) {
    for v in mat {
      vec(v, sep);
    }
  }
}
0