結果

問題 No.2360 Path to Integer
ユーザー 沙耶花沙耶花
提出日時 2023-06-23 22:43:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 246 ms / 2,500 ms
コード長 2,769 bytes
コンパイル時間 5,508 ms
コンパイル使用メモリ 280,860 KB
実行使用メモリ 60,444 KB
最終ジャッジ日時 2023-09-13 17:48:23
合計ジャッジ時間 7,717 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 19 ms
5,916 KB
testcase_09 AC 216 ms
29,932 KB
testcase_10 AC 168 ms
33,340 KB
testcase_11 AC 177 ms
33,196 KB
testcase_12 AC 163 ms
30,588 KB
testcase_13 AC 209 ms
30,240 KB
testcase_14 AC 246 ms
55,408 KB
testcase_15 AC 210 ms
30,064 KB
testcase_16 AC 237 ms
60,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001
using P = pair<long long,mint>;
vector<vector<P>> TT;
template <class Ss, Ss (*op0)(Ss, Ss), Ss (*e)(), class Sv, Ss (*op1)(Sv, Ss), class Se, Ss (*op2)(Se, Ss)>
struct rerooting{
	
	int n;
	vector<vector<int>> to;
	vector<vector<Se>> edge;
	
	vector<Ss> subtree,answer;
	
	vector<Sv> vertex;
	
	rerooting(int _n){
		n = _n;
		to.resize(n);
		edge.resize(n);
		subtree.resize(n,e());
		answer.resize(n,e());
		vertex.resize(n);
	}
	
	rerooting(vector<Sv> v){
		n = v.size();
		to.resize(n);
		edge.resize(n);
		subtree.resize(n,e());
		answer.resize(n,e());
		vertex = v;
	}
	
	void add_directed_edge(int u,int v,Se x){
		to[u].push_back(v);
		edge[u].push_back(x);
	}
	
	void add_edge(int u,int v,Se x){
		add_directed_edge(u,v,x);
		add_directed_edge(v,u,x);
	}
	
	void solve(){
		dfs(0,-1);
		redfs(0,-1,e());
	}
	
	void dfs(int cur,int par){
		Ss temp = e();
		for(int i=0;i<to[cur].size();i++){
			int nxt = to[cur][i];
			if(nxt==par)continue;
			dfs(nxt,cur);
			temp = op0(temp, op2(edge[cur][i], subtree[nxt]));
		}
		subtree[cur] = op1(vertex[cur], temp);
	}
	
	void redfs(int cur, int par, Ss ps){
		vector<Ss> P(to[cur].size()+1,e());
		rep(i,to[cur].size()){
			int nxt = to[cur][i];
			if(nxt!=par){
				P[i+1] = op2(edge[cur][i], subtree[nxt]);
				TT[cur].push_back(subtree[nxt]);
			}
			else {
				P[i+1] = op2(edge[cur][i], ps);
				TT[cur].push_back(ps);
			}
		}
		vector<Ss> S(P.rbegin(),P.rend()-1);
		S.insert(S.begin(),e());
		rep(i,to[cur].size()){
			P[i+1] = op0(P[i], P[i+1]);
			S[i+1] = op0(S[i+1], S[i]);
		}
		answer[cur] = op1(vertex[cur], P.back());

		for(int i=0;i<to[cur].size();i++){
			int nxt = to[cur][i];
			if(nxt!=par){
				redfs(nxt,cur,op1(vertex[cur], op0(P[i],S[to[cur].size()-1-i])));
			}
		}
	}
	
};



P op0(P a,P b){
	return make_pair(a.first+b.first, a.second+b.second);
}

P e(){
	return make_pair(0LL,0);
}

P op1(long long a,P b){
	b.first++;
	b.second++;
	b.second *= mint(10).pow(a);
	return b;
}

P op2(long long a,P b){
	return b;
}

int main(){
	
	int N;
	cin>>N;
	vector<string> s(N);
	vector<long long> c(N);
	rep(i,N){
		cin>>s[i];
		c[i] = s[i].size();
	}
	TT.resize(N);
	rerooting<P,op0,e,long long,op1,long long,op2> R(c);
	rep(i,N-1){
		int a,b;
		cin>>a>>b;
		a--,b--;
		R.add_edge(a,b,0);
	}
	
	R.solve();
	mint ans = 0;
	rep(i,N){
		rep(j,TT[i].size()){
			auto p = TT[i][j];
			ans += p.second * stoll(s[i]) * (N-p.first);
			//cout<<p.first<<','<<p.second.val()<<endl;
		}
		ans += mint(N) * stoll(s[i]);
	}
	cout<<ans.val()<<endl;
	return 0;
}
0