結果

問題 No.2115 Making Forest Easy
ユーザー bayashikobayashiko
提出日時 2022-10-08 03:59:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,931 bytes
コンパイル時間 5,299 ms
コンパイル使用メモリ 280,148 KB
実行使用メモリ 48,188 KB
最終ジャッジ日時 2023-09-04 23:28:56
合計ジャッジ時間 8,696 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
27,848 KB
testcase_01 AC 24 ms
23,448 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#if defined(LOCAL)
#include<stdc++.h>
#else
#include<bits/stdc++.h>
#endif
#include<random>
#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
using namespace std;
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp=boost::multiprecision;
//#define mulint mp::cpp_int
//#define mulfloat mp::cpp_dec_float_100
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
//#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define MINF (lint)(2e18)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define flc(x) __builtin_popcountll(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
#define all(x) x.begin(),x.end()
//#define vec vector<lint>
#define nep(x) next_permutation(all(x))
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=3e5+5;
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
//vector<int> bucket[MAX_N/1000];
//constexpr int MOD=1000000007;
constexpr int MOD=998244353;
#include<atcoder/all>
using namespace atcoder;
typedef __int128_t llint;

using mint=modint998244353;

int N;
lint A[5005];
vector<int> edge[5005];
mint dp1[5005][505]; //頂点iを根とする部分木の、最大値がjになる切り方の総和
mint dp2[5005][505]; //頂点iを根とする部分木の、最大値がjになる切り方のサイズの総和
int dp3[5005]; //部分木のサイズ
mint pow2[5005];

void dfs(int now,int par){
    for(auto child:edge[now]){
        if(child==par) continue;
        dfs(child,now);
        //now-childの辺を切る場合、dp_sub[j]-1本の辺は自由になる
        //サイズ0,max0の切り方が2^(subsize-1)通りあると考える
        dp1[child][0]=pow2[dp3[child]-1];
        dp2[child][0]=0;
        //切らない場合、mergeする
        mint merge1[501],merge2[501];
        rep(i,501) rep(j,501){
            merge1[max(i,j)]+=dp1[now][i]*dp1[child][j];
            merge2[max(i,j)]+=dp1[now][i]*dp2[child][j]+dp1[child][j]*dp2[now][i];
        }
        rep(j,501){
            dp1[now][j]=merge1[j];
            dp2[now][j]=merge2[j];
        }
        dp3[now]+=dp3[child];
    }
}


int main(void){
    cin >> N;
    rep(i,N) cin>> A[i];
    rep(i,N-1){
        int u,v;
        cin >> u >> v;
        u--,v--;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    pow2[0]=1;
    rep(i,5004) pow2[i+1]=pow2[i]*2;
    rep(i,N) dp1[i][A[i]]=1,dp2[i][A[i]]=1,dp3[i]=1;
    dfs(0,-1);
    mint ans=0;
    rep(i,N) rep(j,501) ans+=dp2[i][j]*j*pow2[N-1-dp3[i]+(i==0)];
    cout << ans.val() << endl;
}
0