結果

問題 No.1290 Addition and Subtraction Operation
ユーザー akakimidoriakakimidori
提出日時 2020-11-14 02:41:39
言語 Rust
(1.77.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 3,302 bytes
コンパイル時間 731 ms
コンパイル使用メモリ 152,584 KB
実行使用メモリ 6,708 KB
最終ジャッジ日時 2023-09-30 04:42:33
合計ジャッジ時間 3,832 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 0 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 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,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 0 ms
4,380 KB
testcase_20 AC 0 ms
4,376 KB
testcase_21 AC 0 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 0 ms
4,376 KB
testcase_27 AC 1 ms
4,376 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 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 0 ms
4,380 KB
testcase_42 AC 6 ms
4,412 KB
testcase_43 AC 6 ms
4,380 KB
testcase_44 AC 6 ms
4,424 KB
testcase_45 AC 6 ms
4,456 KB
testcase_46 AC 6 ms
4,412 KB
testcase_47 AC 6 ms
4,440 KB
testcase_48 AC 6 ms
4,408 KB
testcase_49 AC 7 ms
4,444 KB
testcase_50 AC 6 ms
4,388 KB
testcase_51 AC 6 ms
4,424 KB
testcase_52 AC 6 ms
4,448 KB
testcase_53 AC 6 ms
4,440 KB
testcase_54 AC 6 ms
4,428 KB
testcase_55 AC 6 ms
4,436 KB
testcase_56 AC 6 ms
4,504 KB
testcase_57 AC 6 ms
4,376 KB
testcase_58 AC 7 ms
4,744 KB
testcase_59 AC 5 ms
4,380 KB
testcase_60 AC 12 ms
6,624 KB
testcase_61 AC 9 ms
5,212 KB
testcase_62 AC 11 ms
6,356 KB
testcase_63 AC 7 ms
4,788 KB
testcase_64 AC 3 ms
4,376 KB
testcase_65 AC 5 ms
4,380 KB
testcase_66 AC 8 ms
4,992 KB
testcase_67 AC 12 ms
6,708 KB
testcase_68 AC 10 ms
6,192 KB
testcase_69 AC 7 ms
4,800 KB
testcase_70 AC 7 ms
4,692 KB
testcase_71 AC 8 ms
4,928 KB
testcase_72 AC 5 ms
4,376 KB
testcase_73 AC 9 ms
5,084 KB
testcase_74 AC 4 ms
4,380 KB
testcase_75 AC 5 ms
4,376 KB
testcase_76 AC 7 ms
4,744 KB
testcase_77 AC 13 ms
6,296 KB
testcase_78 AC 12 ms
6,176 KB
testcase_79 AC 8 ms
5,668 KB
testcase_80 AC 14 ms
6,596 KB
testcase_81 AC 13 ms
6,588 KB
testcase_82 AC 10 ms
5,884 KB
testcase_83 AC 13 ms
6,552 KB
testcase_84 AC 12 ms
6,344 KB
testcase_85 AC 6 ms
4,380 KB
testcase_86 AC 5 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//---------- begin union_find ----------
pub struct DSU {
    p: Vec<i32>,
}
impl DSU {
    pub fn new(n: usize) -> DSU {
        DSU { p: vec![-1; n] }
    }
    pub fn init(&mut self) {
        self.p.iter_mut().for_each(|p| *p = -1);
    }
    pub fn root(&mut self, mut x: usize) -> usize {
        assert!(x < self.p.len());
        while self.p[x] >= 0 {
            let p = self.p[x] as usize;
            let q = self.p[p];
            if q >= 0 {
                self.p[x] = q;
            }
            x = p;
        }
        x
    }
    pub fn same(&mut self, x: usize, y: usize) -> bool {
        assert!(x < self.p.len() && y < self.p.len());
        self.root(x) == self.root(y)
    }
    pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
        assert!(x < self.p.len() && y < self.p.len());
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y {
            return None;
        }
        if self.p[x] > self.p[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.p[x] += self.p[y];
        self.p[y] = x as i32;
        Some((x, y))
    }
    pub fn size(&mut self, x: usize) -> usize {
        assert!(x < self.p.len());
        let r = self.root(x);
        (-self.p[r]) as usize
    }
}
//---------- end union_find ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------

fn run() {
    input! {
        n: usize,
        m: usize,
        b: [i64; n],
        p: [(usize1, usize); m],
    }
    let mut dp = b;
    dp.iter_mut().step_by(2).for_each(|b| *b *= -1);
    dp.push(0);
    for i in (0..n).rev() {
        dp[i + 1] -= dp[i];
    }
    let mut dsu = DSU::new(n + 1);
    for (l, r) in p {
        if let Some((a, b)) = dsu.unite(l, r) {
            dp[a] += dp[b];
        }
    }
    if (0..=n).all(|k| dp[dsu.root(k)] == 0) {
        println!("YES");
    } else {
        println!("NO");
    }
}

fn main() {
    run();
}
0