結果

問題 No.828 全方位神童数
ユーザー smiken_61smiken_61
提出日時 2019-05-03 23:17:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 9,383 bytes
コンパイル時間 1,877 ms
コンパイル使用メモリ 154,408 KB
実行使用メモリ 34,928 KB
最終ジャッジ日時 2023-08-30 06:40:19
合計ジャッジ時間 7,690 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
16,924 KB
testcase_01 AC 8 ms
17,012 KB
testcase_02 AC 8 ms
16,868 KB
testcase_03 AC 13 ms
17,432 KB
testcase_04 AC 10 ms
16,864 KB
testcase_05 AC 12 ms
17,396 KB
testcase_06 AC 9 ms
16,876 KB
testcase_07 AC 9 ms
17,084 KB
testcase_08 AC 9 ms
16,996 KB
testcase_09 AC 8 ms
16,980 KB
testcase_10 AC 12 ms
17,184 KB
testcase_11 AC 9 ms
16,904 KB
testcase_12 AC 11 ms
17,260 KB
testcase_13 AC 50 ms
20,160 KB
testcase_14 AC 107 ms
23,732 KB
testcase_15 AC 38 ms
19,236 KB
testcase_16 AC 53 ms
20,424 KB
testcase_17 AC 46 ms
19,568 KB
testcase_18 AC 77 ms
21,672 KB
testcase_19 AC 139 ms
25,580 KB
testcase_20 AC 93 ms
22,660 KB
testcase_21 AC 122 ms
24,268 KB
testcase_22 AC 29 ms
18,456 KB
testcase_23 AC 14 ms
17,540 KB
testcase_24 AC 68 ms
21,136 KB
testcase_25 AC 30 ms
18,828 KB
testcase_26 AC 133 ms
24,624 KB
testcase_27 AC 116 ms
23,356 KB
testcase_28 AC 136 ms
24,648 KB
testcase_29 AC 125 ms
24,120 KB
testcase_30 AC 67 ms
21,216 KB
testcase_31 AC 63 ms
20,952 KB
testcase_32 AC 133 ms
24,796 KB
testcase_33 AC 145 ms
25,200 KB
testcase_34 AC 117 ms
23,616 KB
testcase_35 AC 76 ms
21,400 KB
testcase_36 AC 96 ms
22,456 KB
testcase_37 AC 60 ms
20,688 KB
testcase_38 AC 83 ms
21,972 KB
testcase_39 AC 136 ms
24,576 KB
testcase_40 AC 58 ms
20,560 KB
testcase_41 AC 48 ms
20,148 KB
testcase_42 AC 149 ms
25,356 KB
testcase_43 AC 82 ms
34,928 KB
testcase_44 AC 36 ms
23,716 KB
testcase_45 AC 50 ms
27,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

   #include <bits/stdc++.h>

//    #include <boost/multiprecision/cpp_int.hpp>
// #define int long long
 #define inf  1000000007
 #define pa pair<int,int>
 #define ll long long
 #define pal pair<double,double>
 #define ppap pair<pa,int>
  #define PI 3.14159265358979323846
  #define paa pair<int,char>
  #define  mp make_pair
  #define  pb push_back
  #define EPS (1e-10)
                                          
    int dx[8]={0,1,0,-1,1,1,-1,-1};
    int dy[8]={1,0,-1,0,-1,1,1,-1};
                                            using namespace std;
                                   			class pa3{
                                            	public:
                                            	int x;
                                   				int y,z;
                                            	pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {}
                                            	bool operator < (const pa3 &p) const{
                                            		if(x!=p.x) return x<p.x;
                                            		if(y!=p.y) return y<p.y;
                                            		 return z<p.z;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                   				bool operator > (const pa3 &p) const{
                                            		if(x!=p.x) return x>p.x;
                                            		if(y!=p.y) return y>p.y;
                                            		 return z>p.z;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa3 &p) const{
                                            		return x==p.x && y==p.y && z==p.z;
                                            	}
                                            		bool operator != (const pa3 &p) const{
                                            			return !( x==p.x && y==p.y && z==p.z);
                                            	}
                                            
                                            };
                                            
                                            class pa4{
                                            	public:
                                            	int x;
                                            	int y,z,w;
                                            	pa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {}
                                            	bool operator < (const pa4 &p) const{
                                            		if(x!=p.x) return x<p.x;
                                            		if(y!=p.y) return y<p.y;
                                            		if(z!=p.z)return z<p.z;
                                            		return w<p.w;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator > (const pa4 &p) const{
                                            		if(x!=p.x) return x>p.x;
                                            		if(y!=p.y) return y>p.y;
                                            		if(z!=p.z)return z>p.z;
                                            		return w>p.w;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa4 &p) const{
                                            		return x==p.x && y==p.y && z==p.z &&w==p.w;
                                            	}
                                            		
                                            
                                            };
                                            class pa2{
                                            	public:
                                            	int x,y;
                                            	pa2(int x=0,int y=0):x(x),y(y) {}
                                            	pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}
                                            	pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);}
                                            	bool operator < (const pa2 &p) const{
                                            		return y != p.y ? y<p.y: x<p.x;
                                            	}
                                            	bool operator > (const pa2 &p) const{
                                            		return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa2 &p) const{
                                            		return abs(x-p.x)==0 && abs(y-p.y)==0;
                                            	}
                                            	bool operator != (const pa2 &p) const{
                                            		return !(abs(x-p.x)==0 && abs(y-p.y)==0);
                                            	}
                                            		
                                            
                                            };
                                            

                      
                                string itos( int i ) {
                                ostringstream s ;
                                s << i ;
                                return s.str() ;
                                }
                                 
                                int gcd(int v,int b){
                                	if(v>b) return gcd(b,v);
                                	if(v==b) return b;
                                	if(b%v==0) return v;
                                	return gcd(v,b%v);
                                }
                 
                            
                                int mod;
int extgcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int d = extgcd(b, a%b, y, x);
    y -= a/b * x;
    return d;
}
pa operator+(const pa & l,const pa & r) {   
    return {l.first+r.first,l.second+r.second};                                    
}    
pa operator-(const pa & l,const pa & r) {   
    return {l.first-r.first,l.second-r.second};                                    
}  
                                
                int pr[1200010];
                int inv[1200010];
                
                int beki(int wa,int rr,int warukazu){
                	if(rr==0) return 1%warukazu;
                	if(rr==1) return wa%warukazu;
                	wa%=warukazu;
                	if(rr%2==1) return ((ll)beki(wa,rr-1,warukazu)*(ll)wa)%warukazu;
                	ll zx=beki(wa,rr/2,warukazu);
                	return (zx*zx)%warukazu;
                }

                
    			int comb(int nn,int rr){
    				if(rr<0 || rr>nn || nn<0) return 0;
    				int r=pr[nn]*inv[rr];
    				r%=mod;
    				r*=inv[nn-rr];
    				r%=mod;
    				return r;
    			}
                
                void gya(int ert){
                	pr[0]=1;
                	for(int i=1;i<=ert;i++){
                		pr[i]=(pr[i-1]*i)%mod;
                	}
                		inv[ert]=beki(pr[ert],mod-2,mod);
                	for(int i=ert-1;i>=0;i--){
                		inv[i]=inv[i+1]*(i+1)%mod;
                	}
                }
                
              //   cin.tie(0);
    		//	ios::sync_with_stdio(false);
    			//priority_queue<pa3,vector<pa3>,greater<pa3>> pq;            
                 //sort(ve.begin(),ve.end(),greater<int>());
//    mt19937(clock_per_sec);
  //  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ;
      
       
       
                               //----------------kokomade tenpure------------
vector<int> G[200020];
struct unionfind{
	private:
	public:
	
vector<int> par,ranks,kosuu,dekai;
	
	void shoki(int N){
		par.resize(N+1,0);
		ranks.resize(N+1,0);
	
		dekai.resize(N+1,1);
		
		for(int i=0;i<=N;i++){
			par[i]=i,dekai[i]=i;
		}
	}
	
	int de(int x){
		return dekai[root(x)];
	}
	
	int root(int x){
		return par[x]==x ? x : par[x]=root(par[x]);
	}

	bool same(int x,int y){
		return root(x)==root(y);
	}
	bool is_root(int x){
		return x==root(x);
	}
	void unite(int x,int y){
 		x=root(x);
	 	y=root(y);
	
	 	if(x==y) return;
		if(ranks[x]<ranks[y]){
			par[x]=y;
		
			dekai[y]=max(dekai[x],dekai[y]);
		}
	 	else {
			par[y]=x;
			if(ranks[x]==ranks[y]) ranks[x]=ranks[x]+1;
	 	
	 		dekai[x]=max(dekai[y],dekai[x]);
	 	}
		return;
	}
};





int ans[200020]={};

vector<int> G2[200020];


int R[200020];

ll kotae=1ll;
void dfs(int v,int val=1){
	
	
	for(auto ve:G2[v])dfs(ve,val+1);

	kotae*=(val+R[v])%inf;
	
	kotae%=inf;
}
 signed main(){
 	

    	       cin.tie(0);
   			ios::sync_with_stdio(false);
int n;
 	cin>>n;
 	for(int i=1;i<=n;i++)cin>>R[i];
 	for(int i=0;i<n-1;i++){
 		int y,yy;
 		cin>>y>>yy;
 		
 		if(y>yy)	G[y].pb(yy);
 		else G[yy].pb(y);
 	}
 unionfind uf;
 	uf.shoki(200020);

 	
 	for(int i=1;i<=n;i++){
 
 		for(auto v:G[i])if(v<i){
 			G2[i].pb(uf.de(v));
 			uf.unite(v,i);
 		}
 		
 	}
 	
 	dfs(n);
 	
 	//for(int i=1;i<=n;i++)cout<<ans[i]<<endl;
 	
 	cout<<kotae<<endl;
  	return 0;
  }
  
0