結果
問題 | No.1303 Inconvenient Kingdom |
ユーザー |
👑 |
提出日時 | 2020-11-27 22:30:47 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 287 ms / 3,000 ms |
コード長 | 7,794 bytes |
コンパイル時間 | 1,893 ms |
コンパイル使用メモリ | 162,704 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-22 10:05:57 |
合計ジャッジ時間 | 5,612 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 34 |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string;import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;import core.bitop;class EOFException : Throwable { this() { super("EOF"); } }string[] tokens;string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }int readInt() { return readToken.to!int; }long readLong() { return readToken.to!long; }real readReal() { return readToken.to!real; }bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1;(unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }struct ModInt(int M_) {import std.conv : to;alias M = M_;int x;this(ModInt a) { x = a.x; }this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }ref ModInt opAssign(long a) { return (this = ModInt(a)); }ref ModInt opOpAssign(string op)(ModInt a) {static if (op == "+") { x += a.x; if (x >= M) x -= M; }else static if (op == "-") { x -= a.x; if (x < 0) x += M; }else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }else static if (op == "/") { this *= a.inv(); }else static assert(false);return this;}ref ModInt opOpAssign(string op)(long a) {static if (op == "^^") {if (a < 0) return (this = inv()^^(-a));ModInt t2 = this, te = ModInt(1);for (long e = a; e > 0; e >>= 1) {if (e & 1) te *= t2;t2 *= t2;}x = cast(int)(te.x);return this;} else return mixin("this " ~ op ~ "= ModInt(a)");}ModInt inv() const {int a = x, b = M, y = 1, z = 0, t;for (; ; ) {t = a / b; a -= t * b;if (a == 0) {assert(b == 1 || b == -1);return ModInt(b * z);}y -= t * z;t = b / a; b -= t * a;if (b == 0) {assert(a == 1 || a == -1);return ModInt(a * y);}z -= t * y;}}ModInt opUnary(string op: "-")() const { return ModInt(-x); }ModInt opBinary(string op, T)(T a) const {return mixin("ModInt(this) " ~ op ~ "= a");}ModInt opBinaryRight(string op)(long a) const {return mixin("ModInt(a) " ~ op ~ "= this");}bool opCast(T: bool)() const { return (x != 0); }string toString() const { return x.to!string; }}enum MO = 998244353;alias Mint = ModInt!MO;int root(int[] uf, int u) {return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));}bool connect(int[] uf, int u, int v) {u = uf.root(u);v = uf.root(v);if (u == v) return false;if (uf[u] > uf[v]) swap(u, v);uf[u] += uf[v];uf[v] = u;return true;}Mint det(Mint[][] a) {const n = cast(int)(a.length);Mint ret = 1;foreach (h; 0 .. n) {if (!a[h][h]) {foreach (i; h + 1 .. n) {if (a[i][h]) {swap(a[h], a[i]);ret += 1;goto found;}}ret = 0;break;found:}ret *= a[h][h];const r = a[h][h].inv;foreach (i; h + 1 .. n) {const s = r * a[i][h];foreach (j; h + 1 .. n) {a[i][j] -= s * a[h][j];}}}return ret;}Mint[] charPoly(Mint[][] a) {const n = cast(int)(a.length);auto ps = new Mint[n + 1];ps[n] = 1;for (int h = n - 1; h >= 0; --h) {// closed walks at h with repetition allowed from 0, ..., h - 1auto sub = new Mint[][](n, h + 1);for (int i = n; i >= 1; --i) {sub[i - 1][h] += ps[i];}for (int i = n - 1; i >= 1; --i) for (int u = 0; u <= h; ++u) {for (int v = 0; v < h; ++v) {sub[i - 1][v] -= sub[i][u] * a[u][v];}}for (int i = n - 1; i >= 0; --i) for (int u = 0; u <= h; ++u) {ps[i] += sub[i][u] * a[u][h];}}for (int i = n - 1; i >= 0; i -= 2) {ps[i] *= -1;}return ps;}void main() {try {for (; ; ) {const N = readInt();const M = readInt();auto A = new int[M];auto B = new int[M];foreach (i; 0 .. M) {A[i] = readInt() - 1;B[i] = readInt() - 1;}auto adj = new bool[][](N, N);foreach (i; 0 .. M) {adj[A[i]][B[i]] = true;adj[B[i]][A[i]] = true;}auto uf = new int[N];uf[] = -1;foreach (i; 0 .. M) {uf.connect(A[i], B[i]);}int[] szs;foreach (r; 0 .. N) {if (uf[r] < 0) {szs ~= -uf[r];}}szs.sort.reverse;int cost = N * (N - 1) / 2;foreach (s; szs) {cost -= s * (s - 1) / 2;}if (szs.length >= 2) {cost -= szs[0] * szs[1];}cost *= 2;auto uss = new int[][N];foreach (u; 0 .. N) {uss[uf.root(u)] ~= u;}Mint ans;if (szs.length >= 2) {if (szs[0] == szs[1]) {const cnt = cast(int)(szs.count(szs[0]));ans = cnt * (cnt - 1) / 2;} else {const cnt = cast(int)(szs.count(szs[1]));ans = cnt;}ans *= szs[0] * szs[1];foreach (r; 0 .. N) {if (uf[r] < 0) {const n = -uf[r];auto a = new Mint[][](n - 1, n - 1);foreach (x; 0 .. n) foreach (y; 0 .. n) {if (adj[uss[r][x]][uss[r][y]]) {if (x < n - 1) {a[x][x] += 1;}if (x < n - 1 && y < n - 1) {a[x][y] -= 1;}}}debug {writeln("a = ", a);}ans *= det(a);}}} else {{auto a = new Mint[][](N - 1, N - 1);foreach (u; 0 .. N) foreach (v; 0 .. N) {if (adj[u][v]) {if (u < N - 1) {a[u][u] += 1;}if (u < N - 1 && v < N - 1) {a[u][v] -= 1;}}}// ans += det(a);const d = det(a);ans += d;ans -= d * (N - 1);}/*foreach (p; 0 .. N) foreach (q; p + 1 .. N) {if (!adj[p][q]) {int[] us;foreach (u; 0 .. N) {if (u != p && u != q) {us ~= u;}}const n = cast(int)(us.length);auto a = new Mint[][](n, n);foreach (x; 0 .. n) foreach (y; 0 .. n) {if (adj[us[x]][us[y]]) {a[x][x] += 1;a[x][y] -= 1;}}foreach (x; 0 .. n) {if (adj[p][us[x]]) a[x][x] += 1;if (adj[q][us[x]]) a[x][x] += 1;}ans += det(a);}}*/{auto a = new Mint[][](N, N);foreach (u; 0 .. N) foreach (v; 0 .. N) {if (adj[u][v]) {a[u][u] += 1;a[u][v] -= 1;}}const fs = charPoly(a);debug {writeln("fs = ", fs);}ans += (-1)^^((N - 2) & 1) * fs[2];}}writeln(cost);writeln(ans);}} catch (EOFException e) {}}