結果

問題 No.828 全方位神童数
ユーザー ahe100ahe100
提出日時 2019-05-11 21:15:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 357 ms / 2,000 ms
コード長 1,445 bytes
コンパイル時間 1,681 ms
コンパイル使用メモリ 176,128 KB
実行使用メモリ 29,964 KB
最終ジャッジ日時 2023-09-14 18:24:12
合計ジャッジ時間 10,503 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
14,928 KB
testcase_01 AC 7 ms
14,744 KB
testcase_02 AC 7 ms
14,672 KB
testcase_03 AC 19 ms
15,768 KB
testcase_04 AC 10 ms
14,696 KB
testcase_05 AC 18 ms
15,572 KB
testcase_06 AC 9 ms
14,808 KB
testcase_07 AC 9 ms
14,800 KB
testcase_08 AC 10 ms
14,892 KB
testcase_09 AC 8 ms
14,672 KB
testcase_10 AC 15 ms
15,400 KB
testcase_11 AC 8 ms
14,744 KB
testcase_12 AC 16 ms
15,372 KB
testcase_13 AC 109 ms
20,260 KB
testcase_14 AC 244 ms
26,284 KB
testcase_15 AC 83 ms
18,840 KB
testcase_16 AC 115 ms
20,456 KB
testcase_17 AC 90 ms
19,424 KB
testcase_18 AC 163 ms
22,508 KB
testcase_19 AC 306 ms
28,980 KB
testcase_20 AC 198 ms
24,084 KB
testcase_21 AC 269 ms
27,016 KB
testcase_22 AC 59 ms
17,800 KB
testcase_23 AC 22 ms
15,768 KB
testcase_24 AC 161 ms
22,312 KB
testcase_25 AC 64 ms
18,020 KB
testcase_26 AC 320 ms
28,640 KB
testcase_27 AC 268 ms
26,552 KB
testcase_28 AC 317 ms
28,660 KB
testcase_29 AC 301 ms
27,812 KB
testcase_30 AC 161 ms
22,304 KB
testcase_31 AC 156 ms
22,060 KB
testcase_32 AC 315 ms
28,588 KB
testcase_33 AC 357 ms
29,916 KB
testcase_34 AC 271 ms
26,484 KB
testcase_35 AC 170 ms
22,500 KB
testcase_36 AC 220 ms
24,880 KB
testcase_37 AC 138 ms
21,384 KB
testcase_38 AC 193 ms
23,644 KB
testcase_39 AC 322 ms
28,852 KB
testcase_40 AC 134 ms
21,208 KB
testcase_41 AC 112 ms
20,136 KB
testcase_42 AC 357 ms
29,964 KB
testcase_43 AC 189 ms
25,736 KB
testcase_44 AC 76 ms
18,836 KB
testcase_45 AC 112 ms
20,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;

/*
*/

ll n,r[200010],ans=1,mod=1e9+7;
vector<ll> v[200010],v2[200010],d(200010);

struct UnionFind{
	int n;
	vector<int> p;
	UnionFind(){}
	UnionFind(int sz):n(sz),p(sz,0){iota(p.begin(),p.end(),0);}
	int find(int x){
		return (x==p[x]?x:p[x]=find(p[x]));
	}
	bool same(int x,int y){
		return find(x)==find(y);
	}
	void unite(int x,int y){
		x=find(x);y=find(y);
		if(x==y) return;
		if(x<y) swap(x,y);
		p[y]=x;
	}
};

int dijkstra1(int s,vector<ll>& d,vector<ll> v[],ll INF=1e18){
	int farthest=s;
	queue<int> Q;
	rep(i,d.size())d[i]=INF;
	Q.push(s);
	d[s]=0;
	while(!Q.empty()){
		int p = Q.front();Q.pop();
		if(d[farthest]<d[p])farthest=p;
		for(int q:v[p]){
			if(d[q]>d[p]+1){
				d[q]=d[p]+1;
				Q.push(q);
			}
		}
	}
	return farthest;
}

void init(){
	cin>>n;
	rep(i,n)cin>>r[i];
	rep(i,n-1){
		ll a,b;
		cin>>a>>b;
		v[a-1].push_back(b-1);
		v[b-1].push_back(a-1);
	}
}

int main(void){
	init();
	UnionFind f(n);
	rep(i,n){
		for(ll p:v[i]){
			if(p<i){
				v2[f.find(p)].push_back(i);
				v2[i].push_back(f.find(p));
				f.unite(p,i);
			}
		}
	}
	dijkstra1(n-1,d,v2);
	rep(i,n)ans=(ans*(d[i]+1+r[i]))%mod;
	cout<<ans<<endl;
	return 0;
}
0