JZ High School OJ 1388. Cycling

Description

  Tsuiheng hold a bike race, Tsuiheng there are N junction (numbered 1 to N), M and another two-way side connected. Here are a few definitions:
  • path: a series of edge, the latter to meet a starting point for the side of the front end of one side;
  • simple path: at most once after each intersection of paths;
  • ring: start and end in the same simple path junction.
  Ensure that at least one path is connected between each pair of intersections, in addition to also meet most of each side appears only in one ring.
  Your task is to find the longest path to the following two conditions:
  • the starting point can be at any intersection, but must end at the intersection of No. 1;
  • path may be through the same intersection several times, but it will only go through each edge once.
 

Input

  The first line contains two integers N and M (2 <= N <= 10000,1 <= M <= 2N-2), represents the number of junctions and the number of edges.
  Next M rows, each row comprising two different integers A and B (1 <= A, B <= N), represents a direct connection exists between the edges A and B, only a maximum of two intersections between a straight edge connected.

Output

  Output path length of the longest match.

 

Sample Input

Input 1:
4 3
1 2
1 3
2 4

Input 2:
6 6
1 2
1 3
2 4
3 4
3 5
5 6

Input 3:
5 6
1 2
2 3
3 4
4 5
5 3
3 1

Sample Output

Output 1:
2

Output 2:
5

Output 3: 6
Program tracks;
uses Classes;
const MAXN = 10000;
const PUT = 0;
const DJIR = 1;
var
   n, m : longint;
   i : longint;
   a, b : longint;
   adj : array[1..MAXN] of TList;
   nPrstena : longint;
   broj : array[1..MAXN] of longint;
   prsten : array[1..MAXN] of TList;
   stack : array[1..MAXN] of longint;
   stackTop : longint;
   prsteni : array[1..MAXN] of TList;
   mostovi : array[1..MAXN] of TList;
   traversalTime : longint;
   discover : array[1..MAXN] of longint;
   lowlink : array[1..MAXN] of longint;
   memo : array[1..MAXN,0..1] of longint;
procedure dfs( u, dad : longint );
var
   i, v : longint;
begin
   traversalTime := traversalTime + 1;
   discover[u] := traversalTime;
   lowlink[u] := discover[u];
   stackTop := stackTop + 1;
   stack[stackTop] := u;
   for i := 0 to adj[u].Count-1 do begin
      v := longint(adj[u].Items[i]);
      if v = dad then continue;
      if discover[v] <> 0 then begin
         if discover[v] < lowlink[u] then lowlink[u] := discover[v];
      end else begin
         dfs( v, u );
         if lowlink[v] < discover[u] then begin
            if lowlink[v] < lowlink[u] then lowlink[u] := lowlink[v];
         end else if lowlink[v] = discover[u] then begin
            nPrstena := nPrstena + 1;
            prsten[nPrstena] := TList.Create;
            while stack[stackTop] <> v do begin
               prsten[nPrstena].Add( Pointer( broj[stack[stackTop]] ) );
               stackTop := stackTop-1;
            end;
            prsten[nPrstena].Add( Pointer( broj[stack[stackTop]] ) );
            stackTop := stackTop-1;
            rings [u] .add (Pointer (code [nPrstena]));
         Over  presence  Begin
            bridges [in] .add (Pointer (code [Stack [stackTop]]));
            stackTop := stackTop-1;
         end;
      end;
   end;
end;
function max( a, b : longint ) : longint;
begin
   if a > b then max := a else max := b;
end;
function rec( X, stoRacunam : longint ) : longint;
var
   profit : longint;
   P : longint;
   best, Specialization 1, Specialization 2, cycle: integer;
   i, j : longint;
begin
   if memo[X][stoRacunam] >= 0 then begin
      rec := memo[X][stoRacunam];
   end else begin
      memo[X][stoRacunam] := 0;
      profit: = 0 ; (* How maximum benefit can if you do not have to return to X *) 
      for i = 0  to bridges [X] .Count- 1  to  begin  (* for all the bridges that come out of X *) 
         profit: = max (profit 1 + rec (integer (bridges [X] .Items [i]), PATH));
      end ;
      for i: = 0  to rings [X] .Count- 1  to  begin  (* for all the rings of more X *) 
         W: = longint(prsteni[X].Items[i]);
         best := 0;
         smjer1: = 1 ; smjer2: = 1 ;
         ciklus := prsten[P].Count + 1;
         for j := 0 to prsten[P].Count-1 do begin
            best := max( best, smjer1 + rec( longint(prsten[P].Items[j]), PUT ) );
            smjer1 := smjer1 + 1 + rec( longint(prsten[P].Items[j]), DJIR );
            best := max( best, smjer2 + rec( longint(prsten[P].Items[prsten[P].Count-j-1]), PUT ) );
            smjer2 := smjer2 + 1 + rec( longint(prsten[P].Items[prsten[P].Count-j-1]), DJIR );
            Cycle: = Cycle + rec (integer (ring [P] .Items [j]), with a promenade);
         end ;
         memo[X][stoRacunam] := memo[X][stoRacunam] + ciklus;
         profit := max( profit, best - ciklus );
      end;
      if stoRacunam = PUT then memo[X][stoRacunam] := memo[X][stoRacunam] + profit;
      rec := memo[X][stoRacunam];
   end;
end;
begin
   readln( n, m );
   for i := 1 to n do begin
      adj[i] := TList.Create;
      mostovi[i] := Tlist.Create;
      prsteni[i] := Tlist.Create;
      code [i]: = i;
   end ;
   for i: = 1  to m to  begin
      readln( a, b );
      adj[a].Add( Pointer( broj[b] ) );
      adj[b].Add( Pointer( broj[a] ) );
   end;
   traversalTime := 0;
   for i := 1 to n do discover[i] := 0;
   stackTop := 0;
   nPrstena: = 0 ;
   dfs( 1, 0 );
   for i := 1 to n do begin
      memo[i,0] := -1;
      memo[i,1] := -1;
   end;
   writeln( rec( 1, 0 ) );
end.

Guess you like

Origin www.cnblogs.com/anbujingying/p/11316554.html