結果

問題 No.1153 ねこちゃんゲーム
ユーザー koba-e964koba-e964
提出日時 2021-10-11 13:52:39
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 402 ms / 2,500 ms
コード長 5,951 bytes
コンパイル時間 19,035 ms
コンパイル使用メモリ 377,344 KB
実行使用メモリ 95,104 KB
最終ジャッジ日時 2024-09-15 11:40:34
合計ジャッジ時間 36,865 ms
ジャッジサーバーID
(参考情報)
judge2 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 341 ms
48,256 KB
testcase_08 AC 345 ms
48,000 KB
testcase_09 AC 334 ms
48,128 KB
testcase_10 AC 339 ms
48,000 KB
testcase_11 AC 333 ms
48,256 KB
testcase_12 AC 348 ms
48,256 KB
testcase_13 AC 339 ms
47,872 KB
testcase_14 AC 342 ms
48,128 KB
testcase_15 AC 348 ms
47,872 KB
testcase_16 AC 332 ms
48,128 KB
testcase_17 AC 332 ms
48,384 KB
testcase_18 AC 333 ms
48,364 KB
testcase_19 AC 340 ms
48,384 KB
testcase_20 AC 343 ms
48,512 KB
testcase_21 AC 347 ms
48,640 KB
testcase_22 AC 333 ms
48,384 KB
testcase_23 AC 333 ms
48,256 KB
testcase_24 AC 338 ms
48,256 KB
testcase_25 AC 343 ms
48,384 KB
testcase_26 AC 342 ms
48,512 KB
testcase_27 AC 392 ms
94,208 KB
testcase_28 AC 402 ms
95,104 KB
testcase_29 AC 394 ms
90,880 KB
testcase_30 AC 397 ms
90,112 KB
testcase_31 AC 379 ms
73,172 KB
testcase_32 AC 284 ms
50,432 KB
testcase_33 AC 273 ms
50,440 KB
testcase_34 AC 281 ms
50,688 KB
testcase_35 AC 276 ms
50,376 KB
testcase_36 AC 279 ms
50,432 KB
testcase_37 AC 136 ms
51,288 KB
testcase_38 AC 125 ms
51,412 KB
testcase_39 AC 129 ms
51,280 KB
testcase_40 AC 131 ms
50,568 KB
testcase_41 AC 124 ms
50,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 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, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

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();
}

trait LeaveOne: Default + Clone {
    type T: Default + Clone;
    fn build(vals: &[Self::T]) -> Self;
    fn leave_one(&self, excl: Self::T) -> Self::T;
    fn exchange_one(&self, excl: Self::T, incl: Self::T) -> Self::T;
    fn add_one(&self, incl: Self::T) -> Self::T;
    fn as_is(&self) -> Self::T;
}

#[derive(Default, Clone, Debug)]
struct MexLeaveOne {
    mex1: usize,
    mex2: usize,
    f: Vec<usize>,
}

impl LeaveOne for MexLeaveOne {
    type T = usize;
    fn build(vals: &[usize]) -> Self {
        let seen = vals.iter().cloned().collect::<std::collections::HashSet<_>>();
        let mut mex1 = 0;
        while seen.contains(&mex1) {
            mex1 += 1;
        }
        let mut mex2 = mex1 + 1;
        while seen.contains(&mex2) {
            mex2 += 1;
        }
        let mut f = vec![0; mex2];
        for &v in vals {
            if v < mex2 {
                f[v] += 1;
            }
        }
        let loo = MexLeaveOne {
            mex1: mex1,
            mex2: mex2,
            f: f,
        };
        loo
    }
    fn leave_one(&self, excl: usize) -> usize {
        if excl >= self.mex1 {
            return self.mex1;
        }
        if self.f[excl] == 1 {
            return excl;
        }
        self.mex1
    }
    fn exchange_one(&self, excl: usize, incl: usize) -> usize {
        if excl == incl {
            return self.mex1;
        }
        let mex = self.add_one(incl);
        if excl >= mex {
            return mex;
        }
        let res = if self.f[excl] == 1 {
            excl
        } else {
            mex
        };
        res
    }
    fn add_one(&self, incl: usize) -> usize {
        if incl == self.mex1 {
            self.mex2
        } else {
            self.mex1
        }
    }
    fn as_is(&self) -> usize {
        self.mex1
    }
}

