結果

問題 No.1054 Union add query
ユーザー 沙耶花
提出日時 2020-05-15 22:00:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 442 ms / 2,000 ms
コード長 1,059 bytes
コンパイル時間 2,911 ms
コンパイル使用メモリ 197,548 KB
最終ジャッジ日時 2025-01-10 11:37:18
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 8
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:11:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   11 |         scanf("%d %d",&N,&Q);
      |         ~~~~~^~~~~~~~~~~~~~~
main.cpp:24:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   24 |                 scanf("%d %d %d",&T,&A,&B);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#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;
}
0