# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
239564 | MrRobot_28 | Birokracija (COCI18_birokracija) | C++17 | 201 ms | 33192 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector <vector <int> > g;
vector <int> ans;
vector <int> sum, _size, h;
void dfs(int v, int p = -1)
{
_size[v] = 1;
sum[v] = 0;
for(int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if(to != p)
{
h[to] = h[v] + 1;
dfs(to, v);
_size[v] += _size[to];
sum[v] += sum[to];
}
}
sum[v] += h[v] + 1;
ans[v] = sum[v] - _size[v] * h[v];
}
signed main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
int n;
cin >> n;
g.resize(n);
ans.resize(n);
sum.resize(n);
_size.resize(n);
h.resize(n);
for(int i = 1; i < n; i++)
{
int p;
cin >> p;
p--;
g[p].push_back(i);
}
dfs(0, -1);
for(int i = 0; i < n; i++)
{
cout << ans[i] << " ";
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |