12412022-03-28 17:50:05k_balintEnergiatakarékos ellenőrzéscpp14Accepted 100/10079ms44828 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int c=1e5+5;

vector<int> adj[c];
ll dp[c][2];
int sz[c];

void dfs(int v, int p){
    sz[v]=1;

    for(int x:adj[v]){
        if(x!=p){
            dfs(x,v);
            sz[v]+=sz[x];
            dp[v][0]+=min(dp[x][0]+8, dp[x][1]+2*sz[x]);
            dp[v][1]+=dp[x][1]+2*sz[x];
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n; cin>>n;
    for(int i=1;i<n;i++){
        int a,b; cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    dfs(1,0);

    cout << min(dp[1][0],dp[1][1]) << endl;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms6604 KiB
2Accepted79ms18144 KiB
subtask25/5
3Accepted37ms16368 KiB
4Accepted37ms17124 KiB
5Accepted35ms17908 KiB
6Accepted37ms18656 KiB
7Accepted39ms19444 KiB
subtask315/15
8Accepted3ms11596 KiB
9Accepted3ms11608 KiB
10Accepted4ms11608 KiB
11Accepted3ms11612 KiB
12Accepted3ms11616 KiB
13Accepted4ms11616 KiB
14Accepted3ms11620 KiB
subtask415/15
15Accepted3ms11632 KiB
16Accepted3ms11628 KiB
17Accepted3ms11636 KiB
18Accepted3ms11636 KiB
19Accepted3ms11648 KiB
20Accepted4ms11644 KiB
21Accepted3ms11656 KiB
subtask565/65
22Accepted61ms23264 KiB
23Accepted75ms24340 KiB
24Accepted64ms25552 KiB
25Accepted59ms26708 KiB
26Accepted59ms27864 KiB
27Accepted79ms38568 KiB
28Accepted75ms42268 KiB
29Accepted68ms36160 KiB
30Accepted65ms34576 KiB
31Accepted71ms33740 KiB
32Accepted67ms34612 KiB
33Accepted63ms35756 KiB
34Accepted39ms34536 KiB
35Accepted41ms35704 KiB
36Accepted41ms36944 KiB
37Accepted54ms38340 KiB
38Accepted56ms40160 KiB
39Accepted54ms43504 KiB
40Accepted54ms44828 KiB