結果

問題 No.1254 補強への架け橋
ユーザー hiromi_ayasehiromi_ayase
提出日時 2020-10-09 23:05:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 628 ms / 2,000 ms
コード長 4,273 bytes
コンパイル時間 4,887 ms
コンパイル使用メモリ 78,932 KB
実行使用メモリ 85,124 KB
最終ジャッジ日時 2023-09-27 19:28:31
合計ジャッジ時間 35,622 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
55,828 KB
testcase_01 AC 88 ms
53,980 KB
testcase_02 AC 88 ms
53,960 KB
testcase_03 AC 89 ms
51,956 KB
testcase_04 AC 89 ms
54,072 KB
testcase_05 AC 88 ms
53,816 KB
testcase_06 AC 91 ms
53,812 KB
testcase_07 AC 89 ms
53,928 KB
testcase_08 AC 87 ms
53,916 KB
testcase_09 AC 89 ms
53,776 KB
testcase_10 AC 90 ms
53,836 KB
testcase_11 AC 90 ms
53,916 KB
testcase_12 AC 89 ms
54,088 KB
testcase_13 AC 90 ms
53,936 KB
testcase_14 AC 92 ms
53,820 KB
testcase_15 AC 89 ms
51,992 KB
testcase_16 AC 88 ms
53,952 KB
testcase_17 AC 90 ms
53,936 KB
testcase_18 AC 90 ms
53,860 KB
testcase_19 AC 90 ms
53,824 KB
testcase_20 AC 95 ms
56,168 KB
testcase_21 AC 88 ms
54,236 KB
testcase_22 AC 90 ms
53,860 KB
testcase_23 AC 92 ms
53,912 KB
testcase_24 AC 93 ms
53,804 KB
testcase_25 AC 90 ms
53,840 KB
testcase_26 AC 91 ms
53,932 KB
testcase_27 AC 90 ms
53,940 KB
testcase_28 AC 94 ms
53,728 KB
testcase_29 AC 89 ms
53,896 KB
testcase_30 AC 90 ms
53,912 KB
testcase_31 AC 92 ms
53,988 KB
testcase_32 AC 89 ms
54,252 KB
testcase_33 AC 93 ms
53,952 KB
testcase_34 AC 93 ms
53,756 KB
testcase_35 AC 94 ms
53,860 KB
testcase_36 AC 93 ms
53,796 KB
testcase_37 AC 94 ms
53,704 KB
testcase_38 AC 94 ms
53,712 KB
testcase_39 AC 92 ms
53,976 KB
testcase_40 AC 92 ms
53,884 KB
testcase_41 AC 90 ms
54,164 KB
testcase_42 AC 89 ms
53,876 KB
testcase_43 AC 107 ms
54,028 KB
testcase_44 AC 103 ms
54,200 KB
testcase_45 AC 105 ms
54,344 KB
testcase_46 AC 109 ms
54,244 KB
testcase_47 AC 105 ms
54,156 KB
testcase_48 AC 109 ms
54,420 KB
testcase_49 AC 94 ms
53,780 KB
testcase_50 AC 111 ms
54,220 KB
testcase_51 AC 108 ms
52,424 KB
testcase_52 AC 107 ms
54,236 KB
testcase_53 AC 109 ms
54,480 KB
testcase_54 AC 109 ms
54,404 KB
testcase_55 AC 107 ms
54,324 KB
testcase_56 AC 95 ms
54,196 KB
testcase_57 AC 106 ms
54,244 KB
testcase_58 AC 108 ms
54,176 KB
testcase_59 AC 94 ms
53,996 KB
testcase_60 AC 103 ms
54,140 KB
testcase_61 AC 108 ms
54,436 KB
testcase_62 AC 95 ms
51,960 KB
testcase_63 AC 186 ms
59,108 KB
testcase_64 AC 136 ms
55,872 KB
testcase_65 AC 181 ms
60,328 KB
testcase_66 AC 152 ms
56,068 KB
testcase_67 AC 117 ms
55,360 KB
testcase_68 AC 157 ms
57,736 KB
testcase_69 AC 180 ms
60,444 KB
testcase_70 AC 147 ms
55,752 KB
testcase_71 AC 131 ms
55,852 KB
testcase_72 AC 181 ms
60,348 KB
testcase_73 AC 140 ms
55,696 KB
testcase_74 AC 181 ms
60,164 KB
testcase_75 AC 154 ms
57,952 KB
testcase_76 AC 109 ms
54,216 KB
testcase_77 AC 145 ms
55,848 KB
testcase_78 AC 188 ms
58,728 KB
testcase_79 AC 187 ms
59,656 KB
testcase_80 AC 179 ms
60,564 KB
testcase_81 AC 188 ms
59,988 KB
testcase_82 AC 185 ms
59,516 KB
testcase_83 AC 392 ms
66,304 KB
testcase_84 AC 377 ms
66,556 KB
testcase_85 AC 309 ms
64,736 KB
testcase_86 AC 343 ms
66,800 KB
testcase_87 AC 366 ms
64,480 KB
testcase_88 AC 198 ms
60,488 KB
testcase_89 AC 373 ms
66,208 KB
testcase_90 AC 313 ms
65,932 KB
testcase_91 AC 276 ms
64,488 KB
testcase_92 AC 224 ms
62,156 KB
testcase_93 AC 351 ms
62,316 KB
testcase_94 AC 313 ms
64,320 KB
testcase_95 AC 313 ms
65,436 KB
testcase_96 AC 354 ms
66,624 KB
testcase_97 AC 267 ms
62,784 KB
testcase_98 AC 383 ms
66,652 KB
testcase_99 AC 303 ms
65,068 KB
testcase_100 AC 377 ms
66,256 KB
testcase_101 AC 221 ms
61,728 KB
testcase_102 AC 190 ms
60,640 KB
testcase_103 AC 224 ms
62,040 KB
testcase_104 AC 246 ms
62,316 KB
testcase_105 AC 334 ms
62,196 KB
testcase_106 AC 271 ms
62,204 KB
testcase_107 AC 370 ms
66,884 KB
testcase_108 AC 373 ms
66,564 KB
testcase_109 AC 338 ms
62,344 KB
testcase_110 AC 319 ms
65,116 KB
testcase_111 AC 330 ms
64,588 KB
testcase_112 AC 239 ms
62,424 KB
testcase_113 AC 319 ms
64,512 KB
testcase_114 AC 272 ms
64,636 KB
testcase_115 AC 205 ms
59,840 KB
testcase_116 AC 279 ms
64,720 KB
testcase_117 AC 252 ms
62,664 KB
testcase_118 AC 372 ms
66,860 KB
testcase_119 AC 296 ms
62,760 KB
testcase_120 AC 374 ms
64,168 KB
testcase_121 AC 231 ms
62,396 KB
testcase_122 AC 294 ms
64,908 KB
testcase_123 AC 88 ms
53,824 KB
testcase_124 AC 626 ms
85,124 KB
testcase_125 AC 628 ms
83,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

