ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1077. Travelling Tours

Why I got wrong answer? Please help me!
Posted by Grebnov Ilya[ISPU] 11 Apr 2003 21:45
CONST
  NMax = 250;
VAR
  Route : ARRAY[1..NMax, 1..NMax] OF LongInt;
  Used : ARRAY[1..NMax] OF Boolean;
  Path : ARRAY[0..NMax] OF Integer;
  N, M, I, T, H, Count : LongInt;
  Found : Boolean;

  PROCEDURE DSF(Start, C : LongInt);
  VAR
    I : LongInt;
  BEGIN
    Used[C] := True;
    IF (Route[C, Start] = 2) THEN
      BEGIN
        Found := True;
        Used[C] := False;
        Exit;
      END;
    FOR I := 1 TO N DO
      IF (NOT Used[I]) AND (Route[C, I] = 2) THEN
        BEGIN
          Route[C, I] := 0;
          Route[I, C] := 0;
          DSF(Start, I);
          Route[C, I] := 2;
          Route[I, C] := 2;
          IF Found THEN Break;
        END;
    IF (NOT Found) THEN
      IF (Route[C, Start] = 1) THEN
        BEGIN
          Route[Start, C] := 2;
          Route[C, Start] := 2;
          Found := True;
        END;
    IF NOT Found THEN
      FOR I := 1 TO N DO
        IF (NOT Used[I]) AND (Route[C, I] = 1) THEN
          BEGIN
            Route[C, I] := 0;
            Route[I, C] := 0;
            DSF(Start, I);
            IF Found THEN
              BEGIN
                Route[C, I] := 2;
                Route[I, C] := 2;
                Break;
              END
            ELSE
              BEGIN
                Route[C, I] := 1;
                Route[I, C] := 1;
              END;
          END;
    Used[C] := False;
  END;

  PROCEDURE DSF2(Start, C, Size : LongInt);
  VAR
    I : LongInt;
  BEGIN
    Used[C] := True;
    IF (Route[C, Start] = 1) THEN
      BEGIN
        Found := True;
        Used[C] := False;
        Path[Size] := C;
        Path[0] := Size;
        Exit;
      END;
    FOR I := 1 TO N DO
      IF (NOT Used[I]) AND (Route[C, I] = 1) THEN
        BEGIN
          Route[C, I] := 0;
          Route[I, C] := 0;
          DSF2(Start, I, Size+1);
          Route[C, I] := 1;
          Route[I, C] := 1;
          IF Found THEN
            BEGIN
              Path[Size] := C;
              Break;
            END;
        END;
    IF (NOT Found) THEN
      IF (Route[C, Start] = 2) THEN
        BEGIN
          Route[Start, C] := 1;
          Route[C, Start] := 1;
          Path[Size] := C;
          Path[0] := Size;
          Found := True;
        END;
    IF NOT Found THEN
      FOR I := 1 TO N DO
        IF (NOT Used[I]) AND (Route[C, I] = 2) THEN
          BEGIN
            Route[C, I] := 0;
            Route[I, C] := 0;
            DSF2(Start, I, Size+1);
            IF Found THEN
              BEGIN
                Route[C, I] := 1;
                Route[I, C] := 1;
                Path[Size] := C;
                Break;
              END
            ELSE
              BEGIN
                Route[C, I] := 2;
                Route[I, C] := 2;
              END;
          END;
    Used[C] := False;
  END;

BEGIN
  FillChar(Route, SizeOf(Route), 0);
  FillChar(Used, SizeOf(Used), False);
  ReadLn(N, M);
  FOR I := 1 TO M DO
    BEGIN
      ReadLn(T, H);
      Route[T, H] := 1;
      Route[H, T] := 1;
    END;
  Count := 0;
  FOR T := 1 TO N DO
    FOR
    H := 1 TO N DO
      IF Route[T, H] = 1 THEN
        BEGIN
          Used[T] := True;
          Route[T, H] := 0;
          Route[H, T] := 0;
          Found := False;
          DSF(T, H);
          IF Found THEN
            BEGIN
              Route[T, H] := 2;
              Route[H, T] := 2;
              Inc(Count);
            END
          ELSE
            BEGIN
              Route[T, H] := 1;
              Route[H, T] := 1;
            END;
          Used[T] := False;
        END;
  WriteLn(Count);
  IF Count <> 0 THEN
    BEGIN
      FOR T := 1 TO N DO
        FOR H := 1 TO N DO
          IF Route[T, H] = 2 THEN
            BEGIN
              Used[T] := True;
              Route[T, H] := 0;
              Route[H, T] := 0;
              Found := False;
I have Ac now!
Posted by Grebnov Ilya[ISPU] 11 Apr 2003 22:54