結果
| 問題 |
No.1054 Union add query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-05-16 05:50:48 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,356 bytes |
| コンパイル時間 | 742 ms |
| コンパイル使用メモリ | 73,532 KB |
| 実行使用メモリ | 15,184 KB |
| 最終ジャッジ日時 | 2024-09-21 15:47:56 |
| 合計ジャッジ時間 | 4,876 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 5 WA * 3 |
ソースコード
#include <iostream>
#include <vector>
using namespace std;
struct UnionFind{
vector<int> rank, par;
void init(int n){
rank.resize(n);
par.resize(n);
for(int i = 0; i < n; i++){
rank[i] = 1;
par[i] = i;
}
}
int root(int x){
if(par[x] == x) return x;
return par[x] = root(par[x]);
}
bool same(int x, int y){
return root(x) == root(y);
}
int size(int x){
return rank[root(x)];
}
void unite(int x, int y){
x = root(x);
y = root(y);
if(x == y) return;
if(rank[y] > rank[x]) swap(x, y);
par[y] = x;
rank[x] += rank[y];
}
};
int main(){
long long N, Q;
scanf("%lld %lld", &N, &Q);
UnionFind U;
vector<long long> sum(N, 0);
vector<long long> sa(N, 0);
U.init(N);
for(int i = 0; i < Q; i++){
int t, a, b;
scanf("%d %d %d", &t, &a, &b);
a--;
b--;
if(t == 1) {
if(U.size(a) >= U.size(b)) sa[b] += sum[U.root(a)] - sum[U.root(b)];
else sa[a] += sum[U.root(b)] - sum[U.root(a)];
U.unite(a, b);
}
else if(t == 2){
b++;
sum[U.root(a)] += b;
}
else if(t == 3){
printf("%lld\n", sum[U.root(a)] - sa[a]);
}
}
}