53772023-04-27 06:29:33anonEnergiatakarékos ellenőrzéscpp17Accepted 100/10064ms33412 KiB
#include <bits/stdc++.h>

#define FastIO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

#define pb push_back
#define xx first
#define yy second
#define mp make_pair

#define all(x) x.begin(), x.end()
#define sz(x) x.size()

typedef long long ll;

using namespace std;

const double PI = acos(-1);
const ll INF = 1LL << 62;

vector<vector<ll>> g;
vector<ll> sol, tme;
vector<bool> vis;

void dfs(ll r)
{
    vis[r] = true;

    if(sz(g[r]) == 1 && r != 1)
    {
        sol[r] = 0;
        tme[r] = 1;

        return;
    }

    for(auto x : g[r])
    {
        if(!vis[x])
            dfs(x);
    }

    sol[r] = 0;
    tme[r] = sz(g[r]);

    for(auto x : g[r])
    {
        if(sol[x] == -1)
            continue;

        sol[r] += min(1 + sol[x] + tme[x], 8 + sol[x]);
        tme[r] += tme[x];
    }
}

int main()
{
    FastIO;

    ll i, a, b, N;

    cin >> N;

    g.resize(N + 1);

    for(i = 1; i < N; i++)
    {
        cin >> a >> b;

        g[a].push_back(b);
        g[b].push_back(a);
    }

    sol.resize(N + 1);
    tme.resize(N + 1);
    vis.resize(N + 1);

    fill(all(sol), -1);

    dfs(1);

    cout << sol[1] << endl;

    return 0;
}

SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1888 KiB
2Accepted50ms17188 KiB
subtask25/5
3Accepted35ms17888 KiB
4Accepted37ms17832 KiB
5Accepted39ms18312 KiB
6Accepted35ms18120 KiB
7Accepted37ms18736 KiB
subtask315/15
8Accepted3ms3240 KiB
9Accepted2ms3332 KiB
10Accepted3ms3328 KiB
11Accepted3ms3328 KiB
12Accepted3ms3328 KiB
13Accepted3ms3452 KiB
14Accepted3ms3684 KiB
subtask415/15
15Accepted2ms3876 KiB
16Accepted2ms3968 KiB
17Accepted2ms4092 KiB
18Accepted2ms4172 KiB
19Accepted2ms4176 KiB
20Accepted3ms4280 KiB
21Accepted2ms4368 KiB
subtask565/65
22Accepted48ms19472 KiB
23Accepted52ms19508 KiB
24Accepted50ms19504 KiB
25Accepted48ms19588 KiB
26Accepted50ms19692 KiB
27Accepted64ms30360 KiB
28Accepted59ms33412 KiB
29Accepted54ms25368 KiB
30Accepted54ms22136 KiB
31Accepted52ms19576 KiB
32Accepted50ms19480 KiB
33Accepted50ms19440 KiB
34Accepted37ms20104 KiB
35Accepted37ms20044 KiB
36Accepted37ms20100 KiB
37Accepted37ms20648 KiB
38Accepted43ms20768 KiB
39Accepted45ms21192 KiB
40Accepted43ms20828 KiB