結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー tonyu0tonyu0
提出日時 2023-02-03 23:08:30
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 5,429 bytes
コンパイル時間 24,121 ms
コンパイル使用メモリ 378,964 KB
実行使用メモリ 39,424 KB
最終ジャッジ日時 2024-07-02 21:12:46
合計ジャッジ時間 15,305 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 55 ms
19,124 KB
testcase_12 AC 56 ms
19,072 KB
testcase_13 AC 49 ms
19,068 KB
testcase_14 AC 46 ms
19,176 KB
testcase_15 AC 65 ms
39,424 KB
testcase_16 AC 34 ms
12,160 KB
testcase_17 AC 31 ms
12,160 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 56 ms
18,944 KB
testcase_20 AC 44 ms
17,536 KB
testcase_21 AC 51 ms
17,536 KB
testcase_22 AC 49 ms
19,584 KB
testcase_23 AC 48 ms
17,536 KB
testcase_24 AC 54 ms
18,048 KB
testcase_25 AC 59 ms
17,664 KB
testcase_26 AC 67 ms
17,568 KB
testcase_27 AC 55 ms
19,072 KB
testcase_28 AC 52 ms
19,572 KB
testcase_29 AC 56 ms
19,708 KB
testcase_30 AC 61 ms
17,520 KB
testcase_31 AC 52 ms
19,072 KB
testcase_32 AC 51 ms
18,160 KB
testcase_33 AC 53 ms
18,176 KB
testcase_34 AC 51 ms
17,408 KB
testcase_35 AC 60 ms
17,664 KB
testcase_36 AC 62 ms
17,664 KB
testcase_37 AC 51 ms
17,480 KB
testcase_38 AC 47 ms
17,536 KB
testcase_39 AC 49 ms
18,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(dead_code, unused_imports, unused_macros)]
use std::{
    cell::UnsafeCell,
    cmp::{max, min},
    collections::{HashMap, VecDeque},
    io::{BufWriter, Read, Stdin, Stdout, Write},
    mem::swap,
    str::FromStr,
};

fn rec(v: usize, p: usize, g: &Vec<Vec<usize>>, col: &mut Vec<usize>) -> usize {
    let mut res = 0;
    for &nv in g[v].iter() {
        if nv != p {
            res += rec(nv, v, g, col);
            if col[nv] == 0 {
                col[v] ^= 1;
                res += 1;
            }
        }
    }
    return res;
}
fn main() {
    let n = read!(usize);
    let mut g = vec![Vec::new(); n];
    for _ in 0..n - 1 {
        let a = read!(usize);
        let b = read!(usize);
        g[a - 1].push(b - 1);
        g[b - 1].push(a - 1);
    }
    let mut c = read_vec::<usize>(n);
    if (n - c.iter().sum::<usize>()) % 2 != 0 {
        println!("-1");
    } else {
        let res = rec(0, 0, &g, &mut c);
        println!("{}", res);
    }
    out_flush();
}

const IO_BUF_SIZE: usize = 1 << 16;
type Input = Scanner<Stdin>;
type Output = BufWriter<Stdout>;
fn _init_input() -> Input {
    Scanner::new(std::io::stdin())
}
fn _init_output() -> Output {
    BufWriter::with_capacity(IO_BUF_SIZE, std::io::stdout())
}

#[repr(transparent)]
struct Unsync<T>(T);
unsafe impl<T> Sync for Unsync<T> {}

type BadLazy<T> = Unsync<UnsafeCell<Option<T>>>;
impl<T> BadLazy<T> {
    const fn new() -> Self {
        Self(UnsafeCell::new(None))
    }
}

static INPUT: BadLazy<Input> = BadLazy::new();
static OUTPUT: BadLazy<Output> = BadLazy::new();

fn inp<F: FnOnce(&mut Input) -> R, R>(f: F) -> R {
    unsafe { f((&mut *INPUT.0.get()).get_or_insert_with(_init_input)) }
}
fn out<F: FnOnce(&mut Output) -> R, R>(f: F) -> R {
    unsafe { f((&mut *OUTPUT.0.get()).get_or_insert_with(_init_output)) }
}

