42542023-03-19 19:02:47horvathabelÚtadócpp17Wrong answer 0/50800ms8500 KiB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
vector<ll> g[50001];
vector<ll>c;
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() {
int n;
cin>>n;
c.assign(n+1, 0);
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; 
	arak.push_back(x);
}

dfs(1);
for (int i=1; i<=n;i++) db[i].first=db[i].first*(n-db[i].first);
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; 
for (auto x: cnt) cout<<x.first.first<<" "<<x.first.second<<" "<<x.second<<endl; 

}
SubtaskSumTestVerdictTimeMemory
base0/50
1Wrong answer0/04ms4284 KiB
2Wrong answer0/0493ms8500 KiB
3Wrong answer0/24ms4584 KiB
4Wrong answer0/24ms4800 KiB
5Wrong answer0/24ms5064 KiB
6Wrong answer0/24ms5260 KiB
7Wrong answer0/24ms5496 KiB
8Time limit exceeded0/8763ms5432 KiB
9Wrong answer0/28ms5920 KiB
10Wrong answer0/27ms6168 KiB
11Wrong answer0/28ms6220 KiB
12Wrong answer0/28ms6224 KiB
13Wrong answer0/27ms6320 KiB
14Time limit exceeded0/2800ms6060 KiB
15Time limit exceeded0/2768ms6268 KiB
16Time limit exceeded0/2726ms6292 KiB
17Time limit exceeded0/2754ms6568 KiB
18Time limit exceeded0/2765ms6444 KiB
19Time limit exceeded0/2737ms6772 KiB
20Time limit exceeded0/2763ms6836 KiB
21Time limit exceeded0/2745ms6968 KiB
22Time limit exceeded0/2774ms6920 KiB
23Time limit exceeded0/2750ms7028 KiB
24Time limit exceeded0/2763ms7056 KiB