結果
| 問題 |
No.1290 Addition and Subtraction Operation
|
| コンテスト | |
| ユーザー |
noshi91
|
| 提出日時 | 2020-11-13 22:10:39 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 109 ms / 2,000 ms |
| コード長 | 1,286 bytes |
| コンパイル時間 | 1,015 ms |
| コンパイル使用メモリ | 76,360 KB |
| 最終ジャッジ日時 | 2025-01-15 23:13:02 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 85 |
ソースコード
#include <iostream>
#include <vector>
#include <cstdint>
#include <cstddef>
#include <numeric>
using usize = std::size_t;
using i64 = std::int64_t;
struct with_sum {
std::vector<i64> w;
std::vector<usize> p;
with_sum(std::vector<i64> w_): w(w_), p(w_.size()) {
std::iota(p.begin(), p.end(), 0);
}
usize find(usize x) {
if (p[x] == x) {
return x;
} else {
return p[x] = find(p[x]);
}
}
void unite(usize x, usize y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
p[y] = x;
w[x] += w[y];
}
};
int main() {
usize n, m;
std::cin >> n >> m;
std::vector<i64> b(n + 1, 0);
for (usize i = 0; i != n; i += 1) {
i64 t;
std::cin >> t;
b[i] += t;
b[i + 1] += t;
}
for (usize i = 0; i < n + 1; i += 2) {
b[i] = -b[i];
}
with_sum uf(b);
for (usize i = 0; i != m; i += 1) {
usize l, r;
std::cin >> l >> r;
l -= 1;
uf.unite(l, r);
}
for (usize i = 0; i != n + 1; i += 1) {
if(uf.w[uf.find(i)] != 0) {
std::cout << "NO\n";
return 0;
}
}
std::cout << "YES\n";
return 0;
}
noshi91