結果

問題 No.408 五輪ピック
ユーザー snteasntea
提出日時 2017-10-28 19:14:48
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 856 ms / 5,000 ms
コード長 4,987 bytes
コンパイル時間 14,675 ms
コンパイル使用メモリ 387,956 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-22 04:20:04
合計ジャッジ時間 16,840 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,820 KB
testcase_03 AC 1 ms
6,816 KB
testcase_04 AC 1 ms
6,816 KB
testcase_05 AC 11 ms
6,820 KB
testcase_06 AC 294 ms
6,820 KB
testcase_07 AC 22 ms
6,816 KB
testcase_08 AC 9 ms
6,816 KB
testcase_09 AC 9 ms
6,816 KB
testcase_10 AC 202 ms
6,816 KB
testcase_11 AC 182 ms
6,816 KB
testcase_12 AC 21 ms
6,820 KB
testcase_13 AC 11 ms
6,820 KB
testcase_14 AC 85 ms
6,816 KB
testcase_15 AC 10 ms
6,816 KB
testcase_16 AC 12 ms
6,816 KB
testcase_17 AC 13 ms
6,816 KB
testcase_18 AC 14 ms
6,820 KB
testcase_19 AC 15 ms
6,816 KB
testcase_20 AC 139 ms
6,816 KB
testcase_21 AC 76 ms
6,820 KB
testcase_22 AC 237 ms
6,816 KB
testcase_23 AC 16 ms
6,816 KB
testcase_24 AC 856 ms
6,820 KB
testcase_25 AC 12 ms
6,816 KB
testcase_26 AC 365 ms
6,820 KB
testcase_27 AC 718 ms
6,820 KB
testcase_28 AC 66 ms
6,820 KB
testcase_29 AC 66 ms
6,816 KB
testcase_30 AC 1 ms
6,820 KB
testcase_31 AC 1 ms
6,816 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused macro definition: `readlvec`
  --> src/main.rs:29:14
   |
29 | macro_rules! readlvec {
   |              ^^^^^^^^
   |
   = note: `#[warn(unused_macros)]` on by default

warning: unused macro definition: `mvec`
  --> src/main.rs:39:14
   |
39 | macro_rules! mvec {
   |              ^^^^

warning: unused macro definition: `debug`
  --> src/main.rs:48:14
   |
48 | macro_rules! debug {
   |              ^^^^^

warning: unused import: `std::collections::HashSet`
   --> src/main.rs:166:5
    |
166 | use std::collections::HashSet;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(unused_imports)]` on by default

warning: function `printiter` is never used
  --> src/main.rs:54:4
   |
54 | fn printiter<'a, T>(v: &'a T)
   |    ^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: method `size` is never used
   --> src/main.rs:122:8
    |
114 | impl Graph {
    | ---------- method in this implementation
...
122 |     fn size(&self) -> usize{
    |        ^^^^

ソースコード

diff #

// use std::ops::{Index, IndexMut};
// use std::cmp::{Ordering, min, max};
// use std::collections::{BinaryHeap, BTreeMap};
// use std::collections::btree_map::Entry::{Occupied, Vacant};
// use std::clone::Clone;

fn getline() -> String{
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*) 
        }
    };
}

macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

macro_rules! mvec {
    ($v: expr, $s: expr) => {
        vec![$v; $s]
    };
    ($v: expr, $s: expr, $($t: expr),*) => {
        vec![mvec!($v, $($t),*); $s]
    };
}

macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn printiter<'a, T>(v: &'a T)
where
    &'a T: std::iter::IntoIterator, 
    <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display {
    for (i,e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}

type Cost = i64;

#[derive(Clone)]
struct Edge {
    to: usize,
    cost: Cost,
}

impl Ord for Edge {
    fn cmp(&self, other: &Edge) -> std::cmp::Ordering {
        (-self.cost).cmp(&(-other.cost))
    }
}

impl PartialOrd for Edge {
    fn partial_cmp(&self, other: &Edge) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Eq for Edge {}

impl PartialEq for Edge {
    fn eq(&self, other: &Edge) -> bool {
        self.cost == other.cost
    }
}

#[derive(Clone)]
struct Graph {
    adj: Vec<Vec<Edge>>,
}

impl std::ops::Index<usize> for Graph {
    type Output = Vec<Edge>;

    fn index(&self, i: usize) -> &Vec<Edge> {
        &self.adj[i]
    }
}

impl std::ops::IndexMut<usize> for Graph {
    fn index_mut<'a>(&'a mut self, i: usize) -> &'a mut Vec<Edge> {
        &mut self.adj[i]
    }
}

impl Graph {

    fn new(n: usize) -> Graph{
        Graph {
            adj: vec![vec![]; n],
        }
    }

    fn size(&self) -> usize{
        self.adj.capacity()
    }

    fn add_edge(&mut self, from: usize, to_: usize, cost_: Cost) {
        self.adj[from].push(Edge{to: to_, cost: cost_});
    }

    fn add_uedge(&mut self, from: usize, to_: usize, cost_: Cost) {
        self.add_edge(from, to_, cost_);
        self.add_edge(to_, from, cost_);
    }

}

struct Random {
    x: i32,
    y: i32,
    z: i32,
    w: i32,
    t: i32,
}

impl Random {
    fn new() -> Random {
        Random {
            x: 400,
            y: 362436069,
            z: 521288629,
            w: 886751233,
            t: 1,
        }
    }

    fn next(&mut self) -> i32 {
        self.t = self.x^(self.x << 11);
        self.x = self.y;
        self.y = self.z;
        self.z = self.w;
        self.w = (self.w ^ (self.w>>19)) ^ (self.t ^ (self.t>>8));
        self.w&0x7fffffff
    }
}

use std::collections::HashSet;

// struct Solver {
//     n: usize,
//     m: usize,
//     g: Graph,
// }

// impl Solver {
    

//     fn solve() -> String {
//         let mut rnd = Random::new();
//         for _ in 0..1000 {
//             let color: Vec<_> = (0..n).map(|_| rnd.next()%5).collect();
//             let mut dp = vec![1<<5; n];

//         }
//     }
// }

impl Graph {
    fn solve(&self, p: usize, mask: usize, memo: &mut Vec<Vec<Option<bool>>>, color: &Vec<i32>) -> bool {
        // debug!(mask);
        if let Some(res) = memo[p][mask] {
            res
        } else if p == 0 {
            mask == (1<<4)-1
        } else {
            let res = self[p].iter().fold(false, |x, y| {
                x || if (mask>>color[p])&1 == 0 {
                    self.solve(y.to, mask|(1<<color[p]), memo, color)
                } else {
                    false
                }
            });
            memo[p][mask] = Some(res);
            res
        }
    }
}

fn main() {
    let (n, m) = readl!(usize, usize); 
    let g = {
       let mut g = Graph::new(n);
       for _ in 0..m {
           let (a, b) = readl!(usize, usize);
           g.add_uedge(a-1, b-1, -1);
       }
       g
    };
    // let ans = Solver::new(n, m, g);
    let mut rnd = Random::new();
    let mut f = false;
    'main: for _ in 0..200 {
        let color: Vec<_> = (0..n).map(|_| rnd.next()%4).collect();
        let mut dp = vec![vec![None; 1<<4]; n];
        let res = g[0].iter().fold(false, |x, y| {
            x||g.solve(y.to, 0, &mut dp, &color)
        });
        if res {
            f = true;
            break 'main;
        }
    }
    if f {
        println!("YES");
    } else {
        println!("NO");
    }
}
0