Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program HamiltonianPath;
- {$APPTYPE CONSOLE}
- const MAXN = 10;
- var
- n : longint;
- g : array[1..MAXN, 1..MAXN] of boolean;
- ans : array[1..MAXN] of longint;
- was : array[1..MAXN] of boolean;
- function solve(ver, depth : longint) : boolean;
- var
- next : longint;
- begin
- was[ver] := true;
- ans[depth] := ver;
- if depth = n then begin
- result := true;
- exit;
- end;
- for next := 1 to n do if not was[next] and g[ver, next] then begin
- if solve(next, depth + 1) then begin
- result := true;
- exit;
- end;
- end;
- was[ver] := false;
- result := false;
- end;
- var
- m : longint;
- i, i2 : longint;
- a, b : longint;
- begin
- reset(input, 'hamiltonian.in');
- rewrite(output, 'hamiltonian.out');
- fillchar(g, sizeof(g), 0);
- read(n, m);
- for i := 1 to m do begin
- read(a, b);
- g[a, b] := true;
- g[b, a] := true;
- end;
- fillchar(was, sizeof(was), 0);
- for i := 1 to n do begin
- if solve(i, 1) then begin
- for i2 := 1 to n do begin
- if i2 <> 1 then write(' ');
- write(ans[i2]);
- end;
- writeln;
- exit;
- end;
- end;
- writeln('No solution');
- end.
Add Comment
Please, Sign In to add comment