結果

問題 No.1116 Cycles of Dense Graph
ユーザー hiromi_ayasehiromi_ayase
提出日時 2020-07-18 19:05:06
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 7,103 bytes
コンパイル時間 2,715 ms
コンパイル使用メモリ 85,668 KB
実行使用メモリ 60,620 KB
最終ジャッジ日時 2023-08-20 20:06:50
合計ジャッジ時間 9,245 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
50,672 KB
testcase_01 WA -
testcase_02 AC 87 ms
52,696 KB
testcase_03 AC 77 ms
51,128 KB
testcase_04 AC 73 ms
50,804 KB
testcase_05 AC 111 ms
53,148 KB
testcase_06 AC 161 ms
58,788 KB
testcase_07 AC 94 ms
51,432 KB
testcase_08 AC 74 ms
50,688 KB
testcase_09 AC 80 ms
52,520 KB
testcase_10 AC 83 ms
51,948 KB
testcase_11 AC 149 ms
56,716 KB
testcase_12 AC 113 ms
53,448 KB
testcase_13 AC 72 ms
51,132 KB
testcase_14 AC 84 ms
52,992 KB
testcase_15 AC 81 ms
52,500 KB
testcase_16 AC 111 ms
53,124 KB
testcase_17 AC 75 ms
50,668 KB
testcase_18 AC 101 ms
52,980 KB
testcase_19 AC 136 ms
56,896 KB
testcase_20 AC 99 ms
52,912 KB
testcase_21 AC 90 ms
52,400 KB
testcase_22 AC 188 ms
60,620 KB
testcase_23 AC 79 ms
35,856 KB
testcase_24 AC 435 ms
47,580 KB
testcase_25 AC 72 ms
35,212 KB
testcase_26 AC 322 ms
46,496 KB
testcase_27 AC 79 ms
36,120 KB
testcase_28 AC 95 ms
36,316 KB
testcase_29 AC 73 ms
35,244 KB
testcase_30 AC 72 ms
35,192 KB
testcase_31 AC 71 ms
35,508 KB
testcase_32 AC 72 ms
35,208 KB
testcase_33 AC 73 ms
35,504 KB
testcase_34 AC 71 ms
35,504 KB
testcase_35 AC 73 ms
35,448 KB
testcase_36 AC 328 ms
47,136 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 302 ms
46,024 KB
testcase_40 AC 267 ms
46,328 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