fork download
  1. program cupido;
  2.  
  3. const
  4. MAXN = 100000;
  5. type elenco=Array[0..MAXN-1] of LongInt;
  6. var
  7. K, N, i ,j, amico,pos : LongInt;
  8. A, B, C, ID : elenco;
  9.  
  10. Procedure scambia (var a,b: LongInt);
  11. var x:LongInt;
  12. begin
  13. x:=a;
  14. a:=b;
  15. b:=x;
  16. end;
  17. Procedure ordinamento (estremoi,estremos: LongInt; var v : elenco;var u : elenco;var w : elenco;var z : elenco; ordinato:boolean);
  18. var inf, sup, medio:LongInt;
  19. pivot :LongInt;
  20. begin
  21. inf:=estremoi;
  22. sup:=estremos;
  23. medio:= (estremoi+estremos) div 2;
  24. pivot:=v[medio];
  25. repeat
  26. if (ordinato) then
  27. begin
  28. while (v[inf]<pivot) do inf:=inf+1;
  29. while (v[sup]>pivot) do sup:=sup-1;
  30. end;
  31. if inf<=sup then
  32. begin
  33. scambia(v[inf],v[sup]);
  34. scambia(u[inf],u[sup]);
  35. scambia(w[inf],w[sup]);
  36. scambia(z[inf],z[sup]);
  37. inf:=inf+1;
  38. sup:=sup-1;
  39. end;
  40. until inf>sup;
  41. if (estremoi<sup) then ordinamento(estremoi,sup,v,u,w,z,ordinato);
  42. if (inf<estremos) then ordinamento(inf,estremos,v,u,w,z,ordinato);
  43. end;
  44.  
  45. Procedure ricerca (var t:elenco;amicizia:Longint);
  46. var i,m,inizio,fine: longint;
  47.  
  48. begin
  49. inizio:=1; fine:=N;
  50. repeat
  51. m:=(inizio + fine) div 2;
  52. if t[m]<=amicizia then inizio:=m+1;
  53. if t[m]>amicizia then fine:=m-1;
  54. until inizio>fine;
  55.  
  56. if (t[m]<=amicizia) then pos:=m
  57. else pos:=m-1;
  58.  
  59. end;
  60. begin
  61. {
  62.   uncomment the two following lines if you want to read/write from files
  63.   assign(input, 'input.txt'); reset(input);
  64.   assign(output, 'output.txt'); rewrite(output);
  65. }
  66.  
  67. ReadLn(N);
  68.  
  69. for i:=1 to N do
  70. ReadLn(ID[i], A[i], B[i], C[i]);
  71.  
  72. K := 0;
  73.  
  74. ordinamento (1,N,ID,A,B,C, true);
  75. for i:=1 to N do
  76. begin
  77. j:=1;
  78. while j<=3 do
  79. begin
  80. if j=1 then if (A[i]<>-1) and (A[i]>ID[i]) then
  81. begin
  82. amico:=A[i];
  83. ricerca (ID,amico);
  84. if (A[pos]=ID[i]) or (B[pos]=ID[i]) or (C[pos]=ID[i]) then K:=k+1;
  85. end;
  86.  
  87. if j=2 then if (B[i]<>-1) and (B[i]>ID[i]) then
  88. begin
  89. amico:=B[i];
  90. ricerca (ID,amico);
  91. if (A[pos]=ID[i]) or (B[pos]=ID[i]) or (C[pos]=ID[i]) then K:=k+1;
  92. end;
  93. if j=3 then if (C[i]<>-1) and (C[i]>ID[i]) then
  94. begin
  95. amico:=C[i];
  96. ricerca (ID,amico);
  97. if (A[pos]=ID[i]) or (B[pos]=ID[i]) or (C[pos]=ID[i]) then K:=k+1;
  98. end;
  99. j:=j+1;
  100. end;
  101. end;
  102.  
  103. WriteLn(K);
  104. end.
  105.  
Success #stdin #stdout 0.01s 5288KB
stdin
2
2 -1 1 -1
1 -1 -1 2

stdout
1