結果

問題 No.1116 Cycles of Dense Graph
ユーザー hiromi_ayasehiromi_ayase
提出日時 2020-07-18 19:03:53
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 7,097 bytes
コンパイル時間 2,769 ms
コンパイル使用メモリ 93,100 KB
実行使用メモリ 62,024 KB
最終ジャッジ日時 2024-05-08 02:32:28
合計ジャッジ時間 8,386 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
51,336 KB
testcase_01 WA -
testcase_02 AC 82 ms
39,852 KB
testcase_03 WA -
testcase_04 AC 70 ms
38,332 KB
testcase_05 AC 105 ms
40,772 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 75 ms
38,888 KB
testcase_10 AC 80 ms
38,944 KB
testcase_11 WA -
testcase_12 AC 104 ms
41,004 KB
testcase_13 AC 70 ms
38,308 KB
testcase_14 AC 79 ms
39,272 KB
testcase_15 WA -
testcase_16 AC 103 ms
41,180 KB
testcase_17 WA -
testcase_18 AC 86 ms
39,380 KB
testcase_19 WA -
testcase_20 AC 96 ms
39,724 KB
testcase_21 AC 87 ms
39,012 KB
testcase_22 AC 177 ms
58,832 KB
testcase_23 WA -
testcase_24 AC 407 ms
62,024 KB
testcase_25 WA -
testcase_26 AC 294 ms
50,020 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 69 ms
38,012 KB
testcase_30 AC 73 ms
38,504 KB
testcase_31 AC 70 ms
38,064 KB
testcase_32 AC 69 ms
38,128 KB
testcase_33 AC 67 ms
38,252 KB
testcase_34 AC 67 ms
38,188 KB
testcase_35 AC 71 ms
38,056 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 300 ms
49,756 KB
testcase_40 AC 250 ms
49,044 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 += 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