10.21模拟赛 猴猴吃苹果(排序)
作者:互联网
#include<cstring> #include<queue> #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<vector> using namespace std; const int maxm=100007; int n,k; int pre[maxm],last[maxm],other[maxm],l; bool vis[maxm]; int tot; int f[maxm]; struct node { int id,dep; }a[maxm]; struct srt { int id,ans; }t[maxm]; bool cmp(node a,node b) { if(a.dep==b.dep) return a.id<b.id; return a.dep>b.dep; } bool cmp1(srt a,srt b) { if(a.ans==b.ans) return a.id<b.id; return a.ans>b.ans; } void add(int x,int y) { l++; pre[l]=last[x]; last[x]=l; other[l]=y; } void dfs(int x) { for(int p=last[x];p;p=pre[p]) { int v=other[p]; if(v==f[x]) continue; f[v]=x; a[v].dep=a[x].dep+1; a[v].id=v; dfs(v); } } int work(int x) { int tmp=0; while(!vis[x]) { vis[x]=1; x=f[x]; tmp++; } return tmp; } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n-1;i++) { int x; scanf("%d",&x); add(i,x); add(x,i); } a[k].id=k; a[k].dep=1; dfs(k); sort(a,a+n,cmp); memset(vis,0,sizeof(vis)); vis[k]=1; printf("%d\n",k); for(int i=0;i<n;i++) { if(vis[a[i].id]) continue; else { t[++tot].ans=work(a[i].id); t[tot].id=a[i].id; } } sort(t+1,t+tot+1,cmp1); for(int i=1;i<=tot;i++) { printf("%d\n",t[i].id); } return 0; }
标签:10.21,include,last,int,猴猴,ans,dep,maxm,排序 来源: https://www.cnblogs.com/lihan123/p/11716568.html