結果

問題 No.2598 Kadomatsu on Tree
ユーザー umezoumezo
提出日時 2024-01-03 00:42:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 303 ms / 2,000 ms
コード長 1,406 bytes
コンパイル時間 2,404 ms
コンパイル使用メモリ 206,768 KB
実行使用メモリ 49,744 KB
最終ジャッジ日時 2024-01-03 00:43:09
合計ジャッジ時間 15,109 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
12,260 KB
testcase_01 AC 5 ms
12,260 KB
testcase_02 AC 4 ms
12,264 KB
testcase_03 AC 5 ms
12,260 KB
testcase_04 AC 4 ms
12,260 KB
testcase_05 AC 4 ms
12,260 KB
testcase_06 AC 4 ms
12,260 KB
testcase_07 AC 4 ms
12,260 KB
testcase_08 AC 4 ms
12,260 KB
testcase_09 AC 4 ms
12,260 KB
testcase_10 AC 5 ms
12,260 KB
testcase_11 AC 4 ms
12,260 KB
testcase_12 AC 5 ms
12,260 KB
testcase_13 AC 7 ms
12,424 KB
testcase_14 AC 7 ms
12,452 KB
testcase_15 AC 6 ms
12,396 KB
testcase_16 AC 7 ms
12,456 KB
testcase_17 AC 6 ms
12,348 KB
testcase_18 AC 6 ms
12,416 KB
testcase_19 AC 7 ms
12,452 KB
testcase_20 AC 5 ms
12,360 KB
testcase_21 AC 6 ms
12,384 KB
testcase_22 AC 6 ms
12,428 KB
testcase_23 AC 214 ms
19,028 KB
testcase_24 AC 215 ms
19,148 KB
testcase_25 AC 219 ms
19,156 KB
testcase_26 AC 212 ms
19,052 KB
testcase_27 AC 216 ms
19,072 KB
testcase_28 AC 214 ms
19,144 KB
testcase_29 AC 214 ms
19,180 KB
testcase_30 AC 209 ms
19,036 KB
testcase_31 AC 218 ms
19,168 KB
testcase_32 AC 227 ms
19,112 KB
testcase_33 AC 206 ms
19,228 KB
testcase_34 AC 201 ms
18,980 KB
testcase_35 AC 204 ms
19,272 KB
testcase_36 AC 217 ms
19,248 KB
testcase_37 AC 232 ms
19,232 KB
testcase_38 AC 220 ms
19,276 KB
testcase_39 AC 214 ms
19,276 KB
testcase_40 AC 216 ms
19,276 KB
testcase_41 AC 211 ms
19,276 KB
testcase_42 AC 215 ms
19,276 KB
testcase_43 AC 239 ms
19,276 KB
testcase_44 AC 213 ms
19,276 KB
testcase_45 AC 211 ms
19,276 KB
testcase_46 AC 214 ms
19,276 KB
testcase_47 AC 221 ms
19,276 KB
testcase_48 AC 303 ms
49,404 KB
testcase_49 AC 19 ms
15,500 KB
testcase_50 AC 183 ms
35,248 KB
testcase_51 AC 37 ms
21,040 KB
testcase_52 AC 60 ms
21,424 KB
testcase_53 AC 55 ms
16,872 KB
testcase_54 AC 42 ms
16,016 KB
testcase_55 AC 20 ms
13,572 KB
testcase_56 AC 93 ms
18,996 KB
testcase_57 AC 32 ms
14,712 KB
testcase_58 AC 208 ms
19,084 KB
testcase_59 AC 226 ms
19,168 KB
testcase_60 AC 256 ms
46,576 KB
testcase_61 AC 276 ms
49,744 KB
testcase_62 AC 274 ms
43,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

const int MOD=998244353;
const ll c2=499122177;

const int MAX=200200;
vector<int> G[MAX];
ll dp[MAX],ans[MAX],A[MAX];

void dfs1(int v,int p){
  dp[v]=1;
  for(auto nv:G[v]){
    if(nv==p) continue;
    dfs1(nv,v);
    dp[v]+=dp[nv];
  }
}

void dfs2(int v,int p){
  ll ls=0,ls2=0,rs=0,rs2=0;
  for(auto nv:G[v]){
    if(A[nv]<A[v]){
      ls=(ls+dp[nv])%MOD;
      ls2=(ls2+dp[nv]*dp[nv]%MOD)%MOD;
    }
    else if(A[nv]>A[v]){
      rs=(rs+dp[nv])%MOD;
      rs2=(rs2+dp[nv]*dp[nv]%MOD)%MOD;
    }
    ans[v]=((ls*ls%MOD-ls2+MOD)%MOD*c2%MOD+(rs*rs%MOD-rs2+MOD)%MOD*c2%MOD)%MOD;
  }
  
  int N=G[v].size();
  vector<ll> L(N),R(N);
  rep(i,N) L[i]=R[i]=dp[G[v][i]];
  for(int i=1;i<N;i++) L[i]=(L[i]+L[i-1])%MOD;
  for(int i=N-2;i>=0;i--) R[i]=(R[i]+R[i+1])%MOD;
  
  rep(i,N){
    if(G[v][i]==p) continue;
    dp[v]=1;
    if(i) dp[v]=(dp[v]+L[i-1])%MOD;
    if(i<N-1) dp[v]=(dp[v]+R[i+1])%MOD;
    dfs2(G[v][i],v);
  }
}
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n;
  cin>>n;
  rep(i,n-1){
    int u,v;
    cin>>u>>v;
    u--,v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  rep(i,n) cin>>A[i];
  
  dfs1(0,-1);
  dfs2(0,-1);
  
  ll sum=0;
  rep(i,n) sum=(sum+ans[i])%MOD;
  cout<<sum<<endl;
  
  return 0;
}
0