結果
| 問題 |
No.1054 Union add query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-01-12 23:09:14 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,327 bytes |
| コンパイル時間 | 1,533 ms |
| コンパイル使用メモリ | 137,200 KB |
| 最終ジャッジ日時 | 2025-01-17 17:04:05 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 5 WA * 3 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
struct UnionFind{
int sz;
vector<int> par;
vector<int> rank;
vector<int> weight;
vector<int> lazy;
vector<vector<int>> vs;
UnionFind(int n) : sz(n) {
par.resize(sz);
rank.assign(sz, 0);
weight.resize(sz);
lazy.resize(sz);
vs.resize(sz);
for(int i = 0; i < sz; ++i){
par[i] = i;
vs[i].emplace_back(i);
}
}
int find(int x){
if(par[x] == x) return x;
else return par[x] = find(par[x]);
}
bool same(int x, int y){
return find(x) == find(y);
}
bool unite(int x, int y){
x = find(x), y = find(y);
if(x == y) return false;
if(rank[x] < rank[y]) swap(x, y);
par[y] = x;
for(int v : vs[y]) weight[v] += lazy[y] - lazy[x];
lazy[y] = 0;
if(rank[x] == rank[y]) ++rank[x];
return true;
}
void update(int x, int y){
lazy[find(x)] += y;
}
int prod(int x){
return weight[x] + lazy[find(x)];
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q, T, A, B;
cin >> N >> Q;
UnionFind uf(N);
for(int q = 0; q < Q; ++q){
cin >> T >> A >> B;
if(T == 1) uf.unite(A - 1, B - 1);
else if(T == 2) uf.update(A - 1, B);
else cout << uf.prod(A - 1) << '\n';
}
return 0;
}