Cod sursa(job #28142)

Utilizator izso88istvan zsolt izso88 Data 7 martie 2007 15:28:22
Problema Triplete Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.81 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
                            if kkk[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]);

                      end;

     close(T);
{     mma:=kkk[1];
     for i:=2 to n do if kkk[i]>mma then mma:=kkk[i];}
     for i:=1 to n do if kkk[i]=1 then hasz[i]:=0;

     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 }
                        if hasz[i]=1 then
                      begin
                      df(i,i,0);
                      {if men[i]>0 then begin}
                                       hasz[i]:=0;
                                       p:=ad[i];
                                       while p^.kov<>nil do
                                       begin
                                       p:=p^.kov;
                                       kkk[p^.ert]:=kkk[P^.ert]-1;
                                      end;
{                      end;}
                      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.