Submission #50093

# Submission time Handle Problem Language Result Execution time Memory
50093 2018-06-07T13:56:53 Z luciocf Birokracija (COCI18_birokracija) C++14
100 / 100
259 ms 32736 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++)
                     ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5176 KB Output is correct
2 Correct 7 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5412 KB Output is correct
2 Correct 6 ms 5412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5464 KB Output is correct
2 Correct 7 ms 5464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 6384 KB Output is correct
2 Correct 23 ms 6752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 8944 KB Output is correct
2 Correct 53 ms 9472 KB Output is correct
3 Correct 57 ms 11052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 250 ms 16272 KB Output is correct
2 Correct 170 ms 19356 KB Output is correct
3 Correct 170 ms 32736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 259 ms 32736 KB Output is correct
2 Correct 207 ms 32736 KB Output is correct
3 Correct 159 ms 32736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 215 ms 32736 KB Output is correct
2 Correct 230 ms 32736 KB Output is correct
3 Correct 160 ms 32736 KB Output is correct