43172023-03-24 09:40:39horvathabelÚtadócpp17Time limit exceeded 20/50800ms8256 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=0; i<n-1;i++){
	cin>>arak[i];
}

dfs(1);
for (int i=1; i<=n;i++) db[i].first=db[i].first*(n-db[i].first);

cout<<endl;
sort(db.begin(), db.end());
sort(arak.begin(), arak.end());

cout<<endl;

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
base20/50
1Accepted0/04ms4788 KiB
2Accepted0/0456ms8256 KiB
3Accepted2/24ms5192 KiB
4Accepted2/24ms5404 KiB
5Accepted2/24ms5620 KiB
6Accepted2/24ms5832 KiB
7Accepted2/24ms5784 KiB
8Time limit exceeded0/8800ms5156 KiB
9Accepted2/24ms6172 KiB
10Accepted2/24ms6124 KiB
11Accepted2/24ms6208 KiB
12Accepted2/24ms6348 KiB
13Accepted2/26ms6448 KiB
14Time limit exceeded0/2755ms5788 KiB
15Time limit exceeded0/2749ms5840 KiB
16Time limit exceeded0/2783ms5956 KiB
17Time limit exceeded0/2777ms6248 KiB
18Time limit exceeded0/2768ms6444 KiB
19Time limit exceeded0/2753ms6676 KiB
20Time limit exceeded0/2773ms6772 KiB
21Time limit exceeded0/2773ms6668 KiB
22Time limit exceeded0/2750ms6732 KiB
23Time limit exceeded0/2777ms6816 KiB
24Time limit exceeded0/2758ms6744 KiB