結果

問題 No.2115 Making Forest Easy
ユーザー Jaehyun KooJaehyun Koo
提出日時 2023-04-28 21:13:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 3,944 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 188,964 KB
実行使用メモリ 16,372 KB
最終ジャッジ日時 2024-04-28 22:58:55
合計ジャッジ時間 4,809 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
13,388 KB
testcase_01 AC 6 ms
13,900 KB
testcase_02 AC 14 ms
16,372 KB
testcase_03 AC 14 ms
13,768 KB
testcase_04 AC 14 ms
15,728 KB
testcase_05 AC 12 ms
14,536 KB
testcase_06 AC 13 ms
15,688 KB
testcase_07 AC 12 ms
13,512 KB
testcase_08 AC 14 ms
16,360 KB
testcase_09 AC 12 ms
13,768 KB
testcase_10 AC 14 ms
16,372 KB
testcase_11 AC 13 ms
14,408 KB
testcase_12 AC 12 ms
13,896 KB
testcase_13 AC 12 ms
13,896 KB
testcase_14 AC 12 ms
14,200 KB
testcase_15 AC 12 ms
13,512 KB
testcase_16 AC 13 ms
14,024 KB
testcase_17 AC 7 ms
13,128 KB
testcase_18 AC 8 ms
14,264 KB
testcase_19 AC 10 ms
14,664 KB
testcase_20 AC 11 ms
13,516 KB
testcase_21 AC 7 ms
13,260 KB
testcase_22 AC 7 ms
14,068 KB
testcase_23 AC 12 ms
14,920 KB
testcase_24 AC 13 ms
14,576 KB
testcase_25 AC 9 ms
14,356 KB
testcase_26 AC 12 ms
13,940 KB
testcase_27 AC 10 ms
14,196 KB
testcase_28 AC 12 ms
13,764 KB
testcase_29 AC 7 ms
14,136 KB
testcase_30 AC 12 ms
14,296 KB
testcase_31 AC 14 ms
14,924 KB
testcase_32 AC 13 ms
15,564 KB
testcase_33 AC 14 ms
16,196 KB
testcase_34 AC 7 ms
13,380 KB
testcase_35 AC 7 ms
14,024 KB
testcase_36 AC 14 ms
16,288 KB
testcase_37 AC 9 ms
14,024 KB
testcase_38 AC 8 ms
14,072 KB
testcase_39 AC 10 ms
14,964 KB
testcase_40 AC 13 ms
13,636 KB
testcase_41 AC 11 ms
14,324 KB
testcase_42 AC 14 ms
14,408 KB
testcase_43 AC 9 ms
14,068 KB
testcase_44 AC 8 ms
14,068 KB
testcase_45 AC 11 ms
13,968 KB
testcase_46 AC 13 ms
14,148 KB
testcase_47 AC 9 ms
13,388 KB
testcase_48 AC 14 ms
15,732 KB
testcase_49 AC 14 ms
15,860 KB
testcase_50 AC 11 ms
14,324 KB
testcase_51 AC 10 ms
14,244 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;
const int inv2 = (mod + 1) / 2;

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> merge(vector<node> &a, vector<node> &b) {
	int swp = 0;
	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;
	}
	bisect[sz(a)] = sz(b);
	mint tasum = 0, tssum = 0;
	mint asum = 0, ssum = 0;
	vector<node> c;
	for (int i = 0; i < sz(a); i++) {
		for (int j = (i ? bisect[i - 1] : 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 * ssum + a[i].S * asum;
		a[i].S = a[i].S * ssum;
		for (int j = bisect[i]; j < bisect[i + 1]; j++) {
			auto cp = b[j];
			cp.A = (tssum)*cp.A + tasum * cp.S;
			cp.S = (tssum)*cp.S;
			c.push_back(cp);
		}
	}
	vector<node> nxt(sz(a) + sz(c));
	merge(all(a), all(c), nxt.begin());
	a.clear();
	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++;
		}
		i = j;
		a.push_back(toput);
	}
	return a;
}
auto cmp = [](vector<node> &a, vector<node> &b) { return sz(a) > sz(b); };

vector<node> dfs(int x, int p = -1) {
	vector<node> a;
	sz[x] = 1;
	a.push_back((node){val[x], mint(1), mint(1)});
	priority_queue<vector<node>, vector<vector<node>>, decltype(cmp)> pq(cmp); // max heap
	pq.push(a);
	for (auto &y : gph[x]) {
		if (y == p)
			continue;
		auto b = dfs(y, x);
		for (auto &bb : b) {
			ret += mint(bb.j) * bb.A * mint((mod + 1) / 2);
			bb.A *= mint(inv2);
			bb.S *= mint(inv2);
		}
		b.push_back({val[x], mint(0), mint(inv2)});
		sort(all(b));
		pq.push(b);
		sz[x] += sz[y];
	}
	while (sz(pq) > 1) {
		auto x = pq.top();
		pq.pop();
		auto y = pq.top();
		pq.pop();
		pq.push(merge(x, y));
	}
	return pq.top();
}

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