Advertisement
agul

CFBR #88 :: Problem C

Sep 23rd, 2011
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.24 KB | None | 0 0
  1. uses
  2.     SysUtils, Math;
  3.  
  4. var
  5.     n, i, j, tt : longint;
  6.     a : array[0..5010, 0..5010] of boolean;
  7.     g : array[0..5010] of array of longint;
  8.     ch : char;
  9.     w : array[0..5010] of boolean;
  10.  
  11. procedure ok(a, b, c : longint);
  12. begin
  13.     write(a, ' ', b, ' ', c);
  14.     halt(0);
  15. end;
  16.  
  17. procedure dfs(v, c, f : longint);
  18.  
  19. var
  20.     i, tt, tto : longint;
  21.  
  22. begin
  23.     w[v] := true;
  24.     tt := length(g[v]) - 1;
  25.     for i := 0 to tt do begin
  26.         tto := g[v][i];
  27.         if c = 1 then begin
  28.             if not w[tto] then dfs(tto, 2, v);
  29.         end else
  30.         if c = 2 then begin
  31.             if a[tto][f] then ok(f, v, tto) else
  32.             if not w[tto] then dfs(tto, 2, v);
  33.         end;
  34.     end;
  35. end;
  36.    
  37. begin  
  38.     readln(n);
  39.     for i := 1 to n do begin
  40.         for j := 1 to n do begin
  41.             read(ch);
  42.             a[i][j] := ch = '1';
  43.         end;
  44.         readln;
  45.     end;      
  46.     for i := 1 to n do
  47.         for j := 1 to n do
  48.             if a[i][j] then begin
  49.                 tt := length(g[i]);
  50.                 setlength(g[i], tt + 1);
  51.                 g[i][tt] := j;
  52.             end;
  53.     fillchar(w, sizeof(w), 0);
  54.     for i := 1 to n do
  55.         if not w[i] then dfs(i, 1, i);
  56.     write(-1);
  57. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement