結果

問題 No.1116 Cycles of Dense Graph
ユーザー hiromi_ayasehiromi_ayase
提出日時 2020-07-18 19:05:06
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 7,103 bytes
コンパイル時間 2,926 ms
コンパイル使用メモリ 86,260 KB
実行使用メモリ 51,604 KB
最終ジャッジ日時 2024-05-08 02:34:09
合計ジャッジ時間 9,618 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
37,912 KB
testcase_01 WA -
testcase_02 AC 100 ms
39,980 KB
testcase_03 AC 87 ms
38,312 KB
testcase_04 AC 83 ms
38,292 KB
testcase_05 AC 121 ms
40,904 KB
testcase_06 AC 171 ms
46,184 KB
testcase_07 AC 103 ms
40,100 KB
testcase_08 AC 83 ms
38,164 KB
testcase_09 AC 89 ms
38,964 KB
testcase_10 AC 91 ms
39,184 KB
testcase_11 AC 161 ms
44,892 KB
testcase_12 AC 122 ms
40,708 KB
testcase_13 AC 80 ms
38,088 KB
testcase_14 AC 92 ms
38,796 KB
testcase_15 AC 90 ms
38,888 KB
testcase_16 AC 123 ms
40,528 KB
testcase_17 AC 84 ms
38,340 KB
testcase_18 AC 100 ms
39,600 KB
testcase_19 AC 142 ms
43,452 KB
testcase_20 AC 107 ms
39,456 KB
testcase_21 AC 99 ms
38,948 KB
testcase_22 AC 195 ms
47,100 KB
testcase_23 AC 86 ms
38,980 KB
testcase_24 AC 451 ms
51,604 KB
testcase_25 AC 82 ms
38,008 KB
testcase_26 AC 369 ms
49,836 KB
testcase_27 AC 88 ms
38,868 KB
testcase_28 AC 104 ms
38,992 KB
testcase_29 AC 82 ms
37,896 KB
testcase_30 AC 80 ms
38,180 KB
testcase_31 AC 83 ms
38,408 KB
testcase_32 AC 83 ms
38,268 KB
testcase_33 AC 80 ms
38,180 KB
testcase_34 AC 81 ms
38,048 KB
testcase_35 AC 80 ms
38,272 KB
testcase_36 AC 372 ms
50,140 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 342 ms
49,560 KB
testcase_40 AC 293 ms
49,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Main {

  private static int mod = 998244353;
  private static long inv2 = invl(2, mod);
  private static int[][] fif;
  private static long[] p2;
  private static int[] from;
  private static int[] to;
  private static int n;
  private static int m;
  private static int k;

  private static void solve() {
    n = ni();
    m = ni();
    from = new int[m];
    to = new int[m];

    for (int i = 0; i < m; i++) {
      from[i] = ni() - 1;
      to[i] = ni() - 1;
    }

    fif = enumFIF(n * 2, mod);
    p2 = new long[n + 1];
    p2[0] = 1;
    for (int i = 1; i <= n; i++) {
      p2[i] = p2[i - 1] * 2 % mod;
    }

    k = m * 2;
    int[][][][] cnt = new int[k + 1][m + 1][2][2];
    for (int i = 0; i < 1 << m; i++) {
      int[] v = f(i);
      cnt[v[0]][v[1]][v[2]][v[3]]++;
    }

    long ret = 0;
    for (int i = 0; i <= k; i++) {
      for (int j = 0; j <= m; j++) {
        for (int s = 0; s < 2; s++) {
          for (int t = 0; t < 2; t++) {
            if (cnt[i][j][s][t] == 0)
              continue;
            long now = g(i, j, t == 1) * cnt[i][j][s][t] % mod;
            long x = s == 0 ? 1 : -1;

            ret += mod + x * now;
            ret %= mod;
          }
        }
      }
    }

    System.out.println(ret);
  }

  private static long g(int v, int u, boolean loop) {
    if (loop) {
      return 1;
    }

    long ret = 0;
    int nlast = n - v;
    for (int i = Math.max(0, 3 - v); i <= nlast; i++) {
      long now = CX(nlast, i, mod, fif);
      now = now * p2[u] % mod;
      now = now * fif[0][i + u - 1] % mod;
      now = now * inv2 % mod;
      ret += now;
      ret %= mod;
    }
    return ret;
  }

  private static int[] f(int s) {
    Map<Integer, Integer> map = new HashMap<>();
    int cnt = 0;
    int[] deg = new int[k];
    DisjointSet uf = new DisjointSet(k);

    boolean loop = false;
    for (int i = 0; i < m; i++) {
      if ((s >> i & 1) == 1) {
        int a = from[i];
        int b = to[i];
        if (!map.containsKey(a)) {
          map.put(a, cnt++);
        }
        if (!map.containsKey(b)) {
          map.put(b, cnt++);
        }
        a = map.get(a);
        b = map.get(b);

        if (deg[a] == 2 || deg[b] == 2 || loop && uf.equiv(a, b)) {
          return null;
        }
        if (!loop && uf.equiv(a, b)) {
          loop = true;
        }
        deg[a]++;
        deg[b]++;

        uf.union(a, b);
      }
    }
    int v = 0;
    for (int i = 0; i < cnt; i++) {
      if (uf.upper[i] < 0) {
        v++;
      }
    }
    return new int[] { cnt, v, Integer.bitCount(s) % 2, loop ? 1 : 0 };
  }

  private static long invl(long a, long mod) {
    long b = mod;
    long p = 1, q = 0;
    while (b > 0) {
      long c = a / b;
      long d;
      d = a;
      a = b;
      b = d % b;
      d = p;
      p = q;
      q = d - c * q;
    }
    return p < 0 ? p + mod : p;
  }

  public static class DisjointSet {
    public int[] upper; // minus:num_element(root) plus:root(normal)
    // public int[] w;

    public DisjointSet(int n) {
      upper = new int[n];
      Arrays.fill(upper, -1);
    }

    public DisjointSet(DisjointSet ds) {
      this.upper = Arrays.copyOf(ds.upper, ds.upper.length);
    }

    public int root(int x) {
      return upper[x] < 0 ? x : (upper[x] = root(upper[x]));
    }

    public boolean equiv(int x, int y) {
      return root(x) == root(y);
    }

    public boolean union(int x, int y) {
      x = root(x);
      y = root(y);
      if (x != y) {
        if (upper[y] < upper[x]) {
          int d = x;
          x = y;
          y = d;
        }
        upper[x] += upper[y];
        upper[y] = x;
      }
      return x == y;
    }
  }

  public static long CX(long n, long r, int p, int[][] fif) {
    if (n < 0 || r < 0 || r > n)
      return 0;
    int np = (int) (n % p), rp = (int) (r % p);
    if (np < rp)
      return 0;
    if (n == 0 && r == 0)
      return 1;
    int nrp = np - rp;
    if (nrp < 0)
      nrp += p;
    return (long) fif[0][np] * fif[1][rp] % p * fif[1][nrp] % p * CX(n / p, r / p, p, fif) % p;
  }

  public static int[][] enumFIF(int n, int mod) {
    int[] f = new int[n + 1];
    int[] invf = new int[n + 1];
    f[0] = 1;
    for (int i = 1; i <= n; i++) {
      f[i] = (int) ((long) f[i - 1] * i % mod);
    }
    long a = f[n];
    long b = mod;
    long p = 1, q = 0;
    while (b > 0) {
      long c = a / b;
      long d;
      d = a;
      a = b;
      b = d % b;
      d = p;
      p = q;
      q = d - c * q;
    }
    invf[n] = (int) (p < 0 ? p + mod : p);
    for (int i = n - 1; i >= 0; i--) {
      invf[i] = (int) ((long) invf[i + 1] * (i + 1) % mod);
    }
    return new int[][] { f, invf };
  }

  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