結果

問題 No.812 Change of Class
ユーザー koba-e964koba-e964
提出日時 2019-03-20 01:49:00
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,189 ms / 4,000 ms
コード長 4,746 bytes
コンパイル時間 3,670 ms
コンパイル使用メモリ 172,540 KB
実行使用メモリ 18,604 KB
最終ジャッジ日時 2023-09-02 23:39:36
合計ジャッジ時間 23,575 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
13,084 KB
testcase_01 AC 17 ms
4,380 KB
testcase_02 AC 61 ms
7,568 KB
testcase_03 AC 128 ms
14,872 KB
testcase_04 AC 110 ms
13,396 KB
testcase_05 AC 1,189 ms
14,780 KB
testcase_06 AC 1,120 ms
14,448 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 866 ms
15,504 KB
testcase_10 AC 872 ms
15,612 KB
testcase_11 AC 17 ms
6,824 KB
testcase_12 AC 435 ms
14,148 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 600 ms
11,272 KB
testcase_16 AC 33 ms
4,380 KB
testcase_17 AC 552 ms
11,972 KB
testcase_18 AC 411 ms
8,456 KB
testcase_19 AC 452 ms
10,396 KB
testcase_20 AC 936 ms
15,224 KB
testcase_21 AC 366 ms
8,292 KB
testcase_22 AC 158 ms
4,576 KB
testcase_23 AC 24 ms
4,380 KB
testcase_24 AC 40 ms
4,380 KB
testcase_25 AC 128 ms
4,376 KB
testcase_26 AC 739 ms
13,388 KB
testcase_27 AC 645 ms
11,940 KB
testcase_28 AC 426 ms
10,464 KB
testcase_29 AC 95 ms
4,376 KB
testcase_30 AC 555 ms
11,664 KB
testcase_31 AC 488 ms
9,176 KB
testcase_32 AC 196 ms
5,752 KB
testcase_33 AC 566 ms
12,780 KB
testcase_34 AC 34 ms
4,380 KB
testcase_35 AC 79 ms
4,376 KB
testcase_36 AC 37 ms
4,376 KB
testcase_37 AC 231 ms
6,236 KB
testcase_38 AC 7 ms
4,380 KB
testcase_39 AC 249 ms
6,244 KB
testcase_40 AC 49 ms
4,380 KB
testcase_41 AC 5 ms
4,376 KB
testcase_42 AC 579 ms
11,220 KB
testcase_43 AC 44 ms
7,640 KB
testcase_44 AC 384 ms
8,100 KB
testcase_45 AC 42 ms
4,380 KB
testcase_46 AC 39 ms
7,568 KB
testcase_47 AC 384 ms
8,028 KB
testcase_48 AC 329 ms
9,288 KB
testcase_49 AC 107 ms
4,380 KB
testcase_50 AC 6 ms
4,376 KB
testcase_51 AC 80 ms
4,380 KB
testcase_52 AC 143 ms
8,324 KB
testcase_53 AC 70 ms
4,376 KB
testcase_54 AC 64 ms
4,376 KB
testcase_55 AC 431 ms
13,384 KB
testcase_56 AC 525 ms
16,280 KB
testcase_57 AC 559 ms
16,816 KB
testcase_58 AC 254 ms
9,036 KB
testcase_59 AC 675 ms
18,604 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 1 ms
4,376 KB
testcase_62 AC 1 ms
4,380 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