結果

問題 No.1254 補強への架け橋
ユーザー hiromi_ayasehiromi_ayase
提出日時 2020-10-09 23:10:13
言語 Java19
(openjdk 21)
結果
AC  
実行時間 499 ms / 2,000 ms
コード長 4,234 bytes
コンパイル時間 3,244 ms
コンパイル使用メモリ 83,408 KB
実行使用メモリ 82,684 KB
最終ジャッジ日時 2023-09-27 19:34:30
合計ジャッジ時間 31,726 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
54,044 KB
testcase_01 AC 80 ms
54,600 KB
testcase_02 AC 79 ms
54,128 KB
testcase_03 AC 80 ms
54,148 KB
testcase_04 AC 79 ms
53,884 KB
testcase_05 AC 81 ms
54,216 KB
testcase_06 AC 81 ms
54,296 KB
testcase_07 AC 82 ms
54,152 KB
testcase_08 AC 81 ms
53,932 KB
testcase_09 AC 80 ms
53,928 KB
testcase_10 AC 82 ms
54,448 KB
testcase_11 AC 82 ms
54,080 KB
testcase_12 AC 81 ms
54,112 KB
testcase_13 AC 83 ms
54,332 KB
testcase_14 AC 84 ms
53,804 KB
testcase_15 AC 86 ms
54,128 KB
testcase_16 AC 82 ms
54,056 KB
testcase_17 AC 83 ms
56,564 KB
testcase_18 AC 81 ms
54,048 KB
testcase_19 AC 80 ms
54,224 KB
testcase_20 AC 85 ms
53,988 KB
testcase_21 AC 82 ms
54,036 KB
testcase_22 AC 82 ms
54,120 KB
testcase_23 AC 84 ms
53,944 KB
testcase_24 AC 86 ms
54,020 KB
testcase_25 AC 87 ms
54,028 KB
testcase_26 AC 88 ms
54,224 KB
testcase_27 AC 83 ms
54,176 KB
testcase_28 AC 84 ms
54,008 KB
testcase_29 AC 86 ms
53,948 KB
testcase_30 AC 81 ms
54,156 KB
testcase_31 AC 84 ms
53,976 KB
testcase_32 AC 83 ms
54,080 KB
testcase_33 AC 84 ms
54,164 KB
testcase_34 AC 83 ms
53,948 KB
testcase_35 AC 85 ms
53,900 KB
testcase_36 AC 88 ms
54,024 KB
testcase_37 AC 82 ms
53,980 KB
testcase_38 AC 83 ms
53,816 KB
testcase_39 AC 79 ms
53,948 KB
testcase_40 AC 80 ms
54,080 KB
testcase_41 AC 79 ms
54,048 KB
testcase_42 AC 78 ms
54,244 KB
testcase_43 AC 97 ms
54,264 KB
testcase_44 AC 95 ms
54,688 KB
testcase_45 AC 98 ms
54,208 KB
testcase_46 AC 98 ms
54,384 KB
testcase_47 AC 97 ms
54,424 KB
testcase_48 AC 97 ms
54,184 KB
testcase_49 AC 85 ms
53,788 KB
testcase_50 AC 96 ms
54,412 KB
testcase_51 AC 101 ms
54,336 KB
testcase_52 AC 100 ms
54,280 KB
testcase_53 AC 100 ms
54,380 KB
testcase_54 AC 98 ms
54,248 KB
testcase_55 AC 97 ms
54,712 KB
testcase_56 AC 86 ms
53,996 KB
testcase_57 AC 97 ms
54,084 KB
testcase_58 AC 98 ms
54,200 KB
testcase_59 AC 86 ms
54,248 KB
testcase_60 AC 97 ms
54,048 KB
testcase_61 AC 99 ms
54,372 KB
testcase_62 AC 89 ms
54,148 KB
testcase_63 AC 172 ms
59,932 KB
testcase_64 AC 128 ms
55,704 KB
testcase_65 AC 160 ms
60,540 KB
testcase_66 AC 135 ms
56,224 KB
testcase_67 AC 105 ms
55,396 KB
testcase_68 AC 154 ms
58,048 KB
testcase_69 AC 167 ms
60,360 KB
testcase_70 AC 132 ms
55,952 KB
testcase_71 AC 121 ms
55,908 KB
testcase_72 AC 164 ms
60,976 KB
testcase_73 AC 125 ms
56,424 KB
testcase_74 AC 164 ms
60,204 KB
testcase_75 AC 144 ms
55,916 KB
testcase_76 AC 101 ms
54,300 KB
testcase_77 AC 134 ms
56,132 KB
testcase_78 AC 172 ms
59,444 KB
testcase_79 AC 171 ms
59,392 KB
testcase_80 AC 171 ms
60,600 KB
testcase_81 AC 176 ms
59,688 KB
testcase_82 AC 174 ms
60,452 KB
testcase_83 AC 356 ms
66,736 KB
testcase_84 AC 352 ms
67,024 KB
testcase_85 AC 273 ms
64,696 KB
testcase_86 AC 331 ms
66,540 KB
testcase_87 AC 328 ms
66,524 KB
testcase_88 AC 175 ms
60,056 KB
testcase_89 AC 344 ms
66,412 KB
testcase_90 AC 275 ms
64,748 KB
testcase_91 AC 255 ms
64,760 KB
testcase_92 AC 202 ms
62,108 KB
testcase_93 AC 332 ms
66,468 KB
testcase_94 AC 297 ms
65,172 KB
testcase_95 AC 302 ms
65,456 KB
testcase_96 AC 350 ms
66,900 KB
testcase_97 AC 244 ms
62,432 KB
testcase_98 AC 345 ms
66,816 KB
testcase_99 AC 273 ms
64,804 KB
testcase_100 AC 346 ms
66,420 KB
testcase_101 AC 197 ms
62,636 KB
testcase_102 AC 176 ms
59,832 KB
testcase_103 AC 206 ms
62,776 KB
testcase_104 AC 227 ms
62,692 KB
testcase_105 AC 313 ms
66,592 KB
testcase_106 AC 255 ms
64,796 KB
testcase_107 AC 347 ms
66,364 KB
testcase_108 AC 353 ms
66,736 KB
testcase_109 AC 311 ms
64,556 KB
testcase_110 AC 302 ms
64,164 KB
testcase_111 AC 305 ms
64,304 KB
testcase_112 AC 220 ms
62,488 KB
testcase_113 AC 313 ms
65,060 KB
testcase_114 AC 265 ms
64,540 KB
testcase_115 AC 194 ms
60,880 KB
testcase_116 AC 262 ms
64,692 KB
testcase_117 AC 215 ms
62,560 KB
testcase_118 AC 317 ms
66,572 KB
testcase_119 AC 265 ms
64,584 KB
testcase_120 AC 335 ms
66,612 KB
testcase_121 AC 209 ms
63,088 KB
testcase_122 AC 267 ms
64,900 KB
testcase_123 AC 85 ms
54,036 KB
testcase_124 AC 499 ms
80,728 KB
testcase_125 AC 496 ms
82,684 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);

    System.gc();
    int[] ret = dfs(0, -1, g, new boolean[n], new int[n], new int[n], 0);
    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 p = 0;
        while (vhist[depth - p] != nex) {
          p++;

        }
        int[] ret = Arrays.copyOfRange(ehist, depth - p, depth + 1);
        return ret;
      }

      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