結果
問題 | No.1054 Union add query |
ユーザー |
![]() |
提出日時 | 2020-05-16 15:02:38 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,836 bytes |
コンパイル時間 | 1,800 ms |
コンパイル使用メモリ | 178,128 KB |
実行使用メモリ | 29,056 KB |
最終ジャッジ日時 | 2024-09-22 09:00:58 |
合計ジャッジ時間 | 5,835 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | TLE * 1 -- * 7 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll=long long; using vin=vector<int>; using vll=vector<long long>; using vvin=vector<vector<int>>; using vvll=vector<vector<long long>>; using vstr=vector<string>; using vvstr=vector<vector<string>>; using vch=vector<char>; using vvch=vector<vector<char>>; using vbo=vector<bool>; using vvbo=vector<vector<bool>>; using vpii=vector<pair<int,int>>; using pqsin=priority_queue<int,vector<int>,greater<int>>; #define mp make_pair #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep2(i,s,n) for(int i=(s);i<(int)(n);i++) #define all(v) v.begin(),v.end() #define decp(n) cout<<fixed<<setprecision((int)n) const int inf=1e9+7; const ll INF=1e18; vin par(500050); vin deep(500050); void init(int n); int root(int x); void unite(int x,int y); bool same(int x,int y); vvin baby(500050); int main(){ int n,q; scanf("%d %d",&n,&q); queue<int> que; init(n); vin wei(n+1,0); rep(i,q){ int t,a,b; scanf("%d %d %d",&t,&a,&b); if(t==1)unite(a,b); if(t==2){ for(auto kid:baby[root(a)])wei[kid]+=b; } if(t==3)que.push(wei[a]); } while(que.size()){ printf("%d\n",que.front()); que.pop(); } } void init(int n){ rep(i,n+1){ par[i]=i; deep[i]=0; baby[i].push_back(i); } } int root(int x){ if(par[x]==x)return x; else return root(par[x]); } void unite(int x,int y){ x=root(x); y=root(y); if(x==y)return; if(deep[x]<deep[y]){ par[x]=y; baby[y].insert(baby[y].end(),all(baby[x])); baby[x].clear(); } else{ par[y]=x; baby[x].insert(baby[x].end(),all(baby[y])); baby[y].clear(); if(deep[x]==deep[y])deep[x]++; } } bool same(int x,int y){ return root(x)==root(y); }