結果
問題 | No.1054 Union add query |
ユーザー | 沙耶花 |
提出日時 | 2020-05-15 22:00:28 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 340 ms / 2,000 ms |
コード長 | 1,059 bytes |
コンパイル時間 | 2,339 ms |
コンパイル使用メモリ | 206,868 KB |
実行使用メモリ | 35,268 KB |
最終ジャッジ日時 | 2023-10-19 14:18:22 |
合計ジャッジ時間 | 5,412 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,348 KB |
testcase_01 | AC | 2 ms
4,348 KB |
testcase_02 | AC | 2 ms
4,348 KB |
testcase_03 | AC | 191 ms
10,800 KB |
testcase_04 | AC | 340 ms
35,268 KB |
testcase_05 | AC | 164 ms
7,112 KB |
testcase_06 | AC | 172 ms
16,904 KB |
testcase_07 | AC | 153 ms
16,904 KB |
testcase_08 | AC | 167 ms
16,904 KB |
testcase_09 | AC | 246 ms
34,640 KB |
testcase_10 | AC | 148 ms
34,640 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define modulo 1000000007 #define mod(mod_x) ((((long long)mod_x+modulo))%modulo) #define Inf 10000000000000 int main(){ int N,Q; scanf("%d %d",&N,&Q); vector<vector<int>> childs(N,vector<int>()); vector<int> parent(N,0); for(int i=0;i<N;i++){ childs[i].push_back(i); parent[i] = i; } vector<int> num(N,0); for(int i=0;i<Q;i++){ int T,A,B; scanf("%d %d %d",&T,&A,&B); if(T==1){ A--;B--; if(parent[A]==parent[B])continue; if(childs[parent[A]].size()>childs[parent[B]].size())swap(A,B); int p = parent[A]; int t = num[p]; for(int j=0;j<childs[p].size();j++){ int x = childs[p][j]; if(x!=p)num[x]+=num[p]; parent[x] = parent[B]; } for(int j=0;j<childs[p].size();j++){ int x = childs[p][j]; num[x]-=num[parent[B]]; childs[parent[B]].push_back(x); } } if(T==2){ A--; num[parent[A]]+=B; } if(T==3){ A--; int ans = num[A]; if(parent[A]!=A)ans += num[parent[A]]; printf("%d\n",ans); } } return 0; }