結果

問題 No.1290 Addition and Subtraction Operation
ユーザー koba-e964koba-e964
提出日時 2021-11-18 23:54:10
言語 Rust
(1.77.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 3,601 bytes
コンパイル時間 5,832 ms
コンパイル使用メモリ 158,336 KB
実行使用メモリ 5,544 KB
最終ジャッジ日時 2023-08-27 18:43:11
合計ジャッジ時間 10,184 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,384 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 21 ms
4,376 KB
testcase_43 AC 21 ms
4,376 KB
testcase_44 AC 21 ms
4,380 KB
testcase_45 AC 22 ms
4,380 KB
testcase_46 AC 21 ms
4,376 KB
testcase_47 AC 21 ms
4,376 KB
testcase_48 AC 22 ms
4,384 KB
testcase_49 AC 22 ms
4,380 KB
testcase_50 AC 22 ms
4,380 KB
testcase_51 AC 21 ms
4,380 KB
testcase_52 AC 21 ms
4,376 KB
testcase_53 AC 21 ms
4,380 KB
testcase_54 AC 22 ms
4,380 KB
testcase_55 AC 22 ms
4,376 KB
testcase_56 AC 21 ms
4,376 KB
testcase_57 AC 20 ms
4,784 KB
testcase_58 AC 26 ms
4,752 KB
testcase_59 AC 20 ms
4,728 KB
testcase_60 AC 42 ms
5,536 KB
testcase_61 AC 34 ms
5,008 KB
testcase_62 AC 36 ms
5,328 KB
testcase_63 AC 24 ms
4,740 KB
testcase_64 AC 13 ms
5,100 KB
testcase_65 AC 16 ms
4,724 KB
testcase_66 AC 30 ms
5,116 KB
testcase_67 AC 42 ms
5,544 KB
testcase_68 AC 35 ms
5,268 KB
testcase_69 AC 25 ms
4,744 KB
testcase_70 AC 24 ms
4,832 KB
testcase_71 AC 27 ms
4,752 KB
testcase_72 AC 17 ms
4,736 KB
testcase_73 AC 31 ms
5,008 KB
testcase_74 AC 13 ms
4,752 KB
testcase_75 AC 15 ms
4,816 KB
testcase_76 AC 24 ms
4,752 KB
testcase_77 AC 45 ms
5,264 KB
testcase_78 AC 41 ms
5,088 KB
testcase_79 AC 31 ms
4,816 KB
testcase_80 AC 50 ms
5,544 KB
testcase_81 AC 49 ms
5,540 KB
testcase_82 AC 37 ms
4,836 KB
testcase_83 AC 50 ms
5,532 KB
testcase_84 AC 46 ms
5,352 KB
testcase_85 AC 28 ms
4,736 KB
testcase_86 AC 22 ms
5,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
// 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, [graph1; $len:expr]) => {{
        let mut g = vec![vec![]; $len];
        let ab = read_value!($next, [(usize1, usize1)]);
        for (a, b) in ab {
            g[a].push(b);
            g[b].push(a);
        }
        g
    }};
    ($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);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

/**
 * Union-Find tree.
 * Verified by https://atcoder.jp/contests/pakencamp-2019-day3/submissions/9253305
 */
struct UnionFind { disj: Vec<usize>, rank: Vec<usize> }

impl UnionFind {
    fn new(n: usize) -> Self {
        let disj = (0..n).collect();
        UnionFind { disj: disj, rank: vec![1; n] }
    }
    fn root(&mut self, x: usize) -> usize {
        if x != self.disj[x] {
            let par = self.disj[x];
            let r = self.root(par);
            self.disj[x] = r;
        }
        self.disj[x]
    }
    fn unite(&mut self, x: usize, y: usize) {
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y { return }
        if self.rank[x] > self.rank[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.disj[x] = y;
        self.rank[y] += self.rank[x];
    }
    #[allow(unused)]
    fn is_same_set(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    #[allow(unused)]
    fn size(&mut self, x: usize) -> usize {
        let x = self.root(x);
        self.rank[x]
    }
}

fn solve() {
    input! {
        n: usize, m: usize,
        b: [i64; n],
        lr: [(usize1, usize); m],
    }
    let mut b = b;
    for i in 0..n {
        if i % 2 == 1 {
            b[i] = -b[i];
        }
    }
    let mut c = vec![0; n + 1];
    c[0] = b[0];
    for i in 1..n {
        c[i] = b[i] - b[i - 1];
    }
    c[n] = -b[n - 1];
    let mut uf = UnionFind::new(n + 1);
    for (l, r) in lr {
        uf.unite(l, r);
    }
    let mut tot = vec![0; n + 1];
    for i in 0..n + 1 {
        tot[uf.root(i)] += c[i];
    }
    println!("{}", if tot.iter().all(|&x| x == 0) { "YES" } else { "NO" });
}

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