結果

問題 No.1254 補強への架け橋
ユーザー hiromi_ayasehiromi_ayase
提出日時 2020-10-09 23:01:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 583 ms / 2,000 ms
コード長 4,278 bytes
コンパイル時間 3,917 ms
コンパイル使用メモリ 79,800 KB
実行使用メモリ 83,960 KB
最終ジャッジ日時 2023-09-27 19:16:29
合計ジャッジ時間 30,746 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
52,804 KB
testcase_01 AC 73 ms
53,048 KB
testcase_02 AC 76 ms
52,632 KB
testcase_03 AC 76 ms
52,776 KB
testcase_04 AC 74 ms
52,868 KB
testcase_05 AC 74 ms
53,092 KB
testcase_06 AC 74 ms
52,792 KB
testcase_07 AC 73 ms
52,776 KB
testcase_08 AC 76 ms
54,884 KB
testcase_09 AC 74 ms
52,956 KB
testcase_10 AC 73 ms
52,744 KB
testcase_11 AC 75 ms
52,700 KB
testcase_12 AC 76 ms
52,836 KB
testcase_13 AC 80 ms
52,724 KB
testcase_14 AC 75 ms
52,584 KB
testcase_15 AC 76 ms
52,896 KB
testcase_16 AC 74 ms
52,620 KB
testcase_17 AC 74 ms
52,840 KB
testcase_18 AC 74 ms
53,112 KB
testcase_19 AC 74 ms
52,632 KB
testcase_20 AC 80 ms
52,436 KB
testcase_21 AC 75 ms
52,644 KB
testcase_22 AC 77 ms
52,692 KB
testcase_23 AC 76 ms
52,996 KB
testcase_24 AC 79 ms
52,776 KB
testcase_25 AC 80 ms
50,828 KB
testcase_26 AC 79 ms
52,900 KB
testcase_27 AC 78 ms
52,676 KB
testcase_28 AC 80 ms
52,500 KB
testcase_29 AC 79 ms
53,080 KB
testcase_30 AC 75 ms
52,656 KB
testcase_31 AC 79 ms
52,824 KB
testcase_32 AC 77 ms
52,712 KB
testcase_33 AC 82 ms
53,096 KB
testcase_34 AC 80 ms
51,032 KB
testcase_35 AC 81 ms
52,652 KB
testcase_36 AC 79 ms
52,884 KB
testcase_37 AC 79 ms
52,500 KB
testcase_38 AC 79 ms
52,588 KB
testcase_39 AC 78 ms
52,868 KB
testcase_40 AC 77 ms
52,736 KB
testcase_41 AC 77 ms
52,812 KB
testcase_42 AC 78 ms
52,984 KB
testcase_43 AC 100 ms
52,916 KB
testcase_44 AC 90 ms
54,068 KB
testcase_45 AC 87 ms
52,864 KB
testcase_46 AC 92 ms
52,804 KB
testcase_47 AC 91 ms
52,692 KB
testcase_48 AC 94 ms
52,868 KB
testcase_49 AC 84 ms
52,528 KB
testcase_50 AC 95 ms
52,736 KB
testcase_51 AC 92 ms
52,796 KB
testcase_52 AC 91 ms
53,156 KB
testcase_53 AC 94 ms
52,888 KB
testcase_54 AC 93 ms
52,884 KB
testcase_55 AC 95 ms
53,068 KB
testcase_56 AC 82 ms
52,516 KB
testcase_57 AC 92 ms
53,028 KB
testcase_58 AC 92 ms
52,872 KB
testcase_59 AC 82 ms
52,544 KB
testcase_60 AC 88 ms
53,528 KB
testcase_61 AC 94 ms
52,680 KB
testcase_62 AC 81 ms
52,692 KB
testcase_63 AC 160 ms
59,160 KB
testcase_64 AC 122 ms
54,636 KB
testcase_65 AC 150 ms
59,148 KB
testcase_66 AC 130 ms
56,492 KB
testcase_67 AC 102 ms
54,392 KB
testcase_68 AC 153 ms
56,816 KB
testcase_69 AC 162 ms
59,008 KB
testcase_70 AC 132 ms
54,316 KB
testcase_71 AC 123 ms
54,200 KB
testcase_72 AC 158 ms
59,160 KB
testcase_73 AC 120 ms
54,544 KB
testcase_74 AC 159 ms
58,976 KB
testcase_75 AC 133 ms
56,488 KB
testcase_76 AC 93 ms
53,248 KB
testcase_77 AC 124 ms
56,564 KB
testcase_78 AC 156 ms
58,236 KB
testcase_79 AC 158 ms
57,124 KB
testcase_80 AC 160 ms
58,936 KB
testcase_81 AC 160 ms
57,820 KB
testcase_82 AC 157 ms
59,200 KB
testcase_83 AC 324 ms
65,084 KB
testcase_84 AC 317 ms
65,932 KB
testcase_85 AC 257 ms
61,848 KB
testcase_86 AC 306 ms
65,556 KB
testcase_87 AC 307 ms
65,428 KB
testcase_88 AC 171 ms
58,860 KB
testcase_89 AC 322 ms
65,600 KB
testcase_90 AC 273 ms
61,968 KB
testcase_91 AC 235 ms
59,400 KB
testcase_92 AC 197 ms
61,840 KB
testcase_93 AC 303 ms
60,816 KB
testcase_94 AC 274 ms
63,336 KB
testcase_95 AC 278 ms
63,656 KB
testcase_96 AC 317 ms
65,780 KB
testcase_97 AC 236 ms
61,040 KB
testcase_98 AC 323 ms
65,928 KB
testcase_99 AC 257 ms
61,348 KB
testcase_100 AC 326 ms
65,528 KB
testcase_101 AC 187 ms
58,812 KB
testcase_102 AC 172 ms
58,824 KB
testcase_103 AC 193 ms
61,496 KB
testcase_104 AC 212 ms
59,424 KB
testcase_105 AC 286 ms
61,120 KB
testcase_106 AC 233 ms
60,844 KB
testcase_107 AC 314 ms
65,880 KB
testcase_108 AC 324 ms
65,328 KB
testcase_109 AC 284 ms
61,068 KB
testcase_110 AC 270 ms
63,660 KB
testcase_111 AC 283 ms
63,212 KB
testcase_112 AC 209 ms
57,788 KB
testcase_113 AC 280 ms
62,712 KB
testcase_114 AC 232 ms
61,216 KB
testcase_115 AC 175 ms
59,824 KB
testcase_116 AC 250 ms
63,392 KB
testcase_117 AC 206 ms
59,420 KB
testcase_118 AC 304 ms
65,940 KB
testcase_119 AC 247 ms
63,704 KB
testcase_120 AC 313 ms
63,364 KB
testcase_121 AC 197 ms
59,168 KB
testcase_122 AC 256 ms
59,780 KB
testcase_123 AC 84 ms
52,640 KB
testcase_124 AC 569 ms
83,960 KB
testcase_125 AC 583 ms
83,820 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);
    Arrays.sort(ret);
    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