結果
| 問題 |
No.1054 Union add query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-07-05 12:32:38 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 287 ms / 2,000 ms |
| コード長 | 2,388 bytes |
| コンパイル時間 | 2,041 ms |
| コンパイル使用メモリ | 208,068 KB |
| 最終ジャッジ日時 | 2025-01-22 17:43:46 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
コンパイルメッセージ
main.cpp: In function ‘void solve()’:
main.cpp:57:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
57 | scanf("%d %d", &N, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:66:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
66 | scanf("%d %d %d", &T, &A, &B);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
class UnionFindBySize {
int cnt;
vector<int> par, size;
public:
UnionFindBySize() :cnt(0) {}
UnionFindBySize(int _n) :cnt(_n), par(_n), size(_n, 1) {
for (int i = 0; i < _n; ++i) par[i] = i;
}
int find(int k) {
return (k == par[k]) ? k : (par[k] = find(par[k]));
}
int operator[](int k) {
return find(k);
}
int getSize(int k) {
return size[find(k)];
}
void unite(int x, int y) {
x = find(x); y = find(y);
if (x == y) return;
--cnt;
if (size[x] < size[y]) {
par[x] = y;
size[y] += size[x];
} else {
par[y] = x;
size[x] += size[y];
}
}
int count() {
return cnt;
}
};
void solve() {
int N, Q;
scanf("%d %d", &N, &Q);
UnionFindBySize UF(N);
vector<vi> V(N);
rep(i, N) {
V[i].push_back(i);
}
vll X(N), Y(N); // A[i] = X[i]+Y[UF[i]]
rep(ITER, Q) {
int T, A, B;
scanf("%d %d %d", &T, &A, &B);
--A;
if (T == 1) {
--B;
if (UF[A] == UF[B]) {
continue;
}
if (UF.getSize(A) < UF.getSize(B)) {
swap(A, B);
}
int u = UF[A], v = UF[B];
UF.unite(u, v);
each(a, V[v]) {
X[a] += Y[v] - Y[u];
}
ll y = Y[u];
V[u].insert(V[u].end(), all(V[v]));
V[v].clear();
V[v].shrink_to_fit();
} else if (T == 2) {
Y[UF[A]] += B;
} else {
printf("%lld\n", X[A] + Y[UF[A]]);
}
}
}
int main() {
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout << fixed << setprecision(15);
solve();
return 0;
}