結果

問題 No.310 2文字しりとり
ユーザー uwiuwi
提出日時 2015-12-03 06:09:24
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 8,079 bytes
コンパイル時間 6,995 ms
コンパイル使用メモリ 80,068 KB
実行使用メモリ 273,500 KB
最終ジャッジ日時 2023-10-12 09:24:24
合計ジャッジ時間 15,193 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 42 ms
49,400 KB
testcase_02 AC 42 ms
49,668 KB
testcase_03 AC 43 ms
49,360 KB
testcase_04 AC 43 ms
49,564 KB
testcase_05 AC 41 ms
49,488 KB
testcase_06 AC 43 ms
49,456 KB
testcase_07 AC 40 ms
49,364 KB
testcase_08 AC 43 ms
49,464 KB
testcase_09 AC 43 ms
49,464 KB
testcase_10 AC 43 ms
49,408 KB
testcase_11 AC 42 ms
49,368 KB
testcase_12 AC 43 ms
49,196 KB
testcase_13 AC 43 ms
49,188 KB
testcase_14 AC 42 ms
49,484 KB
testcase_15 AC 43 ms
49,328 KB
testcase_16 AC 41 ms
49,376 KB
testcase_17 AC 43 ms
49,652 KB
testcase_18 AC 52 ms
49,476 KB
testcase_19 AC 53 ms
50,108 KB
testcase_20 AC 84 ms
52,144 KB
testcase_21 AC 850 ms
272,988 KB
testcase_22 AC 998 ms
273,500 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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_3 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
//	String INPUT = "4 7 1 2 2 1 2 3 3 2 2 4 4 2 2 2";
	
	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]++;
				}
			}
		}
		
		// connectivity
		DJSet 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]++;
					}
				}
			}
		}
		
//		for(int[] row : M){
//			tr(row);
//		}
		
		int[][] MM = new int[n+1][n+1];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				MM[i+1][j+1] = M[i][j]+1;
				if(MM[i+1][j+1] == mod)MM[i+1][j+1] = 0;
			}
		}
		MM[0][0] = 1;
		for(int i = 1;i < n+1;i++)MM[0][i] = mod-1;
		for(int i = 1;i < n+1;i++)MM[i][0] = mod-1;
		
		for(int i = 0;i < n+1;i++){
			long s = 0;
			for(int j = 1;j < n;j++)s += MM[i][j];
			s %= mod;
			s = s * invl(n, mod) % mod;
			MM[i][0] += s;
			if(MM[i][0] >= mod)MM[i][0] -= mod;
		}
//		for(int[] row : MM){
//			tr(row);
//		}
		
		
		if(src == -1){
			out.println(numEulerianCircuit(MM, outdeg, indeg) * (on*on-m) % mod);
		}else{
			if(--MM[sink+1][src+1] < 0)MM[sink+1][src+1] += mod;
			if(--M[sink][src] < 0)M[sink][src] += mod;
			MM[sink+1][sink+1]++;
			outdeg[sink]++;
			indeg[src]++;
			long nec = numEulerianCircuit(MM, 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 < outdeg.length;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];
			}
		}
		
//		tr(n-1);
		long det = det(Q, mod);
//		tr(det);
		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 mod)
	{
		int n = a.length;
		long ret = 1;
		
		for(int i = 0;i < n;i++){
			int pivot = -1;
			for(int j = i;j < n;j++){
				if(a[j][i] == 0)continue;
				pivot = j;
				break;
			}
			if(pivot == -1)return 0L;
			// swap rows
			if(i != pivot){
				{int[] d = a[i]; a[i] = a[pivot]; a[pivot] = d;}
				ret = mod-ret;
			}
			
			long inv = invl(a[i][i], mod);
			for(int r = i+1;r < n;r++){
				if(a[r][i] != 0){
//					tr(i, r);
					long mul = -a[r][i] * 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_3().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)); }
}
0