44272023-03-27 19:39:19horvathabelÚtadócpp17Time limit exceeded 20/50800ms7660 KiB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
vector<ll> g[50001];
ll c[500001];
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/04ms4156 KiB
2Accepted0/0456ms7660 KiB
3Accepted2/23ms4784 KiB
4Accepted2/23ms4996 KiB
5Accepted2/23ms4876 KiB
6Accepted2/23ms5132 KiB
7Accepted2/23ms5232 KiB
8Time limit exceeded0/8800ms4696 KiB
9Accepted2/24ms5436 KiB
10Accepted2/24ms5360 KiB
11Accepted2/24ms5576 KiB
12Accepted2/24ms5660 KiB
13Accepted2/24ms5796 KiB
14Time limit exceeded0/2774ms5244 KiB
15Time limit exceeded0/2768ms5132 KiB
16Time limit exceeded0/2762ms5612 KiB
17Time limit exceeded0/2777ms5616 KiB
18Time limit exceeded0/2773ms5740 KiB
19Time limit exceeded0/2768ms5740 KiB
20Time limit exceeded0/2764ms5748 KiB
21Time limit exceeded0/2785ms5744 KiB
22Time limit exceeded0/2771ms5816 KiB
23Time limit exceeded0/2754ms5896 KiB
24Time limit exceeded0/2745ms5960 KiB