HTML & JS
#include<bits/stdc++.h>
using namespace std;
std::vector<int> rank1(300005);
std::vector<int> parent(300005);
std::vector<int> exp1(300005, 0);
int find_set(int v)
{
	return v == parent[v] ? v : parent[v] = find_set(parent[v]);
}
void make_Set(int v)
{
	parent[v] = v;
	rank1[v] = 0;
	exp1[v] = 0;
}
void union_sets(int u, int v)
{
	int a = find_set(u);
	int b = find_set(v);
	if (a != b)
	{
		if (rank1[a] < rank1[b])
		{
			swap(a, b);
		}
		parent[b] = a;
		if (rank1[a] == rank1[b])
			rank1[a]++;
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input_cf_edu.txt", "r", stdin);
	freopen("output_cf_edu.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n, m;

	cin >> n >> m;
	//cout << "hi";
	for (int i = 1; i <= n; i++)
	{
		make_Set(i);
	}
	while (m--)
	{
		string s;
		cin >> s;
		//cout << s << endl;
		if (s == "join")
		{
			int u, v;
			cin >> u >> v;
			union_sets(u, v);
		}
		else if (s == "get")
		{
			int u;
			cin >> u;
			cout << exp1[u] << "\n";

		}
		else if (s == "add")
		{
			int x, v;
			cin >> x >> v;
			int m = find_set(x);
			for (int i = 1; i <= n; i++)
			{
				if (parent[i] == m)
				{
					exp1[i] = exp1[i] + v;
				}
			}
		}

	}




	return 0;
}

Generated URL:

Output:

No Output
Report Bug