子树和

Luogu
IDLGB3861
Time1000ms
Memory128MB
DifficultyP2
给你一棵 $n$ 个结点的树,树的根结点是 $1$。 每个结点的权值都是 $1$,你需要对每个结点 $i$ 求出 $i$ 的子树和,也就是子树中有多少个结点。 ## Input 第一行一个数 $n$ 表示树的结点数。 之后 $n-1$ 行,第 $i$ 行输入一个数 $f_{i+1}$ 表示 $i+1$ 点的父亲是 $f_{i+1}$,保证 $f_{i+1}<i+1$。 ## Output 输出 $n$ 行,第 $i$ 行一个数表示 $i$ 点的子树和。 [samples] ## Note 对于 $100\%$ 的数据,满足 $1\le n\le 1000$。
Samples
Input #1
5
1
2
3
3
Output #1
5
4
3
1
1
API Response (JSON)
{
  "problem": {
    "name": "子树和",
    "description": {
      "content": "给你一棵 $n$ 个结点的树,树的根结点是 $1$。 每个结点的权值都是 $1$,你需要对每个结点 $i$ 求出 $i$ 的子树和,也就是子树中有多少个结点。",
      "description_type": "Markdown"
    },
    "platform": "Luogu",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 131072
    },
    "difficulty": {
      "LuoguStyle": "P2"
    },
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "LGB3861"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "给你一棵 $n$ 个结点的树,树的根结点是 $1$。\n\n每个结点的权值都是 $1$,你需要对每个结点 $i$ 求出 $i$ 的子树和,也就是子树中有多少个结点。\n\n## Input\n\n第一行一个数 $n$ 表示树的结点数。\n\n之后 $n-1$ 行,第 $i$ 行输入一个数 $f_{i+1}$ 表示 $i+1$ 点的父亲是 $f_{i+1}$,保证 $f_{i+1}<i+1$。\n\n## Output...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments