結果

問題 No.828 全方位神童数
ユーザー beetbeet
提出日時 2019-05-03 22:22:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 2,770 bytes
コンパイル時間 2,287 ms
コンパイル使用メモリ 211,172 KB
実行使用メモリ 27,596 KB
最終ジャッジ日時 2023-08-30 06:11:43
合計ジャッジ時間 7,463 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 7 ms
4,416 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 6 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 46 ms
12,496 KB
testcase_14 AC 104 ms
22,648 KB
testcase_15 AC 34 ms
9,920 KB
testcase_16 AC 48 ms
12,748 KB
testcase_17 AC 38 ms
10,916 KB
testcase_18 AC 70 ms
16,904 KB
testcase_19 AC 132 ms
26,664 KB
testcase_20 AC 83 ms
19,508 KB
testcase_21 AC 117 ms
23,708 KB
testcase_22 AC 24 ms
8,004 KB
testcase_23 AC 9 ms
4,580 KB
testcase_24 AC 65 ms
15,400 KB
testcase_25 AC 26 ms
8,264 KB
testcase_26 AC 140 ms
25,420 KB
testcase_27 AC 109 ms
22,276 KB
testcase_28 AC 134 ms
25,356 KB
testcase_29 AC 120 ms
24,368 KB
testcase_30 AC 61 ms
15,396 KB
testcase_31 AC 59 ms
15,160 KB
testcase_32 AC 121 ms
25,164 KB
testcase_33 AC 140 ms
27,236 KB
testcase_34 AC 104 ms
22,440 KB
testcase_35 AC 64 ms
16,200 KB
testcase_36 AC 88 ms
19,412 KB
testcase_37 AC 54 ms
13,912 KB
testcase_38 AC 76 ms
17,772 KB
testcase_39 AC 126 ms
25,296 KB
testcase_40 AC 54 ms
13,868 KB
testcase_41 AC 43 ms
12,020 KB
testcase_42 AC 139 ms
27,596 KB
testcase_43 AC 80 ms
26,792 KB
testcase_44 AC 32 ms
12,328 KB
testcase_45 AC 48 ms
16,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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