Submission #70151017


Source Code Expand

// To scale the heights, one must embrace the fall.
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
#define pb emplace_back
#define F(i, a, b) for (int i = (a); i <= (b); ++i)
#define dF(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 500005, M = (N << 1), inf = 1e16, mod = 1e9 + 7;
int n, ans, p[N], sz[N];
vector<int> G[N];
void dfs(int u) {
    sz[u] = 1;
    for (int v : G[u]) {
        dfs(v), sz[u] += sz[v];
    }
}
void dfs(int u, int lim) {
    int mx = 0, sum = 0;
    for (int v : G[u]) {
        sum += sz[v];
        if (sz[v] > sz[mx]) mx = v;
    }
    if (sz[mx] * 2 <= sum) {
        ans += min(lim, sum / 2);
        return;
    } else {
        ans += sum - sz[mx];
        dfs(mx, min(lim - sum + sz[mx], sz[mx] * 2 - sum >> 1));
    }
}
void solve(int TC) {
    cin >> n;
    F(i, 1, n) G[i].clear();
    ans = 0;
    F(i, 2, n) {
        cin >> p[i];
        G[p[i]].pb(i);
    }
    dfs(1);
    dfs(1, n / 2);
    cout << ans << '\n';
}
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    for (int i = 1; i <= T; i ++) {
        solve(i);
    }
    return 0;
}

Submission Info

Submission Time
Task D - Non-Ancestor Matching
User zhutianrui
Language C++ 20 (gcc 12.2)
Score 600
Code Size 1313 Byte
Status AC
Exec Time 126 ms
Memory 62120 KiB

Compile Error

Main.cpp: In function ‘void dfs(long long int, long long int)’:
Main.cpp:34:52: warning: suggest parentheses around ‘-’ inside ‘>>’ [-Wparentheses]
   34 |         dfs(mx, min(lim - sum + sz[mx], sz[mx] * 2 - sum >> 1));
      |                                         ~~~~~~~~~~~^~~~~
Main.cpp: In function ‘void solve(long long int)’:
Main.cpp:37:16: warning: unused parameter ‘TC’ [-Wunused-parameter]
   37 | void solve(int TC) {
      |                ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 1
AC × 87
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt, 02_handmade_10.txt, 02_handmade_11.txt, 02_handmade_12.txt, 02_handmade_13.txt, 02_handmade_14.txt, 02_handmade_15.txt, 02_handmade_16.txt, 02_handmade_17.txt, 02_handmade_18.txt, 02_handmade_19.txt, 02_handmade_20.txt, 02_handmade_21.txt, 02_handmade_22.txt, 02_handmade_23.txt, 02_handmade_24.txt, 02_handmade_25.txt, 02_handmade_26.txt, 02_handmade_27.txt, 02_handmade_28.txt, 02_handmade_29.txt, 02_handmade_30.txt, 02_handmade_31.txt, 02_handmade_32.txt, 02_handmade_33.txt, 02_handmade_34.txt, 02_handmade_35.txt, 02_handmade_36.txt, 02_handmade_37.txt, 02_handmade_38.txt, 02_handmade_39.txt, 02_handmade_40.txt, 02_handmade_41.txt, 02_handmade_42.txt, 02_handmade_43.txt, 02_handmade_44.txt, 02_handmade_45.txt, 03_random_00.txt, 03_random_01.txt, 03_random_02.txt, 03_random_03.txt, 03_random_04.txt, 03_random_05.txt, 03_random_06.txt, 03_random_07.txt, 03_random_08.txt, 03_random_09.txt, 03_random_10.txt, 03_random_11.txt, 03_random_12.txt, 03_random_13.txt, 03_random_14.txt, 03_random_15.txt, 03_random_16.txt, 03_random_17.txt, 03_random_18.txt, 03_random_19.txt, 03_random_20.txt, 03_random_21.txt, 03_random_22.txt, 03_random_23.txt, 03_random_24.txt, 03_random_25.txt, 03_random_26.txt, 04_from_path_subtree_01.txt, 04_from_path_subtree_02.txt, 04_from_path_subtree_03.txt, 04_from_path_subtree_04.txt, 04_from_path_subtree_05.txt, 04_from_path_subtree_06.txt, 04_from_path_subtree_07.txt, 04_from_path_subtree_08.txt, 04_from_path_subtree_09.txt, 04_from_path_subtree_10.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 3 ms 3484 KiB
01_small_00.txt AC 19 ms 3404 KiB
01_small_01.txt AC 24 ms 3492 KiB
01_small_02.txt AC 25 ms 3492 KiB
02_handmade_00.txt AC 23 ms 14760 KiB
02_handmade_01.txt AC 66 ms 62044 KiB
02_handmade_02.txt AC 54 ms 42480 KiB
02_handmade_03.txt AC 55 ms 42520 KiB
02_handmade_04.txt AC 44 ms 24868 KiB
02_handmade_05.txt AC 43 ms 24948 KiB
02_handmade_06.txt AC 90 ms 46544 KiB
02_handmade_07.txt AC 80 ms 32276 KiB
02_handmade_08.txt AC 85 ms 32232 KiB
02_handmade_09.txt AC 81 ms 32248 KiB
02_handmade_10.txt AC 26 ms 3492 KiB
02_handmade_11.txt AC 26 ms 3516 KiB
02_handmade_12.txt AC 26 ms 3496 KiB
02_handmade_13.txt AC 26 ms 3484 KiB
02_handmade_14.txt AC 26 ms 3560 KiB
02_handmade_15.txt AC 27 ms 3612 KiB
02_handmade_16.txt AC 26 ms 3524 KiB
02_handmade_17.txt AC 27 ms 3488 KiB
02_handmade_18.txt AC 81 ms 32208 KiB
02_handmade_19.txt AC 29 ms 3728 KiB
02_handmade_20.txt AC 30 ms 3680 KiB
02_handmade_21.txt AC 29 ms 3568 KiB
02_handmade_22.txt AC 29 ms 3684 KiB
02_handmade_23.txt AC 26 ms 3564 KiB
02_handmade_24.txt AC 25 ms 3560 KiB
02_handmade_25.txt AC 25 ms 3444 KiB
02_handmade_26.txt AC 25 ms 3680 KiB
02_handmade_27.txt AC 25 ms 3636 KiB
02_handmade_28.txt AC 25 ms 3568 KiB
02_handmade_29.txt AC 25 ms 3624 KiB
02_handmade_30.txt AC 25 ms 3680 KiB
02_handmade_31.txt AC 59 ms 48676 KiB
02_handmade_32.txt AC 60 ms 50388 KiB
02_handmade_33.txt AC 65 ms 62120 KiB
02_handmade_34.txt AC 64 ms 62020 KiB
02_handmade_35.txt AC 66 ms 62120 KiB
02_handmade_36.txt AC 64 ms 61984 KiB
02_handmade_37.txt AC 65 ms 62120 KiB
02_handmade_38.txt AC 65 ms 61876 KiB
02_handmade_39.txt AC 65 ms 61976 KiB
02_handmade_40.txt AC 65 ms 61760 KiB
02_handmade_41.txt AC 68 ms 59840 KiB
02_handmade_42.txt AC 66 ms 57356 KiB
02_handmade_43.txt AC 65 ms 55064 KiB
02_handmade_44.txt AC 67 ms 52784 KiB
02_handmade_45.txt AC 26 ms 18520 KiB
03_random_00.txt AC 115 ms 32184 KiB
03_random_01.txt AC 105 ms 32232 KiB
03_random_02.txt AC 122 ms 32204 KiB
03_random_03.txt AC 106 ms 32296 KiB
03_random_04.txt AC 120 ms 32284 KiB
03_random_05.txt AC 121 ms 32252 KiB
03_random_06.txt AC 116 ms 32324 KiB
03_random_07.txt AC 126 ms 32300 KiB
03_random_08.txt AC 114 ms 32264 KiB
03_random_09.txt AC 118 ms 32256 KiB
03_random_10.txt AC 25 ms 15260 KiB
03_random_11.txt AC 26 ms 15172 KiB
03_random_12.txt AC 28 ms 16628 KiB
03_random_13.txt AC 32 ms 17048 KiB
03_random_14.txt AC 27 ms 3476 KiB
03_random_15.txt AC 26 ms 3476 KiB
03_random_16.txt AC 26 ms 3476 KiB
03_random_17.txt AC 26 ms 3488 KiB
03_random_18.txt AC 27 ms 3512 KiB
03_random_19.txt AC 77 ms 40560 KiB
03_random_20.txt AC 78 ms 40456 KiB
03_random_21.txt AC 76 ms 40616 KiB
03_random_22.txt AC 91 ms 44476 KiB
03_random_23.txt AC 88 ms 44384 KiB
03_random_24.txt AC 92 ms 44484 KiB
03_random_25.txt AC 85 ms 44464 KiB
03_random_26.txt AC 81 ms 44388 KiB
04_from_path_subtree_01.txt AC 75 ms 47496 KiB
04_from_path_subtree_02.txt AC 73 ms 52560 KiB
04_from_path_subtree_03.txt AC 68 ms 56100 KiB
04_from_path_subtree_04.txt AC 69 ms 49736 KiB
04_from_path_subtree_05.txt AC 67 ms 54228 KiB
04_from_path_subtree_06.txt AC 74 ms 59444 KiB
04_from_path_subtree_07.txt AC 69 ms 52012 KiB
04_from_path_subtree_08.txt AC 67 ms 55632 KiB
04_from_path_subtree_09.txt AC 69 ms 48372 KiB
04_from_path_subtree_10.txt AC 69 ms 59728 KiB