結果

問題 No.2949 Product on Tree
ユーザー 👑 binap
提出日時 2024-10-25 22:15:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 802 ms / 2,000 ms
コード長 4,829 bytes
コンパイル時間 5,711 ms
コンパイル使用メモリ 262,164 KB
最終ジャッジ日時 2025-02-24 23:34:58
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef long double ld;
typedef pair<int, int> P;

template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const dynamic_modint<m>& a) {os << a.val(); return os;}
template <int m> istream& operator>>(istream& is, static_modint<m>& a) {long long x; is >> x; a = x; return is;}
template <int m> istream& operator>>(istream& is, dynamic_modint<m>& a) {long long x; is >> x; a = x; return is;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;}
template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;}
template<typename T> ostream& operator<<(ostream& os, const set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename T> ostream& operator<<(ostream& os, const unordered_set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename S, auto op, auto e> ostream& operator<<(ostream& os, const atcoder::segtree<S, op, e>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}
template<typename S, auto op, auto e, typename F, auto mapping, auto composition, auto id> ostream& operator<<(ostream& os, const atcoder::lazy_segtree<S, op, e, F, mapping, composition, id>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}

template<typename T> void chmin(T& a, T b){a = min(a, b);}
template<typename T> void chmax(T& a, T b){a = max(a, b);}

// Rerooting
// Arranged from https://github.com/atcoder/live_library/blob/master/rerooting.cpp
// https://youtu.be/zG1L4vYuGrg?t=7092
// TODO: vertex info, edge info

template<typename S, auto op, auto e, auto leaf, typename T, auto mapping>
struct Rerooting {
	static_assert(is_convertible_v<decltype(op), function<S(S, S)>>, "op must work as S(S, S)");
	static_assert(is_convertible_v<decltype(e), function<S()>>, "e must work as S()");
	static_assert(is_convertible_v<decltype(leaf), function<S()>>, "leaf must work as S()");
	static_assert(is_convertible_v<decltype(mapping), function<S(T, S)>>, "mapping must work as S(T, S)");
	int n;
	vector<vector<pair<int, T>>> G;
	vector<vector<pair<int, T>>> G_inv;
	vector<vector<S>> dp;
	vector<S> ans;
	Rerooting(int n) : n(n), G(n), G_inv(n), dp(n), ans(n) {}
	void add_edge(int a, int b, T t) {
		G[a].emplace_back(b, t);
		G_inv[b].emplace_back(a, t);
	}
	void check(vector<vector<pair<int, T>>> G){
		int cnt = 0;
		rep(i, n) cnt += G[i].size();
		assert(cnt == 2 * (n - 1));
	}
	void init() {
		check(G);
		check(G_inv);
		dfs(0);
		bfs(0);
	}
	S dfs(int v, int p = -1) {
		bool isLeaf = true;
		S dpSum = e();
		dp[v] = vector<S>(G_inv[v].size(), e());
		rep(i, int(G_inv[v].size())) {
			auto [u, t] = G_inv[v][i];
			if (u == p) continue;
			isLeaf = false;
			dp[v][i] = mapping(t, dfs(u,v));
			dpSum = op(dpSum, dp[v][i]);
		}
		if(isLeaf) dpSum = leaf();
		return dpSum;
	}
	void bfs(int v, const S& dpP = e(), int p = -1) {
		int deg = G[v].size();
		rep(i, deg) if (G[v][i].first == p) dp[v][i] = dpP;
		
		vector<S> dpSumL(deg + 1, e());
		rep(i, deg) dpSumL[i + 1] = op(dpSumL[i], dp[v][i]);
		vector<S> dpSumR(deg + 1, e());
		for (int i = deg - 1; i >= 0; --i) dpSumR[i] = op(dpSumR[i + 1], dp[v][i]);
		ans[v] = dpSumL[deg];
		
		rep(i, deg) {
			auto [u, t] = G[v][i];
			if (u == p) continue;
			S d = op(dpSumL[i], dpSumR[i + 1]);
			if(p == -1 and deg == 1) d = leaf();
			bfs(u, mapping(t, d), v);
		}
	}
};

using mint = atcoder::modint998244353;

using S = mint;
S op(S s1, S s2){
	return s1 + s2;
}
S e(){
	return 0;
}
S leaf(){
	return 0;
}
using T = mint;
S mapping(T t, S s){
	return s * t + t;
}


int main(){
	int n;
	cin >> n;
	vector<int> a(n);
	cin >> a;
	
	Rerooting<S, op, e, leaf, T, mapping> rerooting(n);
	rep(i, n - 1){
		int u, v;
		cin >> u >> v;
		u--; v--;
		rerooting.add_edge(u, v, a[u]);
		rerooting.add_edge(v, u, a[v]);
	}
	rerooting.init();
	auto ans = rerooting.ans;
	mint res = 0;
	rep(i, n) res += ans[i] * a[i];
	cout << res / 2 << "\n";
	
	return 0;
}
0