結果

問題 No.1290 Addition and Subtraction Operation
ユーザー MisterMister
提出日時 2020-11-30 09:06:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 2,594 bytes
コンパイル時間 1,074 ms
コンパイル使用メモリ 89,384 KB
実行使用メモリ 11,292 KB
最終ジャッジ日時 2023-10-11 02:43:37
合計ジャッジ時間 8,884 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,476 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 1 ms
4,352 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,352 KB
testcase_25 AC 1 ms
4,352 KB
testcase_26 AC 2 ms
4,352 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 1 ms
4,356 KB
testcase_40 AC 1 ms
4,348 KB
testcase_41 AC 1 ms
4,352 KB
testcase_42 AC 15 ms
4,348 KB
testcase_43 AC 15 ms
4,352 KB
testcase_44 AC 15 ms
4,356 KB
testcase_45 AC 15 ms
4,352 KB
testcase_46 AC 15 ms
4,352 KB
testcase_47 AC 15 ms
4,348 KB
testcase_48 AC 15 ms
4,348 KB
testcase_49 AC 15 ms
4,348 KB
testcase_50 AC 15 ms
4,348 KB
testcase_51 AC 15 ms
4,348 KB
testcase_52 AC 15 ms
4,352 KB
testcase_53 AC 15 ms
4,348 KB
testcase_54 AC 15 ms
4,352 KB
testcase_55 AC 15 ms
4,348 KB
testcase_56 AC 15 ms
4,348 KB
testcase_57 AC 24 ms
10,492 KB
testcase_58 AC 27 ms
9,972 KB
testcase_59 AC 24 ms
10,692 KB
testcase_60 AC 33 ms
8,920 KB
testcase_61 AC 30 ms
9,588 KB
testcase_62 AC 30 ms
9,252 KB
testcase_63 AC 27 ms
10,112 KB
testcase_64 AC 20 ms
11,168 KB
testcase_65 AC 22 ms
10,760 KB
testcase_66 AC 29 ms
9,840 KB
testcase_67 AC 34 ms
9,188 KB
testcase_68 AC 31 ms
9,324 KB
testcase_69 AC 27 ms
9,972 KB
testcase_70 AC 26 ms
10,284 KB
testcase_71 AC 28 ms
9,848 KB
testcase_72 AC 23 ms
10,692 KB
testcase_73 AC 29 ms
9,720 KB
testcase_74 AC 20 ms
11,292 KB
testcase_75 AC 22 ms
11,168 KB
testcase_76 AC 26 ms
9,980 KB
testcase_77 AC 36 ms
9,248 KB
testcase_78 AC 35 ms
9,452 KB
testcase_79 AC 31 ms
10,180 KB
testcase_80 AC 38 ms
8,916 KB
testcase_81 AC 38 ms
9,240 KB
testcase_82 AC 32 ms
9,704 KB
testcase_83 AC 37 ms
8,916 KB
testcase_84 AC 36 ms
9,320 KB
testcase_85 AC 26 ms
10,648 KB
testcase_86 AC 25 ms
11,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "combined.cpp"

#include <algorithm>
#include <cassert>
#include <vector>

namespace atcoder {

struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    std::vector<int> parent_or_size;
};

}  // namespace atcoder

#include <iostream>
#line 70 "combined.cpp"

using namespace std;
using lint = long long;

void solve() {
    int n, m;
    cin >> n >> m;

    vector<lint> xs(n);
    for (auto& x : xs) cin >> x;
    xs.insert(xs.begin(), 0);
    xs.push_back(0);

    for (int i = 0; i <= n; i += 2) {
        xs[i] = -xs[i];
    }

    vector<lint> ds(n + 1);
    for (int i = 0; i <= n; ++i) {
        ds[i] = xs[i + 1] - xs[i];
    }

    atcoder::dsu uf(n + 1);
    while (m--) {
        int l, r;
        cin >> l >> r;
        uf.merge(--l, r);
    }

    for (auto g : uf.groups()) {
        lint sum = 0;
        for (auto v : g) sum += ds[v];

        if (sum != 0) {
            cout << "NO\n";
            return;
        }
    }

    cout << "YES\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0