struct Reroot<LOO: LeaveOne> {
    #[allow(unused)]
    pub dp1: Vec<LOO::T>,
    #[allow(unused)]
    pub dp2: Vec<Vec<LOO::T>>,
    #[allow(unused)]
    pub dp_loo: Vec<LOO>,
}

impl<LOO: LeaveOne> Reroot<LOO> {
    pub fn new(g: &[Vec<usize>]) -> Self {
        let n = g.len();
        let mut dp1 = vec![LOO::T::default(); n];
        let mut dp2 = vec![vec![]; n];
        let mut dp_loo = vec![LOO::default(); n];
        Self::dfs1(0, n, &g, &mut dp_loo, &mut dp2);
        Self::dfs2(0, n, &g, &mut dp1, &dp_loo, &mut dp2, LOO::T::default());
        Reroot {
            dp1: dp1,
            dp2: dp2,
            dp_loo: dp_loo,
        }
    }
    fn dfs1(
        v: usize, par: usize, g: &[Vec<usize>],
        dp_loo: &mut [LOO], dp2: &mut [Vec<LOO::T>],
    ) {
        let mut mydp2 = vec![LOO::T::default(); g[v].len()];
        let mut chval = vec![];
        for i in 0..g[v].len() {
            let w = g[v][i];
            if w == par { continue; }
            Self::dfs1(w, v, g, dp_loo, dp2);
            mydp2[i] = dp_loo[w].as_is();
            chval.push(mydp2[i].clone());
        }
        dp_loo[v] = LOO::build(&chval);
        dp2[v] = mydp2;
    }
    fn dfs2(
        v: usize, par: usize, g: &[Vec<usize>],
        dp1: &mut [LOO::T],
        dp_loo: &[LOO],
        dp2: &mut [Vec<LOO::T>],
        passed: LOO::T,
    ) {
        for i in 0..g[v].len() {
            let w = g[v][i];
            if w == par {
                dp2[v][i] = passed.clone();
                continue;
            }
            let inherited = if par >= g.len() {
                dp_loo[v].leave_one(dp2[v][i].clone())
            } else {
                dp_loo[v].exchange_one(dp2[v][i].clone(), passed.clone())
            };
            Self::dfs2(w, v, g, dp1, dp_loo, dp2, inherited);
        }
        dp1[v] = if par >= g.len() {
            dp_loo[v].as_is()
        } else {
            dp_loo[v].add_one(passed)
        };
    }
}

// Tags: rerooting
fn solve() {
    input! {
        n: usize, m: usize,
        a: [usize1; m],
        uv: [(usize1, usize1); n - 1],
    }
    let mut g = vec![vec![]; n];
    for &(u, v) in &uv {
        g[u].push(v);
        g[v].push(u);
    }
    let reroot = Reroot::<MexLeaveOne>::new(&g);
    let mut gr = 0;
    for &a in &a {
        gr ^= reroot.dp1[a];
    }
    if gr == 0 {
        println!("-1 -1");
        return;
    }
    let mut pop = vec![false; n];
    for &a in &a {
        pop[a] = true;
    }
    for i in 0..n {
        if !pop[i] { continue; }
        for j in 0..g[i].len() {
            if (gr ^ reroot.dp1[i] ^ reroot.dp2[i][j]) == 0 {
                let idx = a.iter().position(|&a| a == i).unwrap();
                println!("{} {}", idx + 1, g[i][j] + 1);
                return;
            }
        }
    }
}
0