結果
問題 |
No.1054 Union add query
|
ユーザー |
![]() |
提出日時 | 2020-05-15 22:05:23 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,465 bytes |
コンパイル時間 | 933 ms |
コンパイル使用メモリ | 84,368 KB |
実行使用メモリ | 20,676 KB |
最終ジャッジ日時 | 2024-09-19 10:37:29 |
合計ジャッジ時間 | 4,720 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | TLE * 1 -- * 7 |
ソースコード
#include<iostream> #include<iomanip> #include<cmath> #include<string> #include<vector> #include<list> #include<algorithm> #include<map> #include<set> #include<queue> #include<stack> using namespace std; typedef long long ll; #define fi first #define se second #define mp make_pair #define rep(i, n) for(int i=0;i<n;++i) #define rrep(i, n) for(int i=n;i>=0;--i) const int inf=1e9+7; const ll mod=1e9+7; const ll big=1e18; const double PI=3.14159265359; int par[500005], siz[500005], num[500005]; int root(int x){ while(x!=par[x]) x = par[x]; return x; } int main() { int N, Q; cin>>N>>Q; int T[Q], A[Q], B[Q]; for(int i=0;i<Q;++i){ cin>>T[i]>>A[i]>>B[i]; A[i]--; if(T[i]==1){ B[i]--; } } for(int i=0;i<N;++i){ par[i] = i; siz[i] = 1; num[i] = 0; } int roota, rootb; for(int i=0;i<Q;++i){ if(T[i]==1){ roota = root(A[i]); rootb = root(B[i]); if(roota != rootb){ if(siz[roota] > siz[rootb]) swap(roota, rootb); par[roota] = rootb; siz[rootb] += siz[roota]; } } if(T[i]==2){ roota = root(A[i]); for(int j=0;j<N;++j){ if(roota == root(j)){ num[j] += B[i]; } } } if(T[i]==3){ cout<<num[A[i]]<<endl; } } }