이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define sz(x) int(x.size())
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll f[N];
int n, d[N], pr[N];
vector <int> g[N];
set <int> se[N];
void dfs(int v, int pred = -1) {
d[v] = 1;
for (int i = 0; i < sz(g[v]); ++i) {
int to = g[v][i];
if (to == pred)
continue;
dfs(to, v);
f[v] += f[to];
d[v] += d[to];
}
f[v] += d[v];
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i)
pr[i] = i;
for (int i = 1; i < n; ++i) {
int x;
cin >> x;
x--;
g[i].pb(x);
g[x].pb(i);
}
dfs(0);
for (int i = 0; i < n; ++i)
cout << f[i] << ' ';
}
| # | 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... |