結果

問題 No.2949 Product on Tree
ユーザー 沙耶花沙耶花
提出日時 2024-10-25 21:36:27
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 380 ms / 2,000 ms
コード長 853 bytes
コンパイル時間 4,557 ms
コンパイル使用メモリ 262,644 KB
実行使用メモリ 28,112 KB
最終ジャッジ日時 2024-10-25 21:36:56
合計ジャッジ時間 23,190 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 1000000000000000000LL

mint dp[200005];
mint a[200005];
mint ans = 0;

void dfs(int cv,int pv,vector<vector<int>> &E){
	mint sum = 0;
	mint s2 = 0;
	rep(i,E[cv].size()){
		int to = E[cv][i];
		if(to==pv)continue;
		dfs(to,cv,E);
		sum += dp[to];
		s2 -= dp[to]*dp[to];
	}
	s2 += sum * sum;
	s2 /= 2;
	ans += s2 * a[cv];
	ans += sum * a[cv];
	dp[cv] = sum*a[cv] + a[cv];
}

int main(){
	int n;
	cin>>n;
	rep(i,n){
		int t;
		cin>>t;
		a[i] = t;
	}
	
	vector<vector<int>> E(n);
	rep(i,n-1){
		int a,b;
		cin>>a>>b;
		a--,b--;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	
	dfs(0,-1,E);
	cout<<ans.val()<<endl;
	
	return 0;
}
0