답안 #83121

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
83121 2018-11-05T13:14:26 Z luciocf Birokracija (COCI18_birokracija) C++14
100 / 100
222 ms 34432 KB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2e5+10;

typedef long long ll;

int n;
ll ans[MAXN], size[MAXN];
vector<int> grafo[MAXN];

void get_size(int u, int p)
{
    for (int i = 0; i < grafo[u].size(); i++)
    {
        int v = grafo[u][i];
        if (v == p) continue;
        get_size(v, u);
        size[u] += size[v];
    }
    size[u]++;
}

void DFS(int u, int p)
{
    for (int i = 0; i < grafo[u].size(); i++)
    {
        int v = grafo[u][i];
        if (v == p) continue;

        DFS(v, u);
        ans[u] += (size[v]+ans[v]);
    }
    ans[u]++;
}

int main(void)
{
    cin >> n;

    for (int i = 2; i <= n; i++)
    {
        int x;
        cin >> x;
        grafo[x].push_back(i);
        grafo[i].push_back(x);
    }
    get_size(1, -1);
    DFS(1, -1);

    cout << ans[1];
    for (int i = 2; i <= n; i++)
        cout << " " << ans[i];
    cout << "\n";
}

Compilation message

birokracija.cpp: In function 'void get_size(int, int)':
birokracija.cpp:15:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < grafo[u].size(); i++)
                     ~~^~~~~~~~~~~~~~~~~
birokracija.cpp: In function 'void DFS(int, int)':
birokracija.cpp:27:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < grafo[u].size(); i++)
                     ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5368 KB Output is correct
2 Correct 6 ms 5368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5368 KB Output is correct
2 Correct 6 ms 5388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5396 KB Output is correct
2 Correct 6 ms 5460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 6492 KB Output is correct
2 Correct 22 ms 6988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 9700 KB Output is correct
2 Correct 52 ms 10184 KB Output is correct
3 Correct 54 ms 11740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 199 ms 18244 KB Output is correct
2 Correct 153 ms 21144 KB Output is correct
3 Correct 153 ms 34432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 34432 KB Output is correct
2 Correct 162 ms 34432 KB Output is correct
3 Correct 154 ms 34432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 222 ms 34432 KB Output is correct
2 Correct 141 ms 34432 KB Output is correct
3 Correct 143 ms 34432 KB Output is correct