Submission #1097807


Source Code Expand

use std::io::{self, Stdin};
use std::str::{self, FromStr};
use std::error::Error;
use std::cmp::*;
fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.ne();
    let num: Vec<i64> = (0..n).map(|_| sc.ne()).collect();
    let mut adj = vec![Vec::new(); n];
    for _ in 0..n - 1 {
        let u = sc.ne::<usize>() - 1;
        let v = sc.ne::<usize>() - 1;
        adj[u].push(v);
        adj[v].push(u);
    }
    fn dfs(adj: &Vec<Vec<usize>>, num: &Vec<i64>, cur: usize, par: usize) -> (i64, bool) {
        if adj[cur].len() == 1 {
            return (num[cur], true);
        }
        let mut kids = Vec::new();
        for nxt in &adj[cur] {
            if *nxt == par {
                continue;
            }
            let res = dfs(&adj, &num, *nxt, cur);
            if res.1 == false {
                return (0, false);
            }
            kids.push(res.0);
        }
        let sum = kids.iter().fold(0, |acc, &x| acc + x);
        if sum < num[cur] {
            return (0, false);
        }
        let ma = kids.iter().max().unwrap();
        // sum - num[cur] -> 子のペアの必要数
        if sum - num[cur] > min(sum - *ma, sum / 2) {
            return (0, false);
        }
        (2 * num[cur] - sum, true)
    }
    let ok = match n {
        2 => num[0] == num[1],
        _ => {
            let mut root = 0;
            for i in (0..n).filter(|&i| adj[i].len() > 1) {
                root = i;
                break;
            }
            let res = dfs(&adj, &num, root, root);
            // println!("{:?}", res);
            res.0 == 0 && res.1
        }
    };
    println!("{}", if ok { "YES" } else { "NO" });
}



struct Scanner {
    stdin: Stdin,
    id: usize,
    buf: Vec<u8>,
}

impl Scanner {
    fn new() -> Scanner {
        Scanner {
            stdin: io::stdin(),
            id: 0,
            buf: Vec::new(),
        }
    }
    fn next_line(&mut self) -> Option<String> {
        let mut res = String::new();
        match self.stdin.read_line(&mut res) {
            Ok(0) => return None,
            Ok(_) => Some(res),
            Err(why) => panic!("error in read_line: {}", why.description()),
        }
    }
    fn next<T: FromStr>(&mut self) -> Option<T> {
        while self.buf.len() == 0 {
            self.buf = match self.next_line() {
                Some(r) => {
                    self.id = 0;
                    r.trim().as_bytes().to_owned()
                }
                None => return None,
            };
        }
        let l = self.id;
        assert!(self.buf[l] != b' ');
        let n = self.buf.len();
        let mut r = l;
        while r < n && self.buf[r] != b' ' {
            r += 1;
        }
        let res = match str::from_utf8(&self.buf[l..r]).ok().unwrap().parse::<T>() {
            Ok(s) => Some(s),
            Err(_) => panic!("parse error"),
        };
        while r < n && self.buf[r] == b' ' {
            r += 1;
        }
        if r == n {
            self.buf.clear();
        } else {
            self.id = r;
        }
        res
    }
    fn ne<T: FromStr>(&mut self) -> T {
        self.next::<T>().unwrap()
    }
}

Submission Info

Submission Time
Task C - Cleaning
User mio_h1917
Language Rust (1.15.1)
Score 700
Code Size 3280 Byte
Status AC
Exec Time 65 ms
Memory 17404 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 48
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in41.txt, in42.txt, in43.txt, in44.txt, in45.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt
Case Name Status Exec Time Memory
in1.txt AC 53 ms 6908 KB
in10.txt AC 53 ms 6908 KB
in11.txt AC 63 ms 16380 KB
in12.txt AC 65 ms 16168 KB
in13.txt AC 62 ms 17404 KB
in14.txt AC 59 ms 15868 KB
in15.txt AC 2 ms 380 KB
in16.txt AC 2 ms 380 KB
in17.txt AC 2 ms 380 KB
in18.txt AC 2 ms 380 KB
in19.txt AC 50 ms 6908 KB
in2.txt AC 53 ms 6908 KB
in20.txt AC 49 ms 7548 KB
in21.txt AC 49 ms 7676 KB
in22.txt AC 52 ms 6908 KB
in23.txt AC 51 ms 6908 KB
in24.txt AC 50 ms 6908 KB
in25.txt AC 2 ms 380 KB
in26.txt AC 52 ms 7476 KB
in27.txt AC 51 ms 7480 KB
in28.txt AC 3 ms 380 KB
in29.txt AC 45 ms 6908 KB
in3.txt AC 53 ms 6908 KB
in30.txt AC 2 ms 380 KB
in31.txt AC 52 ms 6908 KB
in32.txt AC 49 ms 6908 KB
in33.txt AC 53 ms 6908 KB
in34.txt AC 53 ms 6908 KB
in35.txt AC 53 ms 6908 KB
in36.txt AC 54 ms 6908 KB
in37.txt AC 3 ms 508 KB
in38.txt AC 52 ms 6908 KB
in39.txt AC 52 ms 6908 KB
in4.txt AC 53 ms 6908 KB
in40.txt AC 49 ms 6908 KB
in41.txt AC 49 ms 6908 KB
in42.txt AC 53 ms 6908 KB
in43.txt AC 51 ms 6908 KB
in44.txt AC 53 ms 6908 KB
in45.txt AC 53 ms 6908 KB
in5.txt AC 53 ms 6908 KB
in6.txt AC 53 ms 6908 KB
in7.txt AC 48 ms 6268 KB
in8.txt AC 14 ms 1916 KB
in9.txt AC 53 ms 6908 KB
sample1.txt AC 3 ms 380 KB
sample2.txt AC 3 ms 380 KB
sample3.txt AC 3 ms 380 KB