第二个点RE,本地stackoverflow,求调
查看原帖
第二个点RE,本地stackoverflow,求调
824755
DauntlessHSM楼主2024/3/27 19:24
import java.io.*;

public class P1088 {
    public static int n, add, count;
    public static int[] cur;
    public static boolean[] visited;
    public static boolean flag;
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static StreamTokenizer t = new StreamTokenizer(br);
    public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    public static int nextInt() throws Exception {
        t.nextToken();
        return (int) t.nval;
    }
    public static void dfs(int step) throws Exception {
        if (flag) {
            return;
        }
        if (step == n + 1) {
            count += 1;
            if (count == add) {
                flag = true;
                for (int i = 1; i <= n; i += 1) {
                    if (i == n) {
                        out.print(cur[i]);
                    } else {
                        out.print(cur[i] + " ");
                    }
                }
                out.flush();
            }
            return;
        }
        for (int i = 1; i <= n; i += 1) {
            if (count == 0) {
                i = cur[step];
            }
            if (!visited[i]) {
                visited[i] = true;
                cur[step] = i;
                dfs(step + 1);
                visited[i] = false;
            }
        }
    }
    public static void main(String[] args) throws Exception {
        n = nextInt();
        add = nextInt();
        cur = new int[n + 5];
        visited = new boolean[n + 5];
        count = 0;
        for (int i = 1; i <= n; i += 1) {
            cur[i] = nextInt();
        }
        dfs(1);

    }
}

2024/3/27 19:24
加载中...