結果

問題 No.1290 Addition and Subtraction Operation
ユーザー kyort0nkyort0n
提出日時 2020-08-11 05:16:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 3,074 bytes
コンパイル時間 1,876 ms
コンパイル使用メモリ 177,644 KB
実行使用メモリ 14,156 KB
最終ジャッジ日時 2023-09-30 02:19:16
合計ジャッジ時間 6,164 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,456 KB
testcase_01 AC 2 ms
5,568 KB
testcase_02 AC 2 ms
5,452 KB
testcase_03 AC 2 ms
5,436 KB
testcase_04 AC 2 ms
5,460 KB
testcase_05 AC 2 ms
5,564 KB
testcase_06 AC 2 ms
5,640 KB
testcase_07 AC 2 ms
5,456 KB
testcase_08 AC 2 ms
5,436 KB
testcase_09 AC 3 ms
5,436 KB
testcase_10 AC 2 ms
5,452 KB
testcase_11 AC 2 ms
5,440 KB
testcase_12 AC 2 ms
5,420 KB
testcase_13 AC 2 ms
5,712 KB
testcase_14 AC 2 ms
5,432 KB
testcase_15 AC 2 ms
5,460 KB
testcase_16 AC 2 ms
5,436 KB
testcase_17 AC 2 ms
5,444 KB
testcase_18 AC 2 ms
5,568 KB
testcase_19 AC 2 ms
5,432 KB
testcase_20 AC 2 ms
5,440 KB
testcase_21 AC 2 ms
5,520 KB
testcase_22 AC 2 ms
5,648 KB
testcase_23 AC 2 ms
5,644 KB
testcase_24 AC 2 ms
5,456 KB
testcase_25 AC 3 ms
5,456 KB
testcase_26 AC 2 ms
5,448 KB
testcase_27 AC 2 ms
5,440 KB
testcase_28 AC 2 ms
5,612 KB
testcase_29 AC 2 ms
5,424 KB
testcase_30 AC 2 ms
5,568 KB
testcase_31 AC 2 ms
5,456 KB
testcase_32 AC 2 ms
5,504 KB
testcase_33 AC 2 ms
5,520 KB
testcase_34 AC 2 ms
5,444 KB
testcase_35 AC 2 ms
5,436 KB
testcase_36 AC 2 ms
5,484 KB
testcase_37 AC 2 ms
5,508 KB
testcase_38 AC 2 ms
5,444 KB
testcase_39 AC 2 ms
5,640 KB
testcase_40 AC 2 ms
5,452 KB
testcase_41 AC 2 ms
5,508 KB
testcase_42 AC 18 ms
5,928 KB
testcase_43 AC 18 ms
5,856 KB
testcase_44 AC 17 ms
5,924 KB
testcase_45 AC 17 ms
6,060 KB
testcase_46 AC 17 ms
5,984 KB
testcase_47 AC 17 ms
5,864 KB
testcase_48 AC 18 ms
5,852 KB
testcase_49 AC 17 ms
5,904 KB
testcase_50 AC 17 ms
6,000 KB
testcase_51 AC 17 ms
5,848 KB
testcase_52 AC 17 ms
5,864 KB
testcase_53 AC 18 ms
5,948 KB
testcase_54 AC 17 ms
5,848 KB
testcase_55 AC 17 ms
5,928 KB
testcase_56 AC 17 ms
5,920 KB
testcase_57 AC 25 ms
13,764 KB
testcase_58 AC 30 ms
14,076 KB
testcase_59 AC 24 ms
13,852 KB
testcase_60 AC 42 ms
14,088 KB
testcase_61 AC 39 ms
14,116 KB
testcase_62 AC 39 ms
14,016 KB
testcase_63 AC 30 ms
14,096 KB
testcase_64 AC 19 ms
13,888 KB
testcase_65 AC 22 ms
13,848 KB
testcase_66 AC 34 ms
14,092 KB
testcase_67 AC 44 ms
14,116 KB
testcase_68 AC 38 ms
14,116 KB
testcase_69 AC 30 ms
14,024 KB
testcase_70 AC 28 ms
14,004 KB
testcase_71 AC 32 ms
14,152 KB
testcase_72 AC 22 ms
13,812 KB
testcase_73 AC 37 ms
14,156 KB
testcase_74 AC 19 ms
13,820 KB
testcase_75 AC 21 ms
13,828 KB
testcase_76 AC 29 ms
14,080 KB
testcase_77 AC 43 ms
14,024 KB
testcase_78 AC 39 ms
14,020 KB
testcase_79 AC 31 ms
14,124 KB
testcase_80 AC 47 ms
14,012 KB
testcase_81 AC 46 ms
14,080 KB
testcase_82 AC 36 ms
14,152 KB
testcase_83 AC 46 ms
14,036 KB
testcase_84 AC 44 ms
14,096 KB
testcase_85 AC 26 ms
13,852 KB
testcase_86 AC 21 ms
13,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long long INF = 1e18;
//const ll mod = 1000000007;
ll N, M;
ll B[100500];
ll C[100500];
ll L[105000], R[105000];
struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<ll> Size;
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);
        for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        Size[x] += Size[y];
        return true;
    }

    ll size(int x){
        return Size[root(x)];
    }
};

ll sum[105000];
ll subsum[105000];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> M;
    for(int i = 1; i <= N; i++) cin >> B[i];
    for(int i = 0; i <= N; i++) {
        C[i] = B[i] + B[i+1];
    }
    N++;
    UnionFind vertex(N);
    for(int i = 0; i < M; i++) {
        cin >> L[i] >> R[i];
        L[i]--;
        if((R[i] - L[i]) % 2 == 0) {
            vertex.merge(L[i], R[i]);
        }
    }
    UnionFind bipartite(2 * N), connected(N);
    for(int i = 0; i < M; i++) {
        if((R[i] - L[i]) % 2 == 1) {
            ll a = vertex.root(L[i]);
            ll b = vertex.root(R[i]);
            bipartite.merge(a + N, b);
            bipartite.merge(a, b + N);
            connected.merge(a, b);
        }
    }
    for(int i = 0; i < N; i++) {
        sum[connected.root(vertex.root(i))] += C[i];
        if(bipartite.issame(vertex.root(i), connected.root(vertex.root(i)))) subsum[connected.root(vertex.root(i))] += C[i];
    }
    /*
    for(int i = 0; i < N; i++) {
        if(vertex.root(i) == i) cerr << i << " ";
    }
    cerr << endl;
    */
    string ans = "YES";
    for(int i = 0; i < N; i++) {
        if(vertex.root(i) != i) continue;
        if(connected.root(i) != i) continue;
        //cerr << i << " " << sum[i] << " " << subsum[i] << " " << bipartite.issame(i, i + N) << endl;
        if(bipartite.issame(i, i + N)) {
            if(abs(sum[i]) % 2 != 0) {
                //cerr << i << endl;
                ans = "NO";
            }
        } else {
            if(subsum[i] * 2 != sum[i]) {
                //cerr << i << endl;
                ans = "NO";
            }
        }
    }
    cout << ans << endl;
    return 0;
}
0