42562023-03-19 19:04:58horvathabelÚtadócpp17Time limit exceeded 20/50800ms8420 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() {
ios::sync_with_stdio(false);
cin.tie(0);
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/04ms4148 KiB
2Accepted0/0474ms8420 KiB
3Accepted2/24ms4576 KiB
4Accepted2/24ms4840 KiB
5Accepted2/24ms5184 KiB
6Accepted2/24ms5400 KiB
7Accepted2/24ms5612 KiB
8Time limit exceeded0/8800ms5624 KiB
9Accepted2/28ms6072 KiB
10Accepted2/26ms6108 KiB
11Accepted2/27ms6220 KiB
12Accepted2/26ms6296 KiB
13Accepted2/26ms6388 KiB
14Time limit exceeded0/2765ms6148 KiB
15Time limit exceeded0/2764ms6168 KiB
16Time limit exceeded0/2745ms6204 KiB
17Time limit exceeded0/2760ms6312 KiB
18Time limit exceeded0/2773ms6572 KiB
19Time limit exceeded0/2768ms6792 KiB
20Time limit exceeded0/2762ms6760 KiB
21Time limit exceeded0/2769ms6876 KiB
22Time limit exceeded0/2739ms6852 KiB
23Time limit exceeded0/2755ms6888 KiB
24Time limit exceeded0/2763ms6912 KiB