#[macro_export]
macro_rules! read {
    () => { read() };
    ($t: ty) => { read::<$t>() };
    ($t: ty, $($tt: ty),*) => { (read::<$t>(), $(read::<$tt>(),)*) };
    [$t: ty; $n: expr] => { read_vec::<$t>($n) };
}
#[macro_export]
macro_rules! println {
    () => { out(|x| { let _ = writeln!(x); }) };
    ($exp: expr) => { out(|x| { let _ = writeln!(x, "{}", $exp); }) };
    ($fmt: expr, $($arg : tt )*) => { out(|x| { let _ = writeln!(x, $fmt, $($arg)*); }) }
}
#[macro_export]
macro_rules! print {
    ($exp: expr) => { out(|x| { let _ = write!(x, "{}", $exp); }) };
    ($fmt: expr, $($arg : tt )*) => { out(|x| { let _ = write!(x, $fmt, $($arg)*); }) }
}

fn out_flush() {
    out(|x| {
        let _ = x.flush();
    });
}

fn input_is_eof() -> bool {
    inp(|x| x.eof())
}
fn read_byte() -> u8 {
    inp(|x| x.byte())
}
fn read_bytes_no_skip(n: usize) -> Vec<u8> {
    inp(|x| x.bytes_no_skip(n))
}
fn read_bytes(n: usize) -> Vec<u8> {
    inp(|x| x.bytes(n))
}
fn read_bytes2(n: usize, m: usize) -> Vec<Vec<u8>> {
    inp(|x| x.bytes2(n, m))
}
fn read_token() -> Vec<u8> {
    inp(|x| x.token_bytes())
}
fn read_token_str() -> String {
    unsafe { String::from_utf8_unchecked(read_token()) }
}
fn read_line() -> Vec<u8> {
    inp(|x| x.line_bytes())
}
fn read_line_str() -> String {
    unsafe { String::from_utf8_unchecked(read_line()) }
}
fn read<T: FromStr>() -> T {
    read_token_str().parse::<T>().ok().expect("failed parse")
}
fn read_vec<T: FromStr>(n: usize) -> Vec<T> {
    (0..n).map(|_| read()).collect()
}
fn read_vec2<T: FromStr>(n: usize, m: usize) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec(m)).collect()
}

struct Scanner<R: Read> {
    src: R,
    _buf: Vec<u8>,
    _pt: usize, // pointer
    _rd: usize, // bytes read
}

impl<R: Read> Scanner<R> {
    fn new(src: R) -> Scanner<R> {
        Scanner {
            src,
            _buf: vec![0; IO_BUF_SIZE],
            _pt: 1,
            _rd: 1,
        }
    }

    fn _check_buf(&mut self) {
        if self._pt == self._rd {
            self._rd = self.src.read(&mut self._buf).unwrap_or(0);
            self._pt = (self._rd == 0) as usize;
        }
    }

    // returns true if end of file
    fn eof(&mut self) -> bool {
        self._check_buf();
        self._rd == 0
    }

    // filters \r, returns \0 if eof
    fn byte(&mut self) -> u8 {
        loop {
            self._check_buf();
            if self._rd == 0 {
                return 0;
            }
            let res = self._buf[self._pt];
            self._pt += 1;
            if res != b'\r' {
                return res;
            }
        }
    }

    fn bytes_no_skip(&mut self, n: usize) -> Vec<u8> {
        (0..n).map(|_| self.byte()).collect()
    }
    fn bytes(&mut self, n: usize) -> Vec<u8> {
        let res = self.bytes_no_skip(n);
        self.byte();
        res
    }
    fn bytes2(&mut self, n: usize, m: usize) -> Vec<Vec<u8>> {
        (0..n).map(|_| self.bytes(m)).collect()
    }

    fn token_bytes(&mut self) -> Vec<u8> {
        let mut res = Vec::new();
        let mut c = self.byte();
        while c <= b' ' {
            if c == b'\0' {
                return res;
            }
            c = self.byte();
        }
        loop {
            res.push(c);
            c = self.byte();
            if c <= b' ' {
                return res;
            }
        }
    }

    fn line_bytes(&mut self) -> Vec<u8> {
        let mut res = Vec::new();
        let mut c = self.byte();
        while c != b'\n' && c != b'\0' {
            res.push(c);
            c = self.byte();
        }
        res
    }
}
0