結果

問題 No.812 Change of Class
ユーザー koba-e964
提出日時 2019-03-20 01:49:00
言語 Rust
(1.30.1)
結果
AC  
実行時間 1,480 ms
コード長 4,746 Byte
コンパイル時間 5,224 ms
使用メモリ 23,244 KB
最終ジャッジ日時 2019-07-03 11:11:39

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
02_hitohude_01 AC 154 ms
18,620 KB
02_hitohude_02 AC 27 ms
9,120 KB
02_hitohude_03 AC 83 ms
13,152 KB
02_hitohude_04 AC 180 ms
21,192 KB
02_hitohude_05 AC 163 ms
18,620 KB
02_M_max_01 AC 1,448 ms
21,276 KB
02_M_max_02 AC 1,480 ms
23,244 KB
02_M_min_01 AC 7 ms
9,096 KB
02_M_min_02 AC 5 ms
9,096 KB
02_M_N_max_01 AC 1,170 ms
23,240 KB
02_M_N_max_02 AC 1,208 ms
23,236 KB
02_N_max_01 AC 23 ms
15,200 KB
02_N_max_02 AC 668 ms
21,228 KB
02_N_min_01 AC 4 ms
9,096 KB
02_N_min_02 AC 3 ms
9,096 KB
02_random_01 AC 791 ms
19,148 KB
02_random_02 AC 41 ms
9,124 KB
02_random_03 AC 676 ms
19,212 KB
02_random_04 AC 504 ms
15,200 KB
02_random_05 AC 585 ms
15,204 KB
02_random_06 AC 1,196 ms
23,236 KB
02_random_07 AC 471 ms
15,208 KB
02_random_08 AC 180 ms
11,160 KB
02_random_09 AC 30 ms
9,120 KB
02_random_10 AC 50 ms
9,120 KB
02_random_11 AC 146 ms
11,108 KB
02_random_12 AC 940 ms
19,288 KB
02_random_13 AC 854 ms
19,268 KB
02_random_14 AC 553 ms
15,208 KB
02_random_15 AC 111 ms
9,120 KB
02_random_16 AC 748 ms
19,148 KB
02_random_17 AC 642 ms
15,200 KB
02_random_18 AC 241 ms
11,160 KB
02_random_19 AC 741 ms
19,252 KB
02_random_20 AC 45 ms
9,124 KB
02_random_21 AC 99 ms
11,164 KB
02_random_22 AC 49 ms
9,120 KB
02_random_23 AC 366 ms
13,208 KB
02_random_24 AC 11 ms
11,112 KB
02_random_25 AC 382 ms
13,156 KB
02_random_26 AC 62 ms
9,116 KB
02_random_27 AC 9 ms
9,116 KB
02_random_28 AC 875 ms
17,836 KB
02_random_29 AC 64 ms
15,200 KB
02_random_30 AC 606 ms
14,792 KB
02_random_31 AC 52 ms
9,120 KB
02_random_32 AC 55 ms
14,532 KB
02_random_33 AC 600 ms
14,792 KB
02_random_34 AC 574 ms
15,052 KB
02_random_35 AC 138 ms
8,912 KB
02_random_36 AC 10 ms
7,284 KB
02_random_37 AC 109 ms
9,920 KB
02_random_38 AC 258 ms
11,560 KB
02_random_39 AC 83 ms
8,916 KB
02_random_40 AC 78 ms
6,872 KB
02_star_01 AC 370 ms
17,068 KB
02_star_02 AC 477 ms
17,772 KB
02_star_03 AC 517 ms
17,580 KB
02_star_04 AC 238 ms
12,092 KB
02_star_05 AC 570 ms
18,020 KB
sample_1 AC 4 ms
6,872 KB
sample_2 AC 4 ms
7,108 KB
sample_3 AC 3 ms
6,872 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