結果

問題 No.408 五輪ピック
ユーザー snteasntea
提出日時 2017-10-28 18:43:24
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 5,006 bytes
コンパイル時間 14,712 ms
コンパイル使用メモリ 378,812 KB
実行使用メモリ 11,236 KB
最終ジャッジ日時 2024-05-01 21:24:03
合計ジャッジ時間 25,157 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 14 ms
5,376 KB
testcase_06 AC 749 ms
5,432 KB
testcase_07 AC 110 ms
5,376 KB
testcase_08 AC 11 ms
5,376 KB
testcase_09 AC 11 ms
5,376 KB
testcase_10 AC 544 ms
5,376 KB
testcase_11 AC 482 ms
5,376 KB
testcase_12 AC 19 ms
5,408 KB
testcase_13 AC 12 ms
5,376 KB
testcase_14 AC 224 ms
5,376 KB
testcase_15 AC 12 ms
5,376 KB
testcase_16 AC 13 ms
5,376 KB
testcase_17 AC 15 ms
5,376 KB
testcase_18 AC 16 ms
5,376 KB
testcase_19 AC 19 ms
5,504 KB
testcase_20 AC 408 ms
5,376 KB
testcase_21 AC 218 ms
5,376 KB
testcase_22 AC 721 ms
5,376 KB
testcase_23 AC 24 ms
6,272 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 mask == (1<<5)-1 {
            p == 0
        } else {
            let res = self[p].iter().fold(false, |x, y|{
                x || if (mask>>color[y.to])&1 == 0 {
                    self.solve(y.to, mask|(1<<color[y.to]), 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..500 {
        let color: Vec<_> = (0..n).map(|_| rnd.next()%5).collect();
        let mut dp = vec![vec![None; 1<<5]; n];
        let res = g[0].iter().fold(false, |x, y| {
            x||g.solve(y.to, 1<<color[y.to], &mut dp, &color)
        });
        if res {
            f = true;
            break 'main;
        }
    }
    if f {
        println!("YES");
    } else {
        println!("NO");
    }
}
0