@SuppressWarnings("unused")
public class Main {

  private static void solve() {
    int n = ni();
    int[] from = new int[n];
    int[] to = new int[n];
    int[] w = new int[n];
    for (int i = 0; i < n; i++) {
      from[i] = ni() - 1;
      to[i] = ni() - 1;
      w[i] = i;
    }
    int[][][] g = packWU(n, from, to, w);

    int[] ret = dfs(0, -1, g, new boolean[n], new int[n], new int[n], 0);
    System.gc();
    out.println(ret.length);
    for (int v : ret) {
      out.print(v + 1 + " ");
    }
    out.println();
  }

  private static int[] dfs(int cur, int pre, int[][][] g, boolean[] ved, int[] ehist, int[] vhist, int depth) {
    ved[cur] = true;
    vhist[depth] = cur;

    for (int[] v : g[cur]) {
      int nex = v[0];
      int e = v[1];
      ehist[depth] = e;
      if (nex == pre)
        continue;

      if (ved[nex]) {
        int[] ret = new int[depth + 1];
        int p = 0;
        while (vhist[depth] != nex) {
          ret[p++] = ehist[depth--];
        }
        ret[p++] = ehist[depth];
        return Arrays.copyOf(ret, p);
      }

      int[] ret = dfs(nex, cur, g, ved, ehist, vhist, depth + 1);
      if (ret != null) {
        return ret;
      }
    }
    return null;
  }

  public static int[][][] packWU(int n, int[] from, int[] to, int[] w) {
    return packWU(n, from, to, w, from.length);
  }

  public static int[][][] packWU(int n, int[] from, int[] to, int[] w, int sup) {
    int[][][] g = new int[n][][];
    int[] p = new int[n];
    for (int i = 0; i < sup; i++)
      p[from[i]]++;
    for (int i = 0; i < sup; i++)
      p[to[i]]++;
    for (int i = 0; i < n; i++)
      g[i] = new int[p[i]][2];
    for (int i = 0; i < sup; i++) {
      --p[from[i]];
      g[from[i]][p[from[i]]][0] = to[i];
      g[from[i]][p[from[i]]][1] = w[i];
      --p[to[i]];
      g[to[i]][p[to[i]]][0] = from[i];
      g[to[i]][p[to[i]]][1] = w[i];
    }
    return g;
  }

  public static void main(String[] args) {
    new Thread(null, new Runnable() {
      @Override
      public void run() {
        long start = System.currentTimeMillis();
        String debug = args.length > 0 ? args[0] : null;
        if (debug != null) {
          try {
            is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug));
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
        reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
        solve();
        out.flush();
        tr((System.currentTimeMillis() - start) + "ms");
      }
    }, "", 64000000).start();
  }

  private static java.io.InputStream is = System.in;
  private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
  private static java.util.StringTokenizer tokenizer = null;
  private static java.io.BufferedReader reader;

  public static String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }

  private static double nd() {
    return Double.parseDouble(next());
  }

  private static long nl() {
    return Long.parseLong(next());
  }

  private static int[] na(int n) {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = ni();
    return a;
  }

  private static char[] ns() {
    return next().toCharArray();
  }

  private static long[] nal(int n) {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nl();
    return a;
  }

  private static int[][] ntable(int n, int m) {
    int[][] table = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[i][j] = ni();
      }
    }
    return table;
  }

  private static int[][] nlist(int n, int m) {
    int[][] table = new int[m][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[j][i] = ni();
      }
    }
    return table;
  }

  private static int ni() {
    return Integer.parseInt(next());
  }

  private static void tr(Object... o) {
    if (is != System.in)
      System.out.println(java.util.Arrays.deepToString(o));
  }
}
0