题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315
解题报告:
#include <stdio.h> #include <string.h> struct Point { int pr; int vis; } points[500000+5]; int ans[500000+5]; int n; int main() { int t; scanf("%d",&t); while(t--) { scanf("%d",&n); int sum=0; memset(points,0,sizeof(points)); for(int i=2; i<=n; i++) scanf("%d",&points[i].pr); for(int i=n; i>1; i--) ///从下往上扫 { if(points[i].vis==0&&points[points[i].pr].vis==0) { points[i].vis=1; points[points[i].pr].vis=1; ans[sum++]=i; } } printf("%d ",sum*1000); for(int i=sum-1; i>=0; i--) { if(i==sum-1) printf("%d",ans[i]); else printf(" %d",ans[i]); } puts(""); } return 0; }