12422022-03-28 18:01:04Valaki2Energiatakarékos ellenőrzéscpp14Accepted 100/10090ms48536 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second

const int maxn = 1e5;

int n;
vector<int> g[maxn + 1];
int ans[maxn + 1];
int full[maxn + 1];
int par[maxn + 1];
int subtree_size[maxn + 1];

void dfs(int cur, int parent) {
    par[cur] = parent;
    for(int nei : g[cur]) {
        if(nei != parent) {
            dfs(nei, cur);
            subtree_size[cur] += subtree_size[nei];
            full[cur] += full[nei];
            ans[cur] += min(8 + ans[nei], 2 * subtree_size[nei] + full[nei]);
        }
    }
    subtree_size[cur]++;
    full[cur] += (subtree_size[cur] - 1) * 2;
}

void solve() {
    cin >> n;
    for(int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    dfs(1, 0);
    cout << ans[1] << "\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms6644 KiB
2Accepted86ms21580 KiB
subtask25/5
3Accepted46ms20916 KiB
4Accepted48ms21660 KiB
5Accepted48ms22520 KiB
6Accepted48ms23192 KiB
7Accepted48ms23932 KiB
subtask315/15
8Accepted3ms11720 KiB
9Accepted3ms11728 KiB
10Accepted3ms11728 KiB
11Accepted3ms11724 KiB
12Accepted4ms11728 KiB
13Accepted4ms11748 KiB
14Accepted3ms11752 KiB
subtask415/15
15Accepted3ms11740 KiB
16Accepted3ms11752 KiB
17Accepted3ms11748 KiB
18Accepted3ms11768 KiB
19Accepted3ms11756 KiB
20Accepted3ms11776 KiB
21Accepted3ms11764 KiB
subtask565/65
22Accepted75ms26656 KiB
23Accepted79ms27804 KiB
24Accepted83ms28956 KiB
25Accepted78ms30108 KiB
26Accepted74ms31284 KiB
27Accepted86ms41048 KiB
28Accepted90ms44732 KiB
29Accepted89ms39532 KiB
30Accepted78ms38016 KiB
31Accepted79ms37100 KiB
32Accepted89ms38052 KiB
33Accepted79ms39180 KiB
34Accepted52ms39192 KiB
35Accepted50ms40256 KiB
36Accepted48ms41540 KiB
37Accepted52ms43124 KiB
38Accepted54ms44896 KiB
39Accepted68ms47192 KiB
40Accepted64ms48536 KiB