53762023-04-27 06:18:16anonEnergiatakarékos ellenőrzéscpp17Time limit exceeded 30/100584ms22416 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;

int main()
{
    FastIO;

    ll i, a, b, r, c, s1, s2, N;

    cin >> N;

    vector<vector<ll>> g(N + 1);

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

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

    vector<ll> sol(N + 1, -1);
    vector<ll> tme(N + 1, 0);

    vector<ll> vis(N + 1, false);
    vis[1] = true;

    vector<ll> q;
    q.pb(1);

    while(q.size())
    {
        r = q.back();

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

            q.pop_back();

            continue;
        }

        c = false;

        for(auto x : g[r])
        {
            if(vis[x])
                continue;

            c = true;
            vis[x] = true;

            q.push_back(x);

            break;
        }

        if(c)
            continue;

        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];
        }

        q.pop_back();
    }

    cout << sol[1] << endl;

    return 0;
}

SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1700 KiB
2Accepted54ms18532 KiB
subtask20/5
3Time limit exceeded555ms10180 KiB
4Time limit exceeded564ms10444 KiB
5Time limit exceeded584ms10200 KiB
6Time limit exceeded532ms10436 KiB
7Time limit exceeded541ms10624 KiB
subtask315/15
8Accepted3ms2880 KiB
9Accepted3ms3124 KiB
10Accepted3ms3008 KiB
11Accepted3ms3012 KiB
12Accepted3ms3292 KiB
13Accepted3ms3152 KiB
14Accepted2ms3260 KiB
subtask415/15
15Accepted3ms3268 KiB
16Accepted3ms3496 KiB
17Accepted3ms3476 KiB
18Accepted3ms3416 KiB
19Accepted3ms3644 KiB
20Accepted3ms3724 KiB
21Accepted3ms3632 KiB
subtask50/65
22Accepted56ms20300 KiB
23Accepted57ms20428 KiB
24Accepted54ms20512 KiB
25Accepted54ms20660 KiB
26Accepted54ms20512 KiB
27Accepted59ms20688 KiB
28Accepted59ms21500 KiB
29Accepted57ms21100 KiB
30Accepted57ms20928 KiB
31Accepted57ms20652 KiB
32Accepted57ms20428 KiB
33Accepted56ms20392 KiB
34Time limit exceeded541ms11800 KiB
35Time limit exceeded558ms11928 KiB
36Time limit exceeded545ms11828 KiB
37Time limit exceeded573ms12360 KiB
38Accepted109ms21876 KiB
39Accepted50ms22120 KiB
40Accepted45ms22416 KiB