42552023-03-19 19:04:16horvathabelÚtadócpp17Time limit exceeded 20/50800ms8464 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<<endl; 
for (auto x: cnt) cout<<x.first.second<<" "<<x.first.first<<" "<<x.second<<endl; 

}
SubtaskSumTestVerdictTimeMemory
base20/50
1Accepted0/04ms4136 KiB
2Accepted0/0518ms8464 KiB
3Accepted2/24ms4548 KiB
4Accepted2/24ms4760 KiB
5Accepted2/24ms5104 KiB
6Accepted2/24ms5184 KiB
7Accepted2/24ms5272 KiB
8Time limit exceeded0/8800ms5408 KiB
9Accepted2/27ms5928 KiB
10Accepted2/27ms6140 KiB
11Accepted2/27ms6352 KiB
12Accepted2/27ms6344 KiB
13Accepted2/27ms6336 KiB
14Time limit exceeded0/2800ms6056 KiB
15Time limit exceeded0/2768ms5984 KiB
16Time limit exceeded0/2749ms5992 KiB
17Time limit exceeded0/2773ms5868 KiB
18Time limit exceeded0/2767ms6000 KiB
19Time limit exceeded0/2736ms5968 KiB
20Time limit exceeded0/2778ms6100 KiB
21Time limit exceeded0/2773ms6212 KiB
22Time limit exceeded0/2760ms6340 KiB
23Time limit exceeded0/2748ms6296 KiB
24Time limit exceeded0/2764ms6404 KiB