結果

問題 No.1254 補強への架け橋
ユーザー hiromi_ayasehiromi_ayase
提出日時 2020-10-09 23:05:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 663 ms / 2,000 ms
コード長 4,273 bytes
コンパイル時間 3,049 ms
コンパイル使用メモリ 93,300 KB
実行使用メモリ 72,152 KB
最終ジャッジ日時 2024-07-20 13:59:09
合計ジャッジ時間 35,210 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
40,680 KB
testcase_01 AC 94 ms
40,840 KB
testcase_02 AC 91 ms
40,800 KB
testcase_03 AC 92 ms
40,684 KB
testcase_04 AC 92 ms
40,688 KB
testcase_05 AC 90 ms
40,864 KB
testcase_06 AC 93 ms
40,476 KB
testcase_07 AC 93 ms
40,688 KB
testcase_08 AC 95 ms
40,944 KB
testcase_09 AC 94 ms
41,024 KB
testcase_10 AC 93 ms
40,712 KB
testcase_11 AC 94 ms
40,756 KB
testcase_12 AC 96 ms
41,048 KB
testcase_13 AC 97 ms
40,648 KB
testcase_14 AC 95 ms
40,856 KB
testcase_15 AC 96 ms
40,748 KB
testcase_16 AC 92 ms
40,756 KB
testcase_17 AC 93 ms
40,888 KB
testcase_18 AC 93 ms
40,932 KB
testcase_19 AC 92 ms
40,972 KB
testcase_20 AC 95 ms
40,584 KB
testcase_21 AC 93 ms
40,960 KB
testcase_22 AC 93 ms
40,756 KB
testcase_23 AC 93 ms
40,632 KB
testcase_24 AC 98 ms
40,976 KB
testcase_25 AC 94 ms
41,092 KB
testcase_26 AC 97 ms
41,048 KB
testcase_27 AC 95 ms
40,824 KB
testcase_28 AC 98 ms
40,644 KB
testcase_29 AC 93 ms
41,068 KB
testcase_30 AC 91 ms
40,996 KB
testcase_31 AC 96 ms
40,940 KB
testcase_32 AC 91 ms
40,860 KB
testcase_33 AC 94 ms
40,596 KB
testcase_34 AC 95 ms
41,112 KB
testcase_35 AC 96 ms
40,744 KB
testcase_36 AC 92 ms
41,020 KB
testcase_37 AC 94 ms
40,552 KB
testcase_38 AC 96 ms
40,624 KB
testcase_39 AC 93 ms
40,760 KB
testcase_40 AC 95 ms
40,672 KB
testcase_41 AC 91 ms
40,976 KB
testcase_42 AC 92 ms
40,996 KB
testcase_43 AC 110 ms
41,392 KB
testcase_44 AC 104 ms
41,248 KB
testcase_45 AC 101 ms
41,096 KB
testcase_46 AC 109 ms
41,056 KB
testcase_47 AC 105 ms
41,140 KB
testcase_48 AC 108 ms
41,228 KB
testcase_49 AC 94 ms
40,864 KB
testcase_50 AC 108 ms
41,436 KB
testcase_51 AC 111 ms
41,440 KB
testcase_52 AC 111 ms
41,320 KB
testcase_53 AC 111 ms
41,360 KB
testcase_54 AC 107 ms
41,024 KB
testcase_55 AC 109 ms
41,312 KB
testcase_56 AC 94 ms
41,056 KB
testcase_57 AC 111 ms
41,248 KB
testcase_58 AC 117 ms
41,272 KB
testcase_59 AC 98 ms
41,000 KB
testcase_60 AC 104 ms
41,132 KB
testcase_61 AC 114 ms
41,112 KB
testcase_62 AC 102 ms
40,808 KB
testcase_63 AC 187 ms
47,064 KB
testcase_64 AC 134 ms
42,424 KB
testcase_65 AC 179 ms
45,448 KB
testcase_66 AC 157 ms
43,548 KB
testcase_67 AC 126 ms
42,140 KB
testcase_68 AC 162 ms
44,300 KB
testcase_69 AC 186 ms
46,880 KB
testcase_70 AC 154 ms
42,704 KB
testcase_71 AC 137 ms
42,180 KB
testcase_72 AC 180 ms
46,800 KB
testcase_73 AC 139 ms
42,736 KB
testcase_74 AC 184 ms
46,148 KB
testcase_75 AC 179 ms
44,268 KB
testcase_76 AC 118 ms
41,276 KB
testcase_77 AC 156 ms
43,280 KB
testcase_78 AC 191 ms
46,848 KB
testcase_79 AC 194 ms
47,176 KB
testcase_80 AC 189 ms
47,380 KB
testcase_81 AC 192 ms
47,088 KB
testcase_82 AC 187 ms
47,212 KB
testcase_83 AC 407 ms
55,548 KB
testcase_84 AC 397 ms
56,276 KB
testcase_85 AC 315 ms
53,288 KB
testcase_86 AC 376 ms
54,544 KB
testcase_87 AC 367 ms
53,376 KB
testcase_88 AC 207 ms
49,300 KB
testcase_89 AC 407 ms
55,788 KB
testcase_90 AC 316 ms
54,100 KB
testcase_91 AC 291 ms
52,296 KB
testcase_92 AC 239 ms
49,484 KB
testcase_93 AC 362 ms
51,552 KB
testcase_94 AC 315 ms
53,064 KB
testcase_95 AC 331 ms
53,188 KB
testcase_96 AC 379 ms
56,536 KB
testcase_97 AC 275 ms
51,532 KB
testcase_98 AC 402 ms
56,076 KB
testcase_99 AC 323 ms
53,348 KB
testcase_100 AC 391 ms
55,336 KB
testcase_101 AC 225 ms
49,412 KB
testcase_102 AC 205 ms
48,676 KB
testcase_103 AC 244 ms
50,064 KB
testcase_104 AC 266 ms
50,484 KB
testcase_105 AC 341 ms
51,516 KB
testcase_106 AC 291 ms
51,696 KB
testcase_107 AC 388 ms
55,768 KB
testcase_108 AC 398 ms
56,300 KB
testcase_109 AC 343 ms
51,580 KB
testcase_110 AC 320 ms
53,420 KB
testcase_111 AC 347 ms
52,248 KB
testcase_112 AC 260 ms
50,228 KB
testcase_113 AC 317 ms
53,628 KB
testcase_114 AC 276 ms
51,872 KB
testcase_115 AC 214 ms
48,964 KB
testcase_116 AC 303 ms
52,640 KB
testcase_117 AC 243 ms
50,600 KB
testcase_118 AC 347 ms
53,096 KB
testcase_119 AC 282 ms
53,368 KB
testcase_120 AC 364 ms
54,040 KB
testcase_121 AC 237 ms
49,928 KB
testcase_122 AC 273 ms
52,716 KB
testcase_123 AC 92 ms
40,828 KB
testcase_124 AC 663 ms
72,152 KB
testcase_125 AC 627 ms
71,688 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