結果

問題 No.812 Change of Class
ユーザー koba-e964
提出日時 2019-03-20 01:49:00
言語 Rust
(1.37.0)
結果
AC  
実行時間 1,568 ms
コード長 4,746 Byte
コンパイル時間 4,612 ms
使用メモリ 14,068 KB
最終ジャッジ日時 2019-10-26 19:10:49

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
02_hitohude_01 AC 162 ms
10,444 KB
02_hitohude_02 AC 26 ms
2,208 KB
02_hitohude_03 AC 82 ms
5,880 KB
02_hitohude_04 AC 184 ms
11,720 KB
02_hitohude_05 AC 161 ms
10,488 KB
02_M_max_01 AC 1,568 ms
13,100 KB
02_M_max_02 AC 1,519 ms
13,664 KB
02_M_min_01 AC 7 ms
1,976 KB
02_M_min_02 AC 4 ms
1,036 KB
02_M_N_max_01 AC 1,252 ms
13,400 KB
02_M_N_max_02 AC 1,314 ms
13,400 KB
02_N_max_01 AC 25 ms
5,416 KB
02_N_max_02 AC 739 ms
11,472 KB
02_N_min_01 AC 2 ms
964 KB
02_N_min_02 AC 2 ms
956 KB
02_random_01 AC 897 ms
9,680 KB
02_random_02 AC 38 ms
1,512 KB
02_random_03 AC 787 ms
10,028 KB
02_random_04 AC 596 ms
7,196 KB
02_random_05 AC 660 ms
9,068 KB
02_random_06 AC 1,391 ms
13,096 KB
02_random_07 AC 580 ms
6,968 KB
02_random_08 AC 209 ms
3,704 KB
02_random_09 AC 30 ms
1,416 KB
02_random_10 AC 48 ms
1,692 KB
02_random_11 AC 153 ms
3,096 KB
02_random_12 AC 1,106 ms
11,268 KB
02_random_13 AC 980 ms
10,312 KB
02_random_14 AC 630 ms
9,068 KB
02_random_15 AC 114 ms
2,404 KB
02_random_16 AC 849 ms
10,000 KB
02_random_17 AC 740 ms
7,688 KB
02_random_18 AC 262 ms
4,872 KB
02_random_19 AC 879 ms
10,928 KB
02_random_20 AC 44 ms
1,644 KB
02_random_21 AC 115 ms
2,800 KB
02_random_22 AC 51 ms
1,452 KB
02_random_23 AC 436 ms
4,924 KB
02_random_24 AC 9 ms
2,584 KB
02_random_25 AC 443 ms
4,920 KB
02_random_26 AC 68 ms
1,912 KB
02_random_27 AC 9 ms
2,272 KB
02_random_28 AC 1,035 ms
9,120 KB
02_random_29 AC 71 ms
6,000 KB
02_random_30 AC 686 ms
6,448 KB
02_random_31 AC 51 ms
1,452 KB
02_random_32 AC 61 ms
5,708 KB
02_random_33 AC 680 ms
6,452 KB
02_random_34 AC 669 ms
7,356 KB
02_random_35 AC 151 ms
2,788 KB
02_random_36 AC 8 ms
1,040 KB
02_random_37 AC 119 ms
3,376 KB
02_random_38 AC 273 ms
6,368 KB
02_random_39 AC 84 ms
1,908 KB
02_random_40 AC 78 ms
1,888 KB
02_star_01 AC 353 ms
9,948 KB
02_star_02 AC 415 ms
12,244 KB
02_star_03 AC 452 ms
13,368 KB
02_star_04 AC 217 ms
7,184 KB
02_star_05 AC 520 ms
14,068 KB
sample_1 AC 2 ms
956 KB
sample_2 AC 2 ms
956 KB
sample_3 AC 2 ms
960 KB
テストケース一括ダウンロード

ソースコード

diff #
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

/*
 * Dijkstra's algorithm.
 * Verified by: AtCoder ABC035 (http://abc035.contest.atcoder.jp/submissions/676539)
 */

struct Dijkstra {
    edges: Vec<Vec<(usize, i64)>>, // adjacent list representation
}

/*
 * Code from https://doc.rust-lang.org/std/collections/binary_heap/
 */
#[derive(Copy, Clone, Eq, PartialEq)]
struct State {
    cost: i64,
    position: usize,
}

// The priority queue depends on `Ord`.
// Explicitly implement the trait so the queue becomes a min-heap
// instead of a max-heap.
impl Ord for State {
    fn cmp(&self, other: &State) -> Ordering {
        // Notice that the we flip the ordering here
        match other.cost.cmp(&self.cost) {
            std::cmp::Ordering::Equal => other.position.cmp(&self.position),
            x => x,
        }
    }
}

// `PartialOrd` needs to be implemented as well.
impl PartialOrd for State {
    fn partial_cmp(&self, other: &State) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}


impl Dijkstra {
    fn new(n: usize) -> Self {
        Dijkstra { edges: vec![Vec::new(); n] }
    }
    fn add_edge(&mut self, from: usize, to: usize, cost: i64) {
        self.edges[from].push((to, cost));
    }
    /*
     * This function returns a Vec consisting of the distances from vertex source.
     */
    fn solve(&self, source: usize, inf: i64) -> Vec<i64> {
        let n = self.edges.len();
        let mut d = vec![inf; n];
        let mut que = std::collections::BinaryHeap::new();
        que.push(State {cost: 0, position: source});
        while let Some(State {cost, position: pos}) = que.pop() {
            if d[pos] <= cost {
                continue;
            }
            d[pos] = cost;
            for adj in &self.edges[pos] {
                que.push(State {cost: cost + adj.1, position: adj.0});
            }
        }
        return d;
    }
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }
    input! {
        n: usize,
        pq: [(usize1, usize1)],
        a: [usize1],
    }
    // Verification
    assert!(1 <= n && n <= 100000);
    assert!(pq.len() <= 100000);
    let mut seen = HashSet::new();
    for &(p, q) in &pq {
        assert!(p < q);
        assert_eq!(seen.get(&(p, q)), None);
        seen.insert((p, q));
    }

    // solve, O(qm log n)
    const INF: i64 = 1 << 48;
    let mut dijk = Dijkstra::new(n);
    for &(p, q) in &pq {
        dijk.add_edge(p, q, 1);
        dijk.add_edge(q, p, 1);
    }
    for &a in &a {
        let dist = dijk.solve(a, INF);
        let mut ma = 0;
        let mut count = 0;
        for i in 0..n {
            if dist[i] < INF {
                ma = max(ma, dist[i]);
                count += 1;
            }
        }
        let mut turn = 0;
        while ma > 1 << turn {
            turn += 1;
        }
        puts!("{} {}\n", count - 1, turn);
    }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}
0