結果

問題 No.2360 Path to Integer
ユーザー shobonvipshobonvip
提出日時 2023-06-23 23:04:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 798 ms / 2,500 ms
コード長 4,248 bytes
コンパイル時間 4,780 ms
コンパイル使用メモリ 282,816 KB
実行使用メモリ 18,472 KB
最終ジャッジ日時 2024-07-01 02:51:31
合計ジャッジ時間 9,767 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 5 ms
6,944 KB
testcase_08 AC 39 ms
6,940 KB
testcase_09 AC 589 ms
15,172 KB
testcase_10 AC 153 ms
18,468 KB
testcase_11 AC 153 ms
18,472 KB
testcase_12 AC 392 ms
15,120 KB
testcase_13 AC 566 ms
15,172 KB
testcase_14 AC 798 ms
15,756 KB
testcase_15 AC 624 ms
15,172 KB
testcase_16 AC 779 ms
15,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef modint998244353 mint;
typedef long long ll;

pair<int,vector<pair<int,int>>> find_centroid(int st, int size, vector<vector<int>> &ikeru, vector<int> &sizedp, vector<int> &tansaku, vector<bool> &removed){
	vector<int> mada = {~st, st};
	tansaku[st] = 1;
	while (!mada.empty()){
		int i = mada.back(); mada.pop_back();
		if (i >= 0){
			for (int j: ikeru[i]){
				if (removed[j]) continue;
				if (tansaku[j] != 1){
					tansaku[j] = 1;
					mada.push_back(~j);
					mada.push_back(j);
				}
			}
		}else{
			i = ~i;
			int tmp = 1;
			for (int j: ikeru[i]){
				if (removed[j]) continue;
				if (tansaku[j] == 2) tmp += sizedp[j];
			}
			sizedp[i] = tmp;
			tansaku[i] = 2;
		}
	}
 
	int now = st;
	tansaku[now] = 3;
	vector<pair<int,int>> ret(0);
	int par = st;
	while (true){
		int v = 0, w = 0;
		if (size != sizedp[now]){
			ret = {make_pair(par, size-sizedp[now])};
		}
		for (int j: ikeru[now]){
			if (removed[j]) continue;
			if (tansaku[j] == 2){
				tansaku[j] = 3;
				ret.push_back(make_pair(j, sizedp[j]));
				if (v < sizedp[j]){
					v = sizedp[j];
					w = j;
				}
			}
		}
		if (v * 2 <= size) break;
		par = now;
		now = w;
	}
 
	removed[now] = true;
	return make_pair(now, ret);
}
 
void solve(int st, int size, vector<vector<int>> &ikeru, vector<int> &sizedp, vector<int> &tansaku, vector<bool> &removed, mint &ans, vector<mint> &a, vector<int> &asize){
	pair<int,vector<pair<int,int>>> ret = find_centroid(st, size, ikeru, sizedp, tansaku, removed);

	//vector<mint> goes((int)ret.second.size());
	if ((int)ret.second.size() > 0){
		vector<mint> come((int)ret.second.size());
		vector<mint> goes((int)ret.second.size());
		vector<mint> nums((int)ret.second.size());
		vector<tuple<int,int,mint>> dp;// KAIWAI, KETA, ATAI (goes)
		mint comesum = 0;
		mint goessum = 0;
		mint numssum = 0;
		int cnt = 0;
		for (pair<int,int> i: ret.second){
			vector<int> mada = {i.first};
			vector<int> sgn = {asize[i.first]};
			vector<mint> gotmp = {a[i.first]};
			vector<mint> cotmp = {a[i.first]};
			tansaku[i.first] = 4;
			while(!mada.empty()){
				//cout << (int)mada.size() << " " << sgn.size() << " " << gotmp.size() << " " << cotmp.size() << endl;
				int x = mada.back();
				mada.pop_back();

				numssum++;
				nums[cnt]++;

				int xsgn = sgn.back();
				sgn.pop_back();

				goes[cnt] += gotmp.back();
				//goessum += gotmp.back();
				mint nowgo = gotmp.back();
				goessum += nowgo;
				gotmp.pop_back();

				come[cnt] += cotmp.back();
				mint nowco = cotmp.back();
				comesum += nowco;
				cotmp.pop_back();

				ans += nowgo + mint(10).pow(xsgn) * a[ret.first];
				ans += nowco * mint(10).pow(asize[ret.first]) + a[ret.first];
				
				dp.push_back(tuple(cnt, xsgn, nowgo));

				for (int j: ikeru[x]){
					if (removed[j]) continue;
					if (tansaku[j] != 4){
						tansaku[j] = 4;
						mada.push_back(j);
						sgn.push_back(asize[j] + xsgn);
						gotmp.push_back(nowgo * mint(10).pow(asize[j]) + a[j]);
						cotmp.push_back(nowco + a[j] * mint(10).pow(xsgn));
					}
				}
			}
			cnt++;
		}

		for (auto [kaiwai, keta, atai]: dp){
			//cout << atai.val() << " " << keta << endl;
			ans += (atai + mint(10).pow(keta) * a[ret.first]) * (numssum - nums[kaiwai]) + mint(10).pow(keta + asize[ret.first]) * (comesum - come[kaiwai]);
		}
	}

	for (pair<int,int> i: ret.second){
		solve(i.first, i.second, ikeru, sizedp, tansaku, removed, ans, a, asize);
	}
}

int main(){
	int n; cin >> n;
	vector<string> as(n);
	for (int i=0; i<n; i++){
		cin >> as[i];
	}

	vector<int> asize(n);
	for (int i=0; i<n; i++){
		asize[i] = as[i].size();
	}

	vector<mint> a(n);
	for (int i=0; i<n; i++){
		//reverse(as[i].begin(), as[i].end());
		for (int j=0; j<(int)as[i].size(); j++){
			a[i] = a[i] * 10 + (as[i][j] - '0');
		}
	}

	vector<vector<int>> ikeru(n, vector<int>(0));
	vector<int> sizedp(n);
	vector<int> tansaku(n);
	vector<bool> removed(n);
	for (int i=0; i<n-1; i++){
		int a, b; cin >> a >> b;
		a--; b--;
		ikeru[a].push_back(b);
		ikeru[b].push_back(a);
	}
	mint ans = 0;
	for (int i=0; i<n; i++){
		ans += a[i];
	}

	solve(0, n, ikeru, sizedp, tansaku, removed, ans, a, asize);
	
	cout << ans.val() << endl;
}
 
0