結果
問題 | No.310 2文字しりとり |
ユーザー |
|
提出日時 | 2015-12-03 03:27:43 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 7,673 bytes |
コンパイル時間 | 5,643 ms |
コンパイル使用メモリ | 84,028 KB |
実行使用メモリ | 202,092 KB |
最終ジャッジ日時 | 2024-09-14 08:18:10 |
合計ジャッジ時間 | 14,199 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 TLE * 1 -- * 6 |
ソースコード
package q8xx;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.util.Arrays;import java.util.InputMismatchException;// O(N^3)public class Q874 {InputStream is;PrintWriter out;String INPUT = "";boolean[][] shrink(boolean[][] ng){int n = ng.length;int[] outdeg = new int[n];int[] indeg = new int[n];for(int i = 0;i < n;i++){for(int j = 0;j < n;j++){if(!ng[i][j]){outdeg[i]++;indeg[j]++;}}}int[] map = new int[n];Arrays.fill(map, -1);int p = 0;for(int i = 0;i < n;i++){if(!(outdeg[i] == 0 && indeg[i] == 0)){map[i] = p++;}}boolean[][] nng = new boolean[p][p];for(int i = 0;i < n;i++){for(int j = 0;j < n;j++){if(ng[i][j] && map[i] != -1 && map[j] != -1){nng[map[i]][map[j]] = true;}}}return nng;}int mod = 1000000007;void solve(){int n = ni(), m = ni();if(n*n == m){out.println(1);return;}int on = n;boolean[][] ng = new boolean[n][n];for(int i = 0;i < m;i++){int f = ni()-1, t = ni()-1;ng[f][t] = true;}ng = shrink(ng);n = ng.length;int[] outdeg = new int[n];int[] indeg = new int[n];for(int i = 0;i < n;i++){for(int j = 0;j < n;j++){if(!ng[i][j]){outdeg[i]++;indeg[j]++;}}}// connectivityDJSet ds = new DJSet(n);for(int i = 0;i < n;i++){for(int j = 0;j < n;j++){if(!ng[i][j]){ds.union(i, j);}}}for(int i = 0;i < n;i++){for(int j = i+1;j < n;j++){if((outdeg[i] >= 1 || indeg[i] >= 1) &&(outdeg[j] >= 1 || indeg[j] >= 1)){if(!ds.equiv(i, j)){out.println(0);return;}}}}int src = -1, sink = -1;for(int i = 0;i < n;i++){if(outdeg[i] == indeg[i])continue;if(outdeg[i] == indeg[i]+1 && src == -1){src = i;continue;}if(outdeg[i] == indeg[i]-1 && sink == -1){sink = i;continue;}out.println(0);return;}int[][] M = new int[n][n];for(int i = 0;i < n;i++){for(int j = 0;j < n;j++){if(!ng[i][j]){if(i != j){M[i][j] = mod-1;M[i][i]++;}}}}if(src == -1){out.println(numEulerianCircuit(M, outdeg, indeg) * (on*on-m) % mod);}else{if(--M[sink][src] < 0)M[sink][src] += mod;M[sink][sink]++;outdeg[sink]++;indeg[src]++;long nec = numEulerianCircuit(M, outdeg, indeg);out.println(nec * invl(mod-M[sink][src], mod) % mod);}}long numEulerianCircuit(int[][] M, int[] outdeg, int[] indeg){int n = M.length;long[] F = new long[n+2];F[0] = 1;for(int i = 1;i <= n;i++)F[i] = F[i-1] * i % mod;long ec = 1;for(int i = 0;i < n;i++){ec = ec * F[outdeg[i]-1] % mod;}int[][] Q = new int[n-1][n-1];for(int i = 0;i < n-1;i++){for(int j = 0;j < n-1;j++){Q[i][j] = M[i][j];}}long det = det(Q, mod, 1);return ec * det % mod;}public static class DJSet {public int[] upper;public DJSet(int n) {upper = new int[n];Arrays.fill(upper, -1);}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 int count() {int ct = 0;for (int u : upper)if (u < 0)ct++;return ct;}}public static long det(int[][] a, int p, int e){int n = a.length;long ret = 1;int mod = 1;for(int i = 1;i <= e;i++)mod *= p;for(int i = 0;i < n;i++){int pivot = -1;int mine = 99;for(int j = i;j < n;j++){if(a[j][i] == 0)continue;int le = 0;int w = a[j][i];while(w % p == 0){w /= p;le++;}if(le < mine){mine = le;pivot = j;}}if(pivot == -1)return 0L;// swap rowsif(i != pivot){{int[] d = a[i]; a[i] = a[pivot]; a[pivot] = d;}ret = mod-ret;}// p^f*x// p^g*y=p^f*(p^(g-f)*y*x^-1// y/x*p^(g-f)// p^minelong pmine = 1;for(int j = 1;j <= mine;j++)pmine *= p;long inv = invl(a[i][i]/pmine, mod);for(int r = i+1;r < n;r++){if(a[r][i] != 0){long mul = -a[r][i]/pmine * inv % mod;if(mul < 0)mul += mod;for(int c = i+1;c < n;c++){a[r][c] = (int)((a[r][c] + a[i][c] * mul) % mod);}}}ret = ret * a[i][i] % mod;}if(ret < 0)ret += mod;return ret;}public 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;}void run() throws Exception{is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());out = new PrintWriter(System.out);long s = System.currentTimeMillis();solve();out.flush();if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");// Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){// @Override// public void run() {// long s = System.currentTimeMillis();// solve();// out.flush();// if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");// }// };// t.start();// t.join();}public static void main(String[] args) throws Exception { new Q874().run(); }private byte[] inbuf = new byte[1024];private int lenbuf = 0, ptrbuf = 0;private int readByte(){if(lenbuf == -1)throw new InputMismatchException();if(ptrbuf >= lenbuf){ptrbuf = 0;try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }if(lenbuf <= 0)return -1;}return inbuf[ptrbuf++];}private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }private double nd() { return Double.parseDouble(ns()); }private char nc() { return (char)skip(); }private String ns(){int b = skip();StringBuilder sb = new StringBuilder();while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')sb.appendCodePoint(b);b = readByte();}return sb.toString();}private char[] ns(int n){char[] buf = new char[n];int b = skip(), p = 0;while(p < n && !(isSpaceChar(b))){buf[p++] = (char)b;b = readByte();}return n == p ? buf : Arrays.copyOf(buf, p);}private int[] na(int n){int[] a = new int[n];for(int i = 0;i < n;i++)a[i] = ni();return a;}private long[] nal(int n){long[] a = new long[n];for(int i = 0;i < n;i++)a[i] = nl();return a;}private char[][] nm(int n, int m) {char[][] map = new char[n][];for(int i = 0;i < n;i++)map[i] = ns(m);return map;}private int[][] nmi(int n, int m) {int[][] map = new int[n][];for(int i = 0;i < n;i++)map[i] = na(m);return map;}private int ni() { return (int)nl(); }private long nl(){long num = 0;int b;boolean minus = false;while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));if(b == '-'){minus = true;b = readByte();}while(true){if(b >= '0' && b <= '9'){num = num * 10 + (b - '0');}else{return minus ? -num : num;}b = readByte();}}private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }}