답안 #237505

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
237505 2020-06-07T06:29:54 Z kartel Birokracija (COCI18_birokracija) C++14
100 / 100
121 ms 23728 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
#define N +200500
#define MaxS N * N
#define M ll(1e9 + 7)
#define sz(x) (int)x.size()
//#define re return
#define oo ll(1e18)
#define el '\n'
#define pii pair <int, int>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <int, null_type, less_equal <int> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef long double ld;

ll d[N], siz[N], f[N], i, n, v;
vector <ll> g[N];

void dfs(ll v, ll pr)
{
    d[v] = d[pr] + 1;
    f[v] = 0;
    siz[v] = 1;

    for (auto u : g[v])
    {
        if (u == pr) continue;

        dfs(u, v);

        siz[v] += siz[u];
        f[v] += f[u];
    }

    f[v] += siz[v];
}

int main()
{
    srand(time(0));
    ios_base::sync_with_stdio(0);
    iostream::sync_with_stdio(0);
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

//    in("input.txt");
//    out("output.txt");

    cin >> n;
    for (i = 2; i <= n; i++)
    {
        cin >> v;
        g[v].pb(i);
    }

    for (i = 1; i <= n; i++) sort(g[i].begin(), g[i].end());

    d[0] = 0;
    dfs(1, 0);

    for (i = 1; i <= n; i++) cout << f[i] << " ";
}

//  x ^ 2 + y ^ 2 = 1
//  x * a_i + y * b_i
//  a_i = -b_i * tan(alpha)
//  a_i / -b_i = tan(alpha)
//  alpha = atan(a_i / (-b_i))
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 4992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 6016 KB Output is correct
2 Correct 16 ms 6400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 8192 KB Output is correct
2 Correct 31 ms 8448 KB Output is correct
3 Correct 30 ms 9088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 13960 KB Output is correct
2 Correct 74 ms 15608 KB Output is correct
3 Correct 71 ms 23728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 13952 KB Output is correct
2 Correct 77 ms 14840 KB Output is correct
3 Correct 82 ms 16248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 13688 KB Output is correct
2 Correct 73 ms 14980 KB Output is correct
3 Correct 79 ms 17400 KB Output is correct