結果

問題 No.1054 Union add query
ユーザー 👑 jupirojupiro
提出日時 2020-06-24 03:16:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 311 ms / 2,000 ms
コード長 2,195 bytes
コンパイル時間 2,098 ms
コンパイル使用メモリ 118,992 KB
実行使用メモリ 37,108 KB
最終ジャッジ日時 2023-09-16 20:38:13
合計ジャッジ時間 6,410 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 181 ms
10,824 KB
testcase_04 AC 311 ms
37,108 KB
testcase_05 AC 148 ms
7,068 KB
testcase_06 AC 150 ms
17,312 KB
testcase_07 AC 147 ms
17,452 KB
testcase_08 AC 153 ms
17,488 KB
testcase_09 AC 191 ms
36,184 KB
testcase_10 AC 134 ms
36,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0