44262023-03-27 19:36:43horvathabelÚtadócpp17Time limit exceeded 20/50800ms8268 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 ; 
bool seen[50001];
ll dfs(int x){
	seen[x]=true; 
	if (c[x]==0){
		db[x]={1,x};
		return db[x].first;
	} 
	else{
		for (int edge:g[x]){
			if (!seen[edge]) db[x].first+=dfs(edge);
			else db[x].first+=db[edge].first; 
		}
		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);

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
base20/50
1Accepted0/04ms4800 KiB
2Accepted0/0456ms8268 KiB
3Accepted2/24ms5272 KiB
4Accepted2/24ms5488 KiB
5Accepted2/24ms5696 KiB
6Accepted2/24ms5656 KiB
7Accepted2/24ms5832 KiB
8Time limit exceeded0/8767ms5408 KiB
9Accepted2/26ms6364 KiB
10Accepted2/24ms6576 KiB
11Accepted2/24ms6784 KiB
12Accepted2/24ms6872 KiB
13Accepted2/24ms7076 KiB
14Time limit exceeded0/2800ms6216 KiB
15Time limit exceeded0/2765ms6304 KiB
16Time limit exceeded0/2745ms6312 KiB
17Time limit exceeded0/2762ms6324 KiB
18Time limit exceeded0/2765ms6456 KiB
19Time limit exceeded0/2762ms6440 KiB
20Time limit exceeded0/2776ms6220 KiB
21Time limit exceeded0/2773ms6216 KiB
22Time limit exceeded0/2749ms6344 KiB
23Time limit exceeded0/2764ms6424 KiB
24Time limit exceeded0/2773ms6360 KiB