2722021-06-22 15:22:21mraronEnergiatakarékos ellenőrzéscpp14Accepted 100/100105ms48004 KiB
#include<bits/stdc++.h>
using namespace std;

int n;
vector<int> adj[100001];
array<long long,2> dp[100001];
int st[100001];
int sz[100001];


void dfs(int x) {
    st[x]=1;

    vector<int> gy;
    sz[x]=1;
    for(auto i:adj[x]) {
        if(0==st[i]) {
            dfs(i);
            sz[x]+=sz[i];
            gy.push_back(i);
        }
    }
    
    if(gy.empty()) {
        dp[x]={0,0};
        return ;
    }

    dp[x][0]=2*sz[x]-2;
    for(int c:gy) {
        dp[x][1]+=min(dp[c][0]+2*sz[c], dp[c][1]+8);
        dp[x][0]+=dp[c][0];
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    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);
    
    cout<<min(dp[1][0], dp[1][1])<<"\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted14ms6524 KiB
2Accepted85ms18820 KiB
subtask25/5
3Accepted45ms21484 KiB
4Accepted43ms22252 KiB
5Accepted54ms23024 KiB
6Accepted48ms23808 KiB
7Accepted46ms24568 KiB
subtask315/15
8Accepted3ms11532 KiB
9Accepted3ms11548 KiB
10Accepted3ms11540 KiB
11Accepted3ms11540 KiB
12Accepted3ms11552 KiB
13Accepted3ms11552 KiB
14Accepted3ms11556 KiB
subtask415/15
15Accepted3ms11560 KiB
16Accepted4ms11560 KiB
17Accepted3ms11568 KiB
18Accepted3ms11652 KiB
19Accepted3ms11580 KiB
20Accepted3ms11580 KiB
21Accepted8ms11584 KiB
subtask565/65
22Accepted75ms23940 KiB
23Accepted75ms25108 KiB
24Accepted82ms26252 KiB
25Accepted82ms27412 KiB
26Accepted86ms28568 KiB
27Accepted105ms43332 KiB
28Accepted96ms48004 KiB
29Accepted90ms39436 KiB
30Accepted86ms36532 KiB
31Accepted81ms34456 KiB
32Accepted79ms35328 KiB
33Accepted93ms36540 KiB
34Accepted61ms39708 KiB
35Accepted61ms40844 KiB
36Accepted71ms41396 KiB
37Accepted59ms42520 KiB
38Accepted59ms43388 KiB
39Accepted64ms44504 KiB
40Accepted75ms45576 KiB