結果

問題 No.2634 Tree Distance 3
コンテスト
ユーザー 👑 potato167
提出日時 2024-02-14 15:08:24
言語 C++17(gcc12)
(gcc 12.4.0 + boost 1.90.0)
コンパイル:
g++-12 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,518 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 7,842 ms
コンパイル使用メモリ 336,196 KB
実行使用メモリ 39,672 KB
最終ジャッジ日時 2026-07-03 15:50:13
合計ジャッジ時間 17,684 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 2
other WA * 69
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
#include "testlib.h"
namespace po167{
struct UFtree
{
	using _F=int;
	int _n;
	std::vector<_F> wei;
	std::vector<int> q;
	int component;
	UFtree(int n):_n(n),wei(n),component(n),par(n){
		for(int i=0;i<n;i++){
			wei[i]=1,par[i]=i;
		}
	}
	void intialize(){
		for(auto x:q){
			wei[root(x)]=1;
			par[x]=x;
		}
		component=(int)par.size();
		q={};
	}
	//根っこを返す
	int root(int a){
		assert(0<=a&&a<_n);
		if(a==par[a]) return a;
		return par[a]=root(par[a]);
	}
	//trueなら1,falseなら0
	int same(int a,int b){
		assert(0<=a&&a<_n);
		assert(0<=b&&b<_n);
		if(root(a)==root(b)) return 1;
		else return 0;
	}
	_F size(int a){
		return wei[root(a)];
	}
	//a,bが違う根っこの元なら結合する,結合したらtrueを返す
	bool unite(int a,int b){
		a=root(a),b=root(b);
		if(a==b) return false;
		if(wei[a]<wei[b]) std::swap(a,b);
		par[b]=a;
		q.push_back(b);
		wei[a]+=wei[b];
		component--;
		return true;
	}
	private:
	std::vector<int> par;
};
}
using po167::UFtree;

const int MIN_N = 2;
const int MAX_N = 200000;
const int MIN_A = 0;
const int MAX_A = 1'000'000'000;


int main(){
	registerValidation();
	int N=inf.readInt(MIN_N,MAX_N);
	inf.readEoln();
	for(int i=0;i<N;i++){
		if(i) inf.readSpace();
		inf.readInt(MIN_A,MAX_A);
	}
	inf.readEoln();
	UFtree T(N);
	for(int i=0;i<N-1;i++){
		int a,b;
		a=inf.readInt(1,N);
		inf.readSpace();
		b=inf.readInt(1,N);
		inf.readEoln();
		T.unite(a-1,b-1);
	}
	ensure(T.component==1);
	inf.readEof();
}
0