結果

問題 No.2115 Making Forest Easy
ユーザー Jaehyun KooJaehyun Koo
提出日時 2023-04-27 21:26:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,540 ms / 2,000 ms
コード長 3,721 bytes
コンパイル時間 1,935 ms
コンパイル使用メモリ 181,008 KB
実行使用メモリ 13,312 KB
最終ジャッジ日時 2024-04-28 11:21:11
合計ジャッジ時間 38,285 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,520 KB
testcase_01 AC 7 ms
11,648 KB
testcase_02 AC 1,503 ms
13,184 KB
testcase_03 AC 1,500 ms
12,032 KB
testcase_04 AC 1,454 ms
12,928 KB
testcase_05 AC 1,125 ms
12,544 KB
testcase_06 AC 1,524 ms
13,312 KB
testcase_07 AC 95 ms
12,032 KB
testcase_08 AC 1,522 ms
13,184 KB
testcase_09 AC 38 ms
12,032 KB
testcase_10 AC 1,506 ms
13,056 KB
testcase_11 AC 1,487 ms
11,904 KB
testcase_12 AC 38 ms
11,904 KB
testcase_13 AC 1,310 ms
11,904 KB
testcase_14 AC 41 ms
12,032 KB
testcase_15 AC 95 ms
11,904 KB
testcase_16 AC 1,481 ms
11,904 KB
testcase_17 AC 40 ms
11,648 KB
testcase_18 AC 107 ms
11,776 KB
testcase_19 AC 665 ms
12,544 KB
testcase_20 AC 80 ms
12,032 KB
testcase_21 AC 28 ms
11,776 KB
testcase_22 AC 148 ms
11,648 KB
testcase_23 AC 1,059 ms
12,800 KB
testcase_24 AC 1,488 ms
12,032 KB
testcase_25 AC 407 ms
12,032 KB
testcase_26 AC 38 ms
12,032 KB
testcase_27 AC 637 ms
11,776 KB
testcase_28 AC 98 ms
11,904 KB
testcase_29 AC 54 ms
11,776 KB
testcase_30 AC 95 ms
11,904 KB
testcase_31 AC 1,433 ms
12,928 KB
testcase_32 AC 1,522 ms
13,184 KB
testcase_33 AC 1,540 ms
13,184 KB
testcase_34 AC 60 ms
11,648 KB
testcase_35 AC 175 ms
12,032 KB
testcase_36 AC 1,517 ms
13,184 KB
testcase_37 AC 593 ms
11,776 KB
testcase_38 AC 233 ms
11,776 KB
testcase_39 AC 448 ms
12,160 KB
testcase_40 AC 92 ms
12,032 KB
testcase_41 AC 39 ms
12,032 KB
testcase_42 AC 1,501 ms
12,032 KB
testcase_43 AC 50 ms
11,904 KB
testcase_44 AC 225 ms
11,648 KB
testcase_45 AC 75 ms
11,776 KB
testcase_46 AC 1,495 ms
11,904 KB
testcase_47 AC 39 ms
11,904 KB
testcase_48 AC 1,428 ms
12,672 KB
testcase_49 AC 1,457 ms
12,800 KB
testcase_50 AC 808 ms
11,904 KB
testcase_51 AC 429 ms
11,776 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; }
};
mint ret = 0;

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

vector<int> gph[MAXN];
int val[MAXN], sz[MAXN], n;
mint pwr[MAXN];

vector<node> dfs(int x, int p = -1) {
	vector<node> a;
	sz[x] = 1;
	a.push_back((node){val[x], mint(1), mint(1)});
	for (auto &y : gph[x]) {
		if (y == p)
			continue;
		auto b = dfs(y, x);
		vector<node> nxt;
		for (auto bb : b) {
			ret += mint(bb.j) * bb.A * mint((mod + 1) / 2);
		}
		sz[x] += sz[y];
		int swp = 0;
		if (sz(a) > sz(b))
			swap(a, b), swp = 1;
		vector<int> bisect(sz(a) + 1);
		int j = 0;
		for (int i = 0; i < sz(a); i++) {
			while (j < sz(b) && b[j].j < a[i].j)
				j++;
			bisect[i] = j;
		}
		mint tasum = 0, tssum = 0;
		bisect[sz(a)] = sz(b);
		for (int i = 0; i < sz(a); i++) {
			mint asum = 0, ssum = 0;
			for (int j = 0; j < bisect[i]; j++) {
				asum += b[j].A;
				ssum += b[j].S;
			}
			tasum += a[i].A;
			tssum += a[i].S;
			a[i].A = a[i].A * (mint(1 - swp) + ssum) + a[i].S * asum;
			a[i].S = a[i].S * (mint(1 - swp) + ssum);
			for (int j = bisect[i]; j < bisect[i + 1]; j++) {
				auto cp = b[j];
				cp.A = (tssum + mint(swp)) * cp.A + tasum * cp.S;
				cp.S = (tssum + mint(swp)) * cp.S;
				nxt.push_back(cp);
			}
		}
		for (int j = 0; j < bisect[0]; j++) {
			b[j].A = mint(swp) * b[j].A;
			b[j].S = mint(swp) * b[j].S;
			nxt.push_back(b[j]);
		}
		for (auto &z : a)
			nxt.push_back(z);
		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.S += nxt[j].S;
				j++;
			}
			toput.A /= mint(2);
			toput.S /= mint(2);
			i = j;
			a.push_back(toput);
		}
	}
	return a;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	pwr[0] = 1;
	for (int i = 0; i < n; i++) {
		pwr[i + 1] = pwr[i] * mint(2);
		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);
	for (auto &z : ans) {
		ret += z.A * mint(z.j);
	}
	cout << ret * pwr[n - 1] << "\n";
}
0