結果
| 問題 | No.1054 Union add query |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-06-24 03:16:48 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 335 ms / 2,000 ms |
| コード長 | 2,195 bytes |
| コンパイル時間 | 1,237 ms |
| コンパイル使用メモリ | 121,004 KB |
| 実行使用メモリ | 37,268 KB |
| 最終ジャッジ日時 | 2024-07-03 19:50:17 |
| 合計ジャッジ時間 | 5,309 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
ソースコード
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
class UnionFind {
public:
vector<int> Parent;
UnionFind(int N) {
Parent = vector<int>(N, -1);
}
int root(int A) {
if (Parent[A] < 0) {
return A;
}
return Parent[A] = root(Parent[A]);
}
long long size(int A) {
return -(long long)Parent[root(A)];
}
bool connect(int A, int B) {
A = root(A);
B = root(B);
if (A == B) {
return false;
}
/*
if (size(A) < size(B)) {
swap(A, B);
}
*/
Parent[A] += Parent[B];
Parent[B] = A;
return true;
}
};
int main()
{
/*
cin.tie(nullptr);
ios::sync_with_stdio(false);
*/
int n, kkt; scanf("%d %d", &n, &kkt);
vector<int> pl(n);
vector<vector<int>> v(n); for (int i = 0; i < n; i++) v[i].emplace_back(i);
vector<int> sc(n);
UnionFind uni(n);
while (kkt--) {
int t, a, b; scanf("%d %d %d", &t, &a, &b);
a--;
if (t == 1) {
b--;
a = uni.root(a);
b = uni.root(b);
if (a == b) continue;
if (uni.size(a) > uni.size(b)) swap(a, b);
for (auto e : v[a]) sc[e] += pl[a] - pl[b];
for (auto e : v[a]) v[b].emplace_back(e);
pl[a] = 0;
v[a].clear();
uni.connect(b, a);
}
if (t == 2) {
pl[uni.root(a)] += b;
}
if (t == 3) {
printf("%d\n", sc[a] + pl[uni.root(a)]);
}
}
return 0;
}