43002023-03-22 14:51:00horvathabelÚtadócpp17Wrong answer 0/50800ms8924 KiB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
vector<ll> g[50001];
vector<ll>c(50001);
vector<pair<ll,int>> db ; 
ll dfs(int x){
	if (c[x]==0){
		db[x]={1,x};
		return db[x].first;
	} 
	else{
		for (int edge:g[x]){
			db[x].first+=dfs(edge);
			db[x].second=x; 
		}
	}
	return (db[x].first);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;

vector<int> p;
p.resize(n+1);
for (int i=1;i<n;i++)
{
ll x,y;
cin>>x>>y;
db.assign(n+1, {1,0}); 
c[x]++; 
p[y]=x;
g[x].push_back(y);

}
vector<ll> arak;
for (int i=1; i<n;i++){
	ll x;
	cin>>x; 
	x=x%32609;
	arak.push_back(x);
}

dfs(1);
for (int i=1; i<=n;i++) db[i].first=db[i].first*(n-db[i].first)%32609;
sort(db.begin(), db.end());
sort(arak.begin(), arak.end());
ll ans=0; 
vector<pair<pair<ll, ll>,ll>> cnt; 
for (int i=2; i<=n;i++){
	ans+=db[i].first*arak[i-2]*2;
	ans=ans%32609;
	cnt.push_back({{db[i].second, p[db[i].second]},arak[i-2]});
}
cout<<ans<<endl; 
for (auto x: cnt) cout<<x.first.second<<" "<<x.first.first<<" "<<x.second<<endl; 

}
SubtaskSumTestVerdictTimeMemory
base0/50
1Accepted0/04ms4808 KiB
2Wrong answer0/0472ms8924 KiB
3Wrong answer0/24ms5072 KiB
4Wrong answer0/24ms5372 KiB
5Wrong answer0/24ms5332 KiB
6Wrong answer0/24ms5328 KiB
7Wrong answer0/24ms5584 KiB
8Time limit exceeded0/8731ms5092 KiB
9Wrong answer0/26ms6364 KiB
10Wrong answer0/27ms6456 KiB
11Wrong answer0/28ms6456 KiB
12Wrong answer0/28ms6600 KiB
13Wrong answer0/27ms6620 KiB
14Time limit exceeded0/2800ms5360 KiB
15Time limit exceeded0/2773ms5528 KiB
16Time limit exceeded0/2741ms5640 KiB
17Time limit exceeded0/2758ms5628 KiB
18Time limit exceeded0/2765ms5736 KiB
19Time limit exceeded0/2773ms5800 KiB
20Time limit exceeded0/2762ms6020 KiB
21Time limit exceeded0/2754ms6140 KiB
22Time limit exceeded0/2769ms6260 KiB
23Time limit exceeded0/2745ms6100 KiB
24Time limit exceeded0/2757ms6172 KiB