結果

問題 No.1290 Addition and Subtraction Operation
ユーザー fastmath
提出日時 2020-11-14 17:52:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,508 bytes
コンパイル時間 2,036 ms
コンパイル使用メモリ 195,560 KB
最終ジャッジ日時 2025-01-16 00:35:43
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 85
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';

const int N = 1e5+7;
vector <int> g[N];
int sum[N], comp[N];
int a[N], d[N];

bool used[N];
void dfs(int u, int c) {
    used[u] = 1;
    comp[u] = c;
    sum[c] += d[u];
    for (int v : g[u]) {
        if (!used[v]) {
            dfs(v, c);
        }   
    }   
}   

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout.setf(ios::fixed); cout.precision(20); 
    #endif
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        if (i % 2 == 0)
            a[i] = -a[i];
        d[i] = a[i] - a[i - 1];

        //debug(d[i]);

    }   

    while (m--) {
        int l, r;
        cin >> l >> r;
        g[r + 1].app(l);
        g[l].app(r + 1);
    }   

    int ptr = 0;
    for (int i = 1; i <= n + 1; ++i) {
        if (!used[i]) {
            dfs(i, ptr);
            ptr++;
        }   
    }   
    for (int i = 0; i < ptr; ++i) {
        if (sum[i] != 0 && comp[n + 1] != i) {
            cout << "NO" << endl;
            exit(0);
        }   
    }   
    cout << "YES" << endl;
}
0