結果

問題 No.812 Change of Class
ユーザー koba-e964koba-e964
提出日時 2019-03-20 01:49:00
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 1,498 ms / 4,000 ms
コード長 4,746 bytes
コンパイル時間 15,535 ms
コンパイル使用メモリ 379,676 KB
実行使用メモリ 19,516 KB
最終ジャッジ日時 2024-06-12 05:42:33
合計ジャッジ時間 41,216 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
13,948 KB
testcase_01 AC 18 ms
5,376 KB
testcase_02 AC 59 ms
8,448 KB
testcase_03 AC 132 ms
15,828 KB
testcase_04 AC 116 ms
14,376 KB
testcase_05 AC 1,498 ms
15,556 KB
testcase_06 AC 1,488 ms
15,292 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1,179 ms
16,444 KB
testcase_10 AC 1,211 ms
16,440 KB
testcase_11 AC 19 ms
7,912 KB
testcase_12 AC 613 ms
14,948 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 805 ms
12,044 KB
testcase_16 AC 36 ms
5,376 KB
testcase_17 AC 709 ms
12,768 KB
testcase_18 AC 519 ms
9,344 KB
testcase_19 AC 588 ms
11,452 KB
testcase_20 AC 1,270 ms
16,280 KB
testcase_21 AC 482 ms
9,176 KB
testcase_22 AC 205 ms
5,416 KB
testcase_23 AC 27 ms
5,376 KB
testcase_24 AC 47 ms
5,376 KB
testcase_25 AC 153 ms
5,376 KB
testcase_26 AC 1,021 ms
14,236 KB
testcase_27 AC 895 ms
12,924 KB
testcase_28 AC 582 ms
11,444 KB
testcase_29 AC 115 ms
5,376 KB
testcase_30 AC 785 ms
12,620 KB
testcase_31 AC 675 ms
10,168 KB
testcase_32 AC 252 ms
6,824 KB
testcase_33 AC 795 ms
13,708 KB
testcase_34 AC 41 ms
5,376 KB
testcase_35 AC 99 ms
5,376 KB
testcase_36 AC 45 ms
5,376 KB
testcase_37 AC 353 ms
7,256 KB
testcase_38 AC 7 ms
5,376 KB
testcase_39 AC 391 ms
7,132 KB
testcase_40 AC 59 ms
5,376 KB
testcase_41 AC 6 ms
5,376 KB
testcase_42 AC 830 ms
12,140 KB
testcase_43 AC 53 ms
8,560 KB
testcase_44 AC 565 ms
8,996 KB
testcase_45 AC 47 ms
5,376 KB
testcase_46 AC 44 ms
8,536 KB
testcase_47 AC 561 ms
8,880 KB
testcase_48 AC 527 ms
10,144 KB
testcase_49 AC 139 ms
5,376 KB
testcase_50 AC 7 ms
5,376 KB
testcase_51 AC 104 ms
5,376 KB
testcase_52 AC 212 ms
9,176 KB
testcase_53 AC 80 ms
5,376 KB
testcase_54 AC 73 ms
5,376 KB
testcase_55 AC 445 ms
14,224 KB
testcase_56 AC 550 ms
17,352 KB
testcase_57 AC 575 ms
17,812 KB
testcase_58 AC 278 ms
10,092 KB
testcase_59 AC 690 ms
19,516 KB
testcase_60 AC 1 ms
5,376 KB
testcase_61 AC 1 ms
5,376 KB
testcase_62 AC 1 ms
5,376 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