记录编号 338583 评测结果 AAAAAAAAAA
题目名称 [NOIP 2015]信息传递 最终得分 100
用户昵称 GravatarZwoi_只会打表抄代码的蒟蒻 是否通过 通过
代码语言 C 运行时间 0.387 s
提交时间 2016-11-05 12:00:11 内存使用 3.69 MiB
显示代码纯文本
  1. #include <stdio.h>
  2. int tell[200010],book[200010],start[200010],tail,a[200010],x,min,pre,t,j,i,n,last[200010];
  3. int main()
  4. {
  5. freopen("2015message.in","r",stdin);
  6. freopen("2015message.out","w",stdout);
  7. scanf("%d",&n);
  8. for(i=1;i<=n;i++)
  9. {
  10. scanf("%d",&tell[i]);
  11. book[tell[i]]=1;
  12. }
  13. tail=0;
  14. min=0x7f7f7f7f;
  15. for(i=1;i<=n;i++)
  16. if(!book[i])
  17. {
  18. tail++;
  19. start[tail]=i;
  20. }
  21. memset(book,0,sizeof(book));
  22. for(i=1;i<=tail;i++)
  23. {
  24. t=tell[start[i]];
  25. a[start[i]]=x=1;
  26. while(book[t]==0)
  27. {
  28. book[t]=i;
  29. x++;
  30. a[t]=pre=x;
  31. t=tell[t];
  32. }
  33. if(pre-a[t]+1<min&&book[t]==i)
  34. min=pre-a[t]+1;
  35. }
  36. j=0;
  37. for(i=1;i<=n;i++)
  38. {
  39. if(a[i]==0)
  40. last[++j]=i;
  41. }
  42. memset(book,0,sizeof(book));
  43. for(i=1;i<=j;i++)
  44. {
  45. if(!book[last[i]])
  46. {
  47. book[last[i]]=1;
  48. t=tell[last[i]];
  49. x=1;
  50. while(!book[t])
  51. {
  52. book[t]=1;
  53. x++;
  54. t=tell[t];
  55. }
  56. if(x<min)
  57. min=x;
  58. }
  59. }
  60. printf("%d",min);
  61. return 0;
  62. }