結果

問題 No.1290 Addition and Subtraction Operation
ユーザー MisterMister
提出日時 2020-11-30 10:03:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 2,801 bytes
コンパイル時間 1,072 ms
コンパイル使用メモリ 90,312 KB
実行使用メモリ 11,420 KB
最終ジャッジ日時 2023-10-11 02:47:30
合計ジャッジ時間 5,644 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 1 ms
4,352 KB
testcase_22 AC 2 ms
4,352 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,352 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 2 ms
4,352 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 1 ms
4,348 KB
testcase_39 AC 2 ms
4,352 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 15 ms
4,348 KB
testcase_43 AC 15 ms
4,348 KB
testcase_44 AC 15 ms
4,348 KB
testcase_45 AC 15 ms
4,348 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,348 KB
testcase_55 AC 15 ms
4,348 KB
testcase_56 AC 15 ms
4,352 KB
testcase_57 AC 24 ms
10,480 KB
testcase_58 AC 27 ms
10,016 KB
testcase_59 AC 24 ms
10,624 KB
testcase_60 AC 34 ms
8,908 KB
testcase_61 AC 29 ms
9,700 KB
testcase_62 AC 31 ms
9,372 KB
testcase_63 AC 26 ms
10,092 KB
testcase_64 AC 21 ms
11,036 KB
testcase_65 AC 23 ms
10,760 KB
testcase_66 AC 29 ms
9,836 KB
testcase_67 AC 34 ms
9,100 KB
testcase_68 AC 31 ms
9,312 KB
testcase_69 AC 27 ms
9,956 KB
testcase_70 AC 26 ms
10,232 KB
testcase_71 AC 28 ms
9,848 KB
testcase_72 AC 22 ms
10,744 KB
testcase_73 AC 30 ms
9,488 KB
testcase_74 AC 20 ms
11,420 KB
testcase_75 AC 22 ms
11,032 KB
testcase_76 AC 26 ms
9,960 KB
testcase_77 AC 35 ms
9,256 KB
testcase_78 AC 35 ms
9,652 KB
testcase_79 AC 32 ms
10,228 KB
testcase_80 AC 38 ms
9,192 KB
testcase_81 AC 38 ms
8,980 KB
testcase_82 AC 32 ms
9,784 KB
testcase_83 AC 36 ms
8,920 KB
testcase_84 AC 36 ms
9,252 KB
testcase_85 AC 27 ms
10,828 KB
testcase_86 AC 25 ms
11,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Tools/vector_alias.hpp"

#include <vector>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }
#line 2 "combined.cpp"

#include <algorithm>
#include <cassert>
#line 6 "combined.cpp"

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 71 "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