type T: array [1..n] of integer; var n: integer; graph: [1..n, 1..n] of integer; // матрица смежности visited: array [1..n] of boolean; function dfs(start_node: integer; current_node: integer; step: integer): boolean; var i: integer; begin if (step > 1) and (start_node == current_node) then Exit(false); if step > 1 then visited[current_node] := true; for i := 1 to n do // короче, первое условие - это проверка на смежность if (graph[current_node, i] <> 0) and not visited[i] then if not dfs(start_node, i, step + 1) then Exit(false); Exit(true); end; function check_for_circuits: boolean; begin for i := 1 to n do begin for j := 1 to n do visited[j] := false; if not dfs(i, i, 1) then Exit(false); end; Exit(true); end; // ... if check_for_circuits then begin // всё ок, контуров нет, ищем что нужно алгоритмом Дейкстры end;