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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
using namespace std;
const int MAXN = 2 * 1e5 + 5;
int par[MAXN];
vector<int> adjList[MAXN];
int subtree[MAXN], sums[MAXN];
void dfs(int src) {
for (auto i : adjList[src]) {
dfs(i);
subtree[src] += subtree[i];
sums[src] += subtree[i] + sums[i];
}
}
int main() {
int n;
cin >> n;
par[1] = 1;
subtree[1] = 0;
sums[1] = 1;
for (int i = 2; i <= n; i++) {
int temp;
cin >> temp;
par[i] = temp;
adjList[temp].push_back(i);
subtree[i] = 1;
sums[i] = 1;
}
dfs(1);
for (int i = 1; i <= n; i++) {
cout << sums[i];
if (i != n) {
cout << " ";
}
}
cout << endl;
}
# | 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... |