1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <cstdio> #include <cstring> #include <queue>
using namespace std;
int fa[1010]; int son[1010][1010]; bool flag[1010];
struct Point{ trueint x,depth; true truePoint(int x,int depth):x(x),depth(depth) {}
trueconst bool operator <(const Point& tmp)const{ truetruereturn depth<tmp.depth; true} };
priority_queue<Point> q;
void dfs(int x,int depth) { trueq.push(Point(x,depth)); truefor (int i = 1; i<=son[x][0]; i++) truetruedfs(son[x][i],depth+1); truereturn; }
void fill(int x,int dis) { trueflag[x] = false; trueif (dis==0) truetruereturn;
trueif (x!=1) truetruefill(fa[x],dis-1); truefor (int i = 1; i<=son[x][0]; i++) truetruefill(son[x][i],dis-1); truereturn; }
int main() { truememset(flag,true,sizeof(flag)); trueint n; truescanf("%d",&n);
truefor (int i = 2; i<=n; i++) true{ truetruescanf("%d",&fa[i]); truetrueson[ fa[i] ][ ++ son[ fa[i] ][0] ] = i; true}
truedfs(1,1);
trueint ans = 0; truewhile (!q.empty()) true{ truetruePoint p = q.top(); q.pop(); truetrueif (flag[p.x]) truetrue{ truetruetrueans++; truetruetruefill(fa[fa[p.x]],2); truetrue} true}
trueprintf("%d",ans); }
|