43022023-03-22 17:30:13horvathabelÚtadócpp17Wrong answer 0/50777ms8252 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(n+1);
for (int i=1; i<n;i++){
	cin>>arak[i-1];
}

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(n-1); 
for (int i=2; i<=n;i++){
	ans+=db[i].first*arak[i-2]*2;
	ans=ans%32609;
	cnt[i-2]={{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<<"\n"; 

}
SubtaskSumTestVerdictTimeMemory
base0/50
1Wrong answer0/04ms4788 KiB
2Wrong answer0/0456ms8252 KiB
3Wrong answer0/24ms5300 KiB
4Wrong answer0/24ms5516 KiB
5Wrong answer0/24ms5844 KiB
6Wrong answer0/24ms5928 KiB
7Wrong answer0/24ms5992 KiB
8Time limit exceeded0/8771ms5376 KiB
9Wrong answer0/24ms6264 KiB
10Wrong answer0/24ms6476 KiB
11Wrong answer0/24ms6580 KiB
12Wrong answer0/26ms6444 KiB
13Wrong answer0/24ms6636 KiB
14Time limit exceeded0/2763ms6012 KiB
15Time limit exceeded0/2741ms6012 KiB
16Time limit exceeded0/2777ms6184 KiB
17Time limit exceeded0/2765ms6420 KiB
18Time limit exceeded0/2758ms6504 KiB
19Time limit exceeded0/2765ms6420 KiB
20Time limit exceeded0/2746ms6516 KiB
21Time limit exceeded0/2767ms6672 KiB
22Time limit exceeded0/2777ms6816 KiB
23Time limit exceeded0/2763ms6840 KiB
24Time limit exceeded0/2763ms6812 KiB