catch.dpr 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. program catch;
  2. const maxn=1000;
  3. var y,next:array[1..maxn shl 1]of longint;
  4. from,fa,tree,tot,tod:array[1..maxn]of longint;
  5. tottime:array[0..10]of longint;
  6. time:longint;
  7. n,ans,len:longint;
  8. procedure add(a,b:longint);
  9. begin
  10. inc(len);
  11. y[len]:=b;
  12. next[len]:=from[a];
  13. from[a]:=len;
  14. end;
  15. procedure inputint;
  16. var i,a,b:longint;
  17. begin
  18. readln(n);
  19. fillchar(from,sizeof(from),0);
  20. len:=0;
  21. for i:=1 to n-1 do
  22. begin
  23. readln(a,b);
  24. add(a,b);
  25. add(b,a);
  26. end;
  27. end;
  28. procedure maketree(root:longint);
  29. var i,x:longint;
  30. begin
  31. fillchar(tree,sizeof(tree),0);
  32. tree[1]:=root;
  33. len:=1;
  34. fillchar(fa,sizeof(fa),$FF);
  35. for x:=1 to n do
  36. begin
  37. i:=from[tree[x]];
  38. while i<>0 do
  39. begin
  40. if y[i]<>fa[tree[x]] then
  41. begin
  42. inc(len);
  43. tree[len]:=y[i];
  44. fa[y[i]]:=tree[x];
  45. end;
  46. i:=next[i];
  47. end;
  48. end;
  49. end;
  50. function fmax(x,y:longint):longint;
  51. begin
  52. if x>y then fmax:=x
  53. else fmax:=y;
  54. end;
  55. procedure workans(root:longint);
  56. var i,x:longint;
  57. begin
  58. fillchar(tot,sizeof(tot),0);
  59. fillchar(tod,sizeof(tod),0);
  60. for i:=n downto 2 do
  61. begin
  62. x:=tree[i];
  63. tot[x]:=fmax(tot[x],tod[x]+1);
  64. if tot[x]>tot[fa[x]] then
  65. begin
  66. tod[fa[x]]:=tot[fa[x]];
  67. tot[fa[x]]:=tot[x];
  68. end
  69. else if tot[x]>tod[fa[x]] then
  70. tod[fa[x]]:=tot[x];
  71. end;
  72. tot[root]:=fmax(tot[root],tod[root]+1);
  73. if tot[root]<ans then ans:=tot[root];
  74. end;
  75. procedure work;
  76. var root:longint;
  77. begin
  78. ans:=maxlongint;
  79. for root:=1 to n do
  80. begin
  81. maketree(root);
  82. workans(root);
  83. end;
  84. end;
  85. procedure outputint;
  86. begin
  87. inc(tottime[ans]);
  88. end;
  89. procedure gen;
  90. var i:longint;
  91. begin
  92. assign(output,'input.in');rewrite(output);
  93. writeln(1000);
  94. for i:=2 to 1000 do
  95. writeln(i,' ',random(i-1)+1);
  96. close(output);
  97. end;
  98. begin
  99. randomize;
  100. fillchar(tottime,sizeof(tottime),0);
  101. for time:=1 to 1000 do
  102. begin
  103. gen;
  104. assign(input,'input.in');reset(input);
  105. inputint;
  106. work;
  107. outputint;
  108. close(input);
  109. end;
  110. assign(output,'output.out');rewrite(output);
  111. for time:=1 to 10 do
  112. writeln(time,' ',tottime[time]);
  113. close(output);
  114. end.