結果

問題 No.1254 補強への架け橋
ユーザー hiromi_ayasehiromi_ayase
提出日時 2020-10-09 23:10:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 560 ms / 2,000 ms
コード長 4,234 bytes
コンパイル時間 2,334 ms
コンパイル使用メモリ 89,604 KB
実行使用メモリ 69,672 KB
最終ジャッジ日時 2024-07-20 14:05:14
合計ジャッジ時間 32,560 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
40,640 KB
testcase_01 AC 85 ms
40,888 KB
testcase_02 AC 89 ms
40,888 KB
testcase_03 AC 90 ms
41,064 KB
testcase_04 AC 90 ms
40,752 KB
testcase_05 AC 87 ms
40,824 KB
testcase_06 AC 91 ms
40,656 KB
testcase_07 AC 87 ms
40,904 KB
testcase_08 AC 89 ms
40,940 KB
testcase_09 AC 86 ms
40,940 KB
testcase_10 AC 88 ms
40,548 KB
testcase_11 AC 88 ms
40,720 KB
testcase_12 AC 89 ms
40,732 KB
testcase_13 AC 90 ms
40,680 KB
testcase_14 AC 92 ms
40,688 KB
testcase_15 AC 91 ms
40,888 KB
testcase_16 AC 90 ms
40,940 KB
testcase_17 AC 92 ms
40,932 KB
testcase_18 AC 91 ms
40,744 KB
testcase_19 AC 88 ms
40,864 KB
testcase_20 AC 93 ms
40,768 KB
testcase_21 AC 88 ms
40,800 KB
testcase_22 AC 87 ms
40,588 KB
testcase_23 AC 89 ms
40,876 KB
testcase_24 AC 89 ms
40,844 KB
testcase_25 AC 88 ms
40,844 KB
testcase_26 AC 92 ms
40,824 KB
testcase_27 AC 92 ms
40,808 KB
testcase_28 AC 87 ms
41,004 KB
testcase_29 AC 94 ms
40,972 KB
testcase_30 AC 94 ms
40,844 KB
testcase_31 AC 94 ms
40,820 KB
testcase_32 AC 94 ms
40,688 KB
testcase_33 AC 95 ms
40,548 KB
testcase_34 AC 93 ms
40,868 KB
testcase_35 AC 92 ms
40,784 KB
testcase_36 AC 96 ms
40,760 KB
testcase_37 AC 92 ms
40,796 KB
testcase_38 AC 91 ms
40,792 KB
testcase_39 AC 88 ms
40,928 KB
testcase_40 AC 88 ms
40,668 KB
testcase_41 AC 89 ms
40,776 KB
testcase_42 AC 86 ms
40,776 KB
testcase_43 AC 102 ms
41,524 KB
testcase_44 AC 96 ms
41,176 KB
testcase_45 AC 96 ms
41,092 KB
testcase_46 AC 102 ms
41,524 KB
testcase_47 AC 105 ms
41,176 KB
testcase_48 AC 104 ms
41,160 KB
testcase_49 AC 93 ms
40,748 KB
testcase_50 AC 108 ms
41,456 KB
testcase_51 AC 108 ms
41,464 KB
testcase_52 AC 115 ms
41,340 KB
testcase_53 AC 102 ms
41,348 KB
testcase_54 AC 110 ms
41,304 KB
testcase_55 AC 106 ms
41,448 KB
testcase_56 AC 94 ms
40,972 KB
testcase_57 AC 109 ms
41,200 KB
testcase_58 AC 105 ms
41,524 KB
testcase_59 AC 94 ms
40,844 KB
testcase_60 AC 106 ms
41,080 KB
testcase_61 AC 112 ms
41,332 KB
testcase_62 AC 100 ms
40,948 KB
testcase_63 AC 179 ms
47,584 KB
testcase_64 AC 135 ms
42,508 KB
testcase_65 AC 171 ms
46,152 KB
testcase_66 AC 144 ms
43,456 KB
testcase_67 AC 119 ms
42,200 KB
testcase_68 AC 153 ms
43,884 KB
testcase_69 AC 175 ms
44,784 KB
testcase_70 AC 136 ms
42,792 KB
testcase_71 AC 127 ms
42,420 KB
testcase_72 AC 175 ms
46,264 KB
testcase_73 AC 127 ms
42,808 KB
testcase_74 AC 173 ms
46,564 KB
testcase_75 AC 170 ms
44,428 KB
testcase_76 AC 110 ms
41,476 KB
testcase_77 AC 152 ms
43,436 KB
testcase_78 AC 194 ms
46,572 KB
testcase_79 AC 197 ms
46,852 KB
testcase_80 AC 183 ms
46,928 KB
testcase_81 AC 178 ms
46,732 KB
testcase_82 AC 175 ms
46,704 KB
testcase_83 AC 393 ms
55,892 KB
testcase_84 AC 365 ms
55,320 KB
testcase_85 AC 293 ms
53,540 KB
testcase_86 AC 350 ms
54,660 KB
testcase_87 AC 378 ms
55,032 KB
testcase_88 AC 199 ms
48,964 KB
testcase_89 AC 373 ms
55,784 KB
testcase_90 AC 301 ms
53,968 KB
testcase_91 AC 275 ms
52,512 KB
testcase_92 AC 226 ms
49,984 KB
testcase_93 AC 341 ms
53,860 KB
testcase_94 AC 311 ms
53,236 KB
testcase_95 AC 313 ms
53,508 KB
testcase_96 AC 349 ms
55,168 KB
testcase_97 AC 263 ms
51,512 KB
testcase_98 AC 367 ms
54,876 KB
testcase_99 AC 301 ms
53,144 KB
testcase_100 AC 362 ms
55,452 KB
testcase_101 AC 220 ms
49,280 KB
testcase_102 AC 187 ms
49,056 KB
testcase_103 AC 221 ms
49,572 KB
testcase_104 AC 241 ms
50,396 KB
testcase_105 AC 340 ms
53,736 KB
testcase_106 AC 280 ms
52,296 KB
testcase_107 AC 376 ms
56,052 KB
testcase_108 AC 374 ms
56,208 KB
testcase_109 AC 324 ms
53,600 KB
testcase_110 AC 334 ms
53,444 KB
testcase_111 AC 326 ms
53,996 KB
testcase_112 AC 249 ms
50,540 KB
testcase_113 AC 336 ms
54,104 KB
testcase_114 AC 267 ms
52,072 KB
testcase_115 AC 202 ms
49,232 KB
testcase_116 AC 290 ms
53,440 KB
testcase_117 AC 243 ms
50,764 KB
testcase_118 AC 361 ms
55,152 KB
testcase_119 AC 277 ms
53,044 KB
testcase_120 AC 365 ms
54,456 KB
testcase_121 AC 235 ms
49,864 KB
testcase_122 AC 295 ms
52,440 KB
testcase_123 AC 91 ms
41,016 KB
testcase_124 AC 560 ms
69,252 KB
testcase_125 AC 548 ms
69,672 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