結果

問題 No.2115 Making Forest Easy
ユーザー Jaehyun KooJaehyun Koo
提出日時 2023-04-27 20:11:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 2,908 bytes
コンパイル時間 1,536 ms
コンパイル使用メモリ 177,508 KB
実行使用メモリ 12,612 KB
最終ジャッジ日時 2024-04-28 10:41:34
合計ジャッジ時間 4,490 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,468 KB
testcase_01 AC 3 ms
10,436 KB
testcase_02 AC 10 ms
12,088 KB
testcase_03 AC 232 ms
10,884 KB
testcase_04 AC 10 ms
11,672 KB
testcase_05 AC 8 ms
11,304 KB
testcase_06 AC 9 ms
12,172 KB
testcase_07 AC 11 ms
10,936 KB
testcase_08 AC 8 ms
12,228 KB
testcase_09 AC 15 ms
11,532 KB
testcase_10 AC 9 ms
12,176 KB
testcase_11 AC 90 ms
10,804 KB
testcase_12 AC 17 ms
12,456 KB
testcase_13 AC 48 ms
11,080 KB
testcase_14 AC 14 ms
11,168 KB
testcase_15 AC 12 ms
11,716 KB
testcase_16 AC 72 ms
10,804 KB
testcase_17 AC 4 ms
10,492 KB
testcase_18 AC 5 ms
10,664 KB
testcase_19 AC 7 ms
11,384 KB
testcase_20 AC 8 ms
10,704 KB
testcase_21 AC 4 ms
10,504 KB
testcase_22 AC 23 ms
10,504 KB
testcase_23 AC 7 ms
11,488 KB
testcase_24 AC 88 ms
10,820 KB
testcase_25 AC 7 ms
10,948 KB
testcase_26 AC 12 ms
10,780 KB
testcase_27 AC 61 ms
10,692 KB
testcase_28 AC 10 ms
10,700 KB
testcase_29 AC 4 ms
10,760 KB
testcase_30 AC 10 ms
11,460 KB
testcase_31 AC 9 ms
11,972 KB
testcase_32 AC 8 ms
12,184 KB
testcase_33 AC 8 ms
12,076 KB
testcase_34 AC 8 ms
10,824 KB
testcase_35 AC 5 ms
10,912 KB
testcase_36 AC 8 ms
12,612 KB
testcase_37 AC 41 ms
11,204 KB
testcase_38 AC 13 ms
10,476 KB
testcase_39 AC 5 ms
11,076 KB
testcase_40 AC 10 ms
11,072 KB
testcase_41 AC 13 ms
11,404 KB
testcase_42 AC 98 ms
11,204 KB
testcase_43 AC 8 ms
11,080 KB
testcase_44 AC 10 ms
10,552 KB
testcase_45 AC 9 ms
11,460 KB
testcase_46 AC 24 ms
11,588 KB
testcase_47 AC 7 ms
10,948 KB
testcase_48 AC 8 ms
11,740 KB
testcase_49 AC 8 ms
11,760 KB
testcase_50 AC 48 ms
11,076 KB
testcase_51 AC 27 ms
10,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = array<lint, 2>;
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
const int MAXN = 300005;
const int mod = 998244353;

struct mint {
	int val;
	mint() { val = 0; }
	mint(const lint &v) {
		val = (-mod <= v && v < mod) ? v : v % mod;
		if (val < 0)
			val += mod;
	}

	friend ostream &operator<<(ostream &os, const mint &a) { return os << a.val; }
	friend bool operator==(const mint &a, const mint &b) { return a.val == b.val; }
	friend bool operator!=(const mint &a, const mint &b) { return !(a == b); }
	friend bool operator<(const mint &a, const mint &b) { return a.val < b.val; }

	mint operator-() const { return mint(-val); }
	mint &operator+=(const mint &m) {
		if ((val += m.val) >= mod)
			val -= mod;
		return *this;
	}
	mint &operator-=(const mint &m) {
		if ((val -= m.val) < 0)
			val += mod;
		return *this;
	}
	mint &operator*=(const mint &m) {
		val = (lint)val * m.val % mod;
		return *this;
	}
	friend mint ipow(mint a, lint p) {
		mint ans = 1;
		for (; p; p /= 2, a *= a)
			if (p & 1)
				ans *= a;
		return ans;
	}
	mint inv() const { return ipow(*this, mod - 2); }
	mint &operator/=(const mint &m) { return (*this) *= m.inv(); }

	friend mint operator+(mint a, const mint &b) { return a += b; }
	friend mint operator-(mint a, const mint &b) { return a -= b; }
	friend mint operator*(mint a, const mint &b) { return a *= b; }
	friend mint operator/(mint a, const mint &b) { return a /= b; }
	operator int64_t() const { return val; }
};

struct node {
	int j;
	mint A, B, S;
	bool operator<(const node &nd) const { return j < nd.j; }
};

vector<int> gph[MAXN];
int val[MAXN];

vector<node> dfs(int x, int p = -1) {
	vector<node> a;
	a.push_back((node){val[x], mint(1), mint(0), mint(1)});
	for (auto &y : gph[x]) {
		if (y == p)
			continue;
		auto b = dfs(y, x);
		vector<node> nxt;
		for (auto aa : a) {
			for (auto bb : b) {
				node comb = (node){max(aa.j, bb.j), aa.A * bb.S + bb.A * aa.S, aa.B * bb.S + bb.B * aa.S, aa.S * bb.S};
				nxt.push_back(comb);
				node mrg = (node){aa.j, aa.A * bb.S, (bb.B + mint(bb.j) * bb.A) * aa.S + aa.B * bb.S, aa.S * bb.S};
				nxt.push_back(mrg);
			}
		}
		a.clear();
		sort(all(nxt));
		for (int i = 0; i < sz(nxt);) {
			int j = i;
			node toput;
			toput.j = nxt[i].j;
			while (j < sz(nxt) && nxt[i].j == nxt[j].j) {
				toput.A += nxt[j].A;
				toput.B += nxt[j].B;
				toput.S += nxt[j].S;
				j++;
			}
			i = j;
			a.push_back(toput);
		}
	}
	return a;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> val[i];
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		gph[u].push_back(v);
		gph[v].push_back(u);
	}
	auto ans = dfs(0);
	mint ret = 0;
	for (auto &z : ans) {
		ret += z.A * mint(z.j) + z.B;
	}
	cout << ret << "\n";
}
0