Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
- #define FILE_READ_IN freopen("input.txt","r",stdin);
- #define FILE_READ_OUT freopen("output.txt","w",stdout);
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- #define maxn 100
- int parent[maxn]; int SIZE[maxn];
- int points[maxn],extra[maxn];
- int get(int u){
- if(parent[u]==u)
- return u;
- return parent[u]=get(parent[u]);
- }
- void make_set(int n)
- {
- for(int i=1;i<=n;i++)
- {
- parent[i]=i;
- SIZE[i]=1;
- points[i]=0;
- extra[i]=0;
- }
- }
- void UNION(int u,int v)
- {
- if(get(u)==get(v)) return;
- u=get(u);
- v=get(v);
- if(points[u]>points[v])
- {
- parent[u]=v;
- //SIZE[v]+=SIZE[u];
- }
- else
- {
- parent[v]=u;
- SIZE[u]+=SIZE[v];
- //extra[v]+=points[u];
- }
- }
- int main(){
- FAST
- #ifndef ONLINE_JUDGE
- FILE_READ_IN
- FILE_READ_OUT
- #endif
- int n,m; cin>>n>>m;
- make_set(n);
- for(int i=0;i<m;i++)
- {
- string cmd; cin>>cmd;
- if(cmd=="add")
- {
- int u,v; cin>>u>>v;
- int p=get(u);
- extra[u]+=points[p];
- extra[v]+=points[u];
- points[p]+=v+points[u];
- }
- else if(cmd=="join")
- {
- int u,v; cin>>u>>v;
- UNION(u,v);
- }
- else
- {
- int x; cin>>x;
- int p=get(x);
- //cout<<extra[x]<<" "<<extra[p]<<"\n";
- cout<<points[p]-extra[x]<<"\n"; // substract the points before union from the result
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement