結果

問題 No.994 ばらばらコイン
ユーザー cympfhcympfh
提出日時 2020-02-21 21:38:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,007 ms / 2,000 ms
コード長 2,767 bytes
コンパイル時間 5,191 ms
コンパイル使用メモリ 164,536 KB
実行使用メモリ 9,280 KB
最終ジャッジ日時 2024-04-17 07:29:49
合計ジャッジ時間 14,603 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 962 ms
8,960 KB
testcase_03 AC 991 ms
9,216 KB
testcase_04 AC 999 ms
9,088 KB
testcase_05 AC 487 ms
5,504 KB
testcase_06 AC 1,007 ms
9,280 KB
testcase_07 AC 983 ms
9,088 KB
testcase_08 AC 682 ms
7,040 KB
testcase_09 AC 951 ms
8,576 KB
testcase_10 AC 705 ms
7,040 KB
testcase_11 AC 774 ms
7,680 KB
testcase_12 AC 825 ms
8,064 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 0 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let k: usize = sc.cin();
    let mut g = vec![vec![]; n];
    for _ in 1..n {
        let a = sc.cin::<usize>() - 1;
        let b = sc.cin::<usize>() - 1;
        if a != b {
            g[a].push(b);
            g[b].push(a);
        }
    }
    let mut visit = vec![false; n];
    visit[0] = true;
    let mut stack = vec![0];
    while let Some(u) = stack.pop() {
        // trace!(u);
        visit[u] = true;
        for &v in g[u].iter() {
            trace!(v);
            if visit[v] { continue }
            stack.push(v);
        }
    }
    trace!(&visit);
    let m = visit.iter().filter(|&&x| x).count();
    if m >= k {
        put!(k - 1);
    } else {
        put!(-1);
    }
}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> { self.cin::<String>().chars().collect() }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.cin()).collect() }
}
0