100772024-03-26 16:18:20111Energiatakarékos ellenőrzéscpp17Accepted 100/10071ms56572 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
#ifndef ONLINE_JUDGE
	freopen("be1.txt","r",stdin);
#endif
	int N;
	cin>>N;
	vector<vector<int>>g(N+1);
	for(int i=0;i<N-1;i++){
		int a,b;
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	vector<int>s(N+1),a(N+1),b(N+1),c(N+1);
	auto dfs=[&](auto self,int i)->void{
		s[i]=1;
		for(int j:g[i]){
			if(s[j]){
				continue;
			}
			self(self,j);
			s[i]+=s[j];
			c[i]+=c[j];
			int x=b[j]+s[j]*2+c[j];
			int y=a[j]+8;
			if(y<x){
				a[i]+=y;
				c[i]+=4;
			}
			else{
				a[i]+=x;
			}
			b[i]+=min(x,y);
		}
	};
	dfs(dfs,1);
	cout<<a[1]<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1956 KiB
2Accepted59ms21484 KiB
subtask25/5
3Accepted41ms22688 KiB
4Accepted41ms23676 KiB
5Accepted41ms24504 KiB
6Accepted41ms25396 KiB
7Accepted41ms26400 KiB
subtask315/15
8Accepted3ms8016 KiB
9Accepted3ms7944 KiB
10Accepted3ms7948 KiB
11Accepted2ms7916 KiB
12Accepted2ms7956 KiB
13Accepted3ms7960 KiB
14Accepted3ms8188 KiB
subtask415/15
15Accepted2ms8180 KiB
16Accepted3ms8312 KiB
17Accepted2ms8396 KiB
18Accepted3ms8528 KiB
19Accepted3ms8620 KiB
20Accepted3ms8620 KiB
21Accepted3ms8624 KiB
subtask565/65
22Accepted57ms28020 KiB
23Accepted57ms29196 KiB
24Accepted61ms30660 KiB
25Accepted61ms32044 KiB
26Accepted57ms33216 KiB
27Accepted70ms50872 KiB
28Accepted71ms56572 KiB
29Accepted65ms45416 KiB
30Accepted68ms42016 KiB
31Accepted64ms39756 KiB
32Accepted61ms40492 KiB
33Accepted61ms41532 KiB
34Accepted50ms43292 KiB
35Accepted46ms44428 KiB
36Accepted46ms45604 KiB
37Accepted50ms47432 KiB
38Accepted52ms48836 KiB
39Accepted56ms49956 KiB
40Accepted54ms51244 KiB