結果

問題 No.2377 SUM AND XOR on Tree
ユーザー shobonvipshobonvip
提出日時 2023-07-07 23:00:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 459 ms / 4,000 ms
コード長 1,619 bytes
コンパイル時間 4,324 ms
コンパイル使用メモリ 264,424 KB
実行使用メモリ 12,876 KB
最終ジャッジ日時 2023-09-29 00:37:20
合計ジャッジ時間 13,717 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 371 ms
10,616 KB
testcase_09 AC 389 ms
10,560 KB
testcase_10 AC 381 ms
10,672 KB
testcase_11 AC 382 ms
10,868 KB
testcase_12 AC 399 ms
10,608 KB
testcase_13 AC 378 ms
10,676 KB
testcase_14 AC 339 ms
10,296 KB
testcase_15 AC 364 ms
10,564 KB
testcase_16 AC 351 ms
10,512 KB
testcase_17 AC 459 ms
10,452 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 333 ms
10,808 KB
testcase_24 AC 351 ms
10,864 KB
testcase_25 AC 364 ms
10,812 KB
testcase_26 AC 228 ms
11,596 KB
testcase_27 AC 229 ms
11,552 KB
testcase_28 AC 229 ms
11,508 KB
testcase_29 AC 216 ms
12,876 KB
testcase_30 AC 214 ms
12,700 KB
testcase_31 AC 219 ms
12,692 KB
testcase_32 AC 244 ms
10,456 KB
testcase_33 AC 236 ms
10,648 KB
testcase_34 AC 236 ms
10,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;

typedef modint998244353 mint;
typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}
template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

int main(){
	int n; cin >> n;
	vector ikeru(n, vector<int>(0));
	rep(i,0,n-1){
		int u, v; cin >> u >> v;
		u--; v--;
		ikeru[u].push_back(v);
		ikeru[v].push_back(u);
	}
	mint ans = 0;
	vector<int> a(n);
	rep(i,0,n)cin>>a[i];
	rep(num,0,30){
		vector<int> b(n);
		rep(i,0,n){
			if (a[i]>>num&1) b[i] = 1;
		}
		vector<int> mada = {~0, 0};
		vector<int> tansaku(n);
		tansaku[0] = 1;
		vector<mint> dp0(n);
		vector<mint> dp1(n);
		while(!mada.empty()){
			int i = mada.back();
			mada.pop_back();
			if (i >= 0){
				for(int j: ikeru[i]){
					if (tansaku[j] == 0){
						tansaku[j] = 1;
						mada.push_back(~j);
						mada.push_back(j);
					}
				}
			}else{
				i = ~i;
				mint tmp0 = 0;
				mint tmp1 = 0;
				if (b[i] == 0) tmp0++;
				else tmp1++;
				for (int j: ikeru[i]){
					if (tansaku[j] == 2){
						mint ntmp0 = tmp0 * dp1[j] + tmp1 * dp1[j] + tmp0 * dp0[j];
						mint ntmp1 = tmp1 * dp1[j] + tmp0 * dp1[j] + tmp1 * dp0[j];
						tmp0 = ntmp0;
						tmp1 = ntmp1;
					}
				}
				dp0[i] = tmp0;
				dp1[i] = tmp1;
				tansaku[i] = 2;
			}
		}
		ans += mint(2).pow(num) * dp1[0];
	}
	cout << ans.val() << endl;
}
0