結果
| 問題 |
No.828 全方位神童数
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2019-05-03 22:22:08 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 185 ms / 2,000 ms |
| コード長 | 2,770 bytes |
| コンパイル時間 | 2,479 ms |
| コンパイル使用メモリ | 207,384 KB |
| 最終ジャッジ日時 | 2025-01-07 03:26:59 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 43 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T,T MOD = 1000000007>
struct Mint{
T v;
Mint():v(0){}
Mint(signed v):v(v){}
Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}
Mint pow(long long k){
Mint res(1),tmp(v);
while(k){
if(k&1) res*=tmp;
tmp*=tmp;
k>>=1;
}
return res;
}
static Mint add_identity(){return Mint(0);}
static Mint mul_identity(){return Mint(1);}
Mint inv(){return pow(MOD-2);}
Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
Mint& operator/=(Mint a){return (*this)*=a.inv();}
Mint operator+(Mint a) const{return Mint(v)+=a;};
Mint operator-(Mint a) const{return Mint(v)-=a;};
Mint operator*(Mint a) const{return Mint(v)*=a;};
Mint operator/(Mint a) const{return Mint(v)/=a;};
Mint operator-() const{return v?Mint(MOD-v):Mint(v);}
bool operator==(const Mint a)const{return v==a.v;}
bool operator!=(const Mint a)const{return v!=a.v;}
bool operator <(const Mint a)const{return v <a.v;}
};
struct FastIO{
FastIO(){
cin.tie(0);
ios::sync_with_stdio(0);
}
}fastio_beet;
struct UnionFind{
Int n;
vector<Int> r,p,q;
UnionFind(){}
UnionFind(Int sz):n(sz),r(sz,1),p(sz,0),q(sz,0){
iota(p.begin(),p.end(),0);q=p;}
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(r[x]<r[y]) swap(x,y);
r[x]+=r[y];
chmax(q[x],q[y]);
p[y]=x;
}
Int size(Int x){
return r[find(x)];
}
Int val(Int x){
return q[find(x)];
}
};
//INSERT ABOVE HERE
signed main(){
Int n;
cin>>n;
vector<Int> r(n);
for(Int i=0;i<n;i++) cin>>r[i];
vector<vector<Int> > G(n);
for(Int i=1;i<n;i++){
Int x,y;
cin>>x>>y;
x--;y--;
if(x<y) swap(x,y);
G[x].emplace_back(y);
}
UnionFind uf(n);
vector<vector<Int> > H(n);
for(Int v=0;v<n;v++){
for(Int u:G[v]){
//cout<<v<<"->"<<uf.val(u)<<endl;
H[v].emplace_back(uf.val(u));
uf.unite(v,u);
}
}
vector<Int> dep(n,-1);
queue<Int> que;
dep[n-1]=1;
que.emplace(n-1);
while(!que.empty()){
Int v=que.front();que.pop();
for(Int u:H[v]){
if(~dep[u]) continue;
dep[u]=dep[v]+1;
que.emplace(u);
}
}
using M = Mint<Int>;
M ans(1);
for(Int i=0;i<n;i++) ans*=M(r[i]+dep[i]);
cout<<ans.v<<endl;
return 0;
}
beet