Cod sursa(job #28132)

Utilizator izso88istvan zsolt izso88 Data 7 martie 2007 15:17:00
Problema Triplete Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.37 kb
const fff=4100;
type pe=^te;
     te=record
              ert:integer;
              kov:pe;
              end;

var  ad:array[1..fff] of pe;
     men,kkk,hasz:array[1..fff] of longint;

     t:text;
     p,y:pe;
     i,j,k,n,m,mma:longint;
     hany:longint;

procedure df(hol,kezd,szint:integer);
          var e:pe;
          begin
          if (szint=3) and (kezd=hol) then inc(men[kezd])
          else
          if ((szint>0)and (szint<3)) and (kezd=hol) then begin end else
          if szint<3 then begin
                          e:=ad[hol];
                          while e^.kov<>nil do begin
                          e:=e^.kov;
                          if hasz[e^.ert]=1 then df(e^.ert,kezd,szint+1);
                          end;
                          end;
          end;

begin
     assign(t,'triplete.in');
     reset(T);
      read(t,n,m);
      hany:=0;
      mma:=0;
     for i:=1 to n do kkk[i]:=0;

     for i:=1 to n do begin
                      new(ad[i]);
                      ad[i]^.ert:=0;
                      ad[i]^.kov:=nil;
                      end;
     for i:=1 to m do begin
                      read(t,j,k);
                      p:=ad[j];
                      while p^.kov<>nil do p:=p^.kov;
                      new(y);
                      y^.ert:=k;
                      Y^.kov:=nil;
                      p^.kov:=y;
                      inc(kkk[j]);

                      p:=ad[k];
                      while p^.kov<>nil do p:=p^.kov;
                      new(y);
                      y^.ert:=j;
                      Y^.kov:=nil;
                      p^.kov:=y;
                      inc(kkk[k]);
                      if kkk[i]>mma then mma:=kkk[i];
                      if kkk[j]>mma then mma:=kkk[j];

                      end;

     close(T);
     for i:=1 to n do if ad[i]^.kov<>nil then if ad[i]^.kov^.kov=nil then
        ad[i]^.kov:=nil;

     for i:=1 to n do men[i]:=0;
     for i:=1 to n do hasz[i]:=1;

     for k:=mma downto 2 do
     for i:=1 to n do if kkk[i]=k then
                      begin
                      df(i,i,0);
                      if men[i]>0 then hasz[i]:=0;
                      end;

     hany:=0;
     for i:=1 to n do hany:=hany+(men[i]);

     assign(t,'triplete.out');
     rewrite(T);
       writeln(t,hany div 2);
     close(T);

end.