Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define mp make_pair
- #define mt make_tuple
- using namespace std;
- typedef long long ll;
- typedef pair<int,int> pii;
- typedef pair<ll,ll> pll;
- const ll INFL = (1LL<<62)-1;
- const int INF = (1<<30)-1;
- const int MAXN = 200001;
- int pai[MAXN];
- int expp[MAXN];
- int find(int in){
- if(pai[in]==in)
- return in;
- int cur = find(pai[in]);
- if(cur != pai[in]){
- expp[in]+=expp[pai[in]];
- }
- return pai[in] = cur;
- }
- void onion(int a, int b){
- a = find(a);
- b = find(b);
- if(rand()&1)
- swap(a,b);
- if(a==b)
- return;
- pai[a] = b;
- expp[a]-=expp[b];
- }
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int n, m;
- cin >> n >> m;
- iota(pai,pai+n+1,0);
- for(int i = 0; i < m; i++){
- string s;
- int a, b;
- cin >> s;
- if(s=="add"){
- cin >> a >> b;
- a = find(a);
- expp[a]+=b;
- } else if(s=="join"){
- cin >> a >> b;
- onion(a,b);
- } else {
- cin >> a;
- find(a);
- cout << expp[a] + (pai[a] == a ? 0 : expp[pai[a]]) << '\n';
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment