Cod sursa(job #24201)

Utilizator vladcyb1Vlad Berteanu vladcyb1 Data 1 martie 2007 21:28:25
Problema Triplete Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.26 kb

  const
        FIN = 'triplete.in';
        FOUT = 'triplete.out';
        NMAX = 4096;

  type
      ref = ^cell;
      cell = record inf : longint; urm : ref; end;

  var
     V : array[ 1..NMAX ] of ref;
     nivel, sel : array[ 1..NMAX ] of longint;
     N, M, ans : longint;
     f, g : text;


  procedure add( var p : ref; inf : longint );
   var q : ref;
    begin
     new( q ); q^.inf := inf; q^.urm := p; p := q;
    end;

  procedure read_data;
   var i, x, y : longint;
   begin
    assign( f, FIN ); reset( f );
    readln( f, N, M );
    for i := 1 to M do
     begin
      readln( f, x, y );
      add( v[x], y );
      add( v[y], x );
     end;
    close( f );
   end;

  procedure DFS( nod, niv : longint );
   var tmp : ref;
    begin
      nivel[ nod ] := niv; sel[ nod ] := 1;
      tmp := v[ nod ];
      while tmp <> nil do
        begin
         if sel[ tmp^.inf ] = 0 then DFS( tmp^.inf, niv + 1 ) else
         if abs( nivel[tmp^.inf] - niv ) = 2 then inc( ans );
         tmp := tmp^.urm;
        end;
    end;

   procedure save;
    begin
     assign( g, FOUT ); rewrite( g );
     writeln( g, ans ); close( g );
    end;

   begin
    read_data; ans := 0;
    DFS( 1, 0 );
    save;
   end.