結果

問題 No.2949 Product on Tree
ユーザー 👑 binapbinap
提出日時 2024-10-25 22:15:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 721 ms / 2,000 ms
コード長 4,829 bytes
コンパイル時間 5,209 ms
コンパイル使用メモリ 276,624 KB
実行使用メモリ 69,908 KB
最終ジャッジ日時 2024-10-25 22:16:16
合計ジャッジ時間 35,483 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 622 ms
43,888 KB
testcase_04 AC 595 ms
42,796 KB
testcase_05 AC 641 ms
43,908 KB
testcase_06 AC 608 ms
43,916 KB
testcase_07 AC 607 ms
42,968 KB
testcase_08 AC 608 ms
44,280 KB
testcase_09 AC 635 ms
44,720 KB
testcase_10 AC 599 ms
45,308 KB
testcase_11 AC 614 ms
46,220 KB
testcase_12 AC 640 ms
47,484 KB
testcase_13 AC 634 ms
48,200 KB
testcase_14 AC 627 ms
51,728 KB
testcase_15 AC 658 ms
56,340 KB
testcase_16 AC 664 ms
52,504 KB
testcase_17 AC 633 ms
54,220 KB
testcase_18 AC 674 ms
53,748 KB
testcase_19 AC 658 ms
58,504 KB
testcase_20 AC 668 ms
57,416 KB
testcase_21 AC 707 ms
58,084 KB
testcase_22 AC 648 ms
60,088 KB
testcase_23 AC 615 ms
44,824 KB
testcase_24 AC 650 ms
44,544 KB
testcase_25 AC 608 ms
44,704 KB
testcase_26 AC 642 ms
44,688 KB
testcase_27 AC 621 ms
44,812 KB
testcase_28 AC 654 ms
44,868 KB
testcase_29 AC 617 ms
45,296 KB
testcase_30 AC 653 ms
46,040 KB
testcase_31 AC 630 ms
47,804 KB
testcase_32 AC 672 ms
48,232 KB
testcase_33 AC 680 ms
52,408 KB
testcase_34 AC 691 ms
61,484 KB
testcase_35 AC 696 ms
55,424 KB
testcase_36 AC 686 ms
64,788 KB
testcase_37 AC 687 ms
65,684 KB
testcase_38 AC 684 ms
59,404 KB
testcase_39 AC 689 ms
60,428 KB
testcase_40 AC 721 ms
69,128 KB
testcase_41 AC 688 ms
63,380 KB
testcase_42 AC 698 ms
69,908 KB
testcase_43 AC 213 ms
35,908 KB
testcase_44 AC 241 ms
36,488 KB
testcase_45 AC 274 ms
45,924 KB
testcase_46 AC 246 ms
41,448 KB
testcase_47 AC 186 ms
31,332 KB
testcase_48 AC 255 ms
42,736 KB
権限があれば一括ダウンロードができます

ソースコード

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