結果

問題 No.1774 Love Triangle (Hard)
ユーザー ygussanyygussany
提出日時 2021-10-03 21:05:13
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 9,577 bytes
コンパイル時間 953 ms
コンパイル使用メモリ 41,600 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-26 20:58:12
合計ジャッジ時間 18,187 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 12 ms
6,944 KB
testcase_03 RE -
testcase_04 AC 10 ms
6,940 KB
testcase_05 AC 22 ms
6,944 KB
testcase_06 AC 4 ms
6,944 KB
testcase_07 RE -
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 99 ms
6,944 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 801 ms
6,940 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 AC 150 ms
6,944 KB
testcase_18 RE -
testcase_19 AC 321 ms
6,940 KB
testcase_20 AC 945 ms
6,940 KB
testcase_21 AC 541 ms
6,944 KB
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 RE -
testcase_59 RE -
testcase_60 RE -
testcase_61 RE -
testcase_62 RE -
testcase_63 RE -
testcase_64 RE -
testcase_65 RE -
testcase_66 RE -
testcase_67 RE -
testcase_68 RE -
testcase_69 RE -
testcase_70 RE -
testcase_71 RE -
testcase_72 RE -
testcase_73 RE -
testcase_74 RE -
testcase_75 RE -
testcase_76 RE -
testcase_77 RE -
testcase_78 RE -
testcase_79 RE -
testcase_80 RE -
testcase_81 RE -
testcase_82 RE -
testcase_83 RE -
testcase_84 RE -
testcase_85 RE -
testcase_86 RE -
testcase_87 RE -
testcase_88 RE -
testcase_89 RE -
testcase_90 RE -
testcase_91 RE -
testcase_92 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

#define DIM 200
const int Mod[3] = {10007, 19997, 30011};

int div_mod(int x, int y, int z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

int rank_matrix(int d, int A[][DIM], int m)
{
	if (d <= 0) return 1;
	
	static int i, j, k, r, p[DIM], X[DIM][DIM], inv, tmp[DIM];
	for (i = 0; i < d; i++) p[i] = i;
	for (i = 0; i < d; i++) {
		for (j = 0; j < d; j++) {
			X[i][j] = A[i][j] % Mod[m];
			if (X[i][j] < 0) X[i][j] += Mod[m];
		}
	}
	for (i = 0, r = 0; i < d; i++) {
		for (j = r; j < d; j++) if (X[i][p[j]] != 0) break;
		if (j == d) continue;
		else if (j != r) {
			p[r] ^= p[j];
			p[j] ^= p[r];
			p[r] ^= p[j];
		}
		inv = div_mod(1, X[i][p[r]], Mod[m]);
		for (k = i; k < d; k++) tmp[k] = X[k][p[r]] * inv % Mod[m];
		for (j = r + 1; j < d; j++) {
			if (X[i][p[j]] != 0) {
				for (k = i + 1; k < d; k++) {
					X[k][p[j]] -= tmp[k] * X[i][p[j]] % Mod[m];
					if (X[k][p[j]] < 0) X[k][p[j]] += Mod[m];
				}
				X[i][p[j]] = 0;
			}
		}
		r++;
	}
	return r;
}

int inv_matrix(int d, int A[][DIM], int A_inv[][DIM], int m)
{
	static int i, j, k, p[DIM], X[DIM][DIM*2], inv;
	int dd = d * 2;
	for (i = 0; i < d; i++) p[i] = i;
	for (i = 0; i < d; i++) {
		for (j = 0; j < d; j++) {
			X[i][j] = A[i][j];
			X[i][j+d] = (j == i)? 1: 0;
		}
	}
	for (i = 0; i < d; i++) {
		for (k = i; k < d; k++) if (X[p[k]][i] != 0) break;
		if (k == d) return -1;
		else if (k != i) {
			p[i] ^= p[k];
			p[k] ^= p[i];
			p[i] ^= p[k];
		}
		inv = div_mod(1, X[p[i]][i], Mod[m]);
		for (j = i; j < dd; j++) X[p[i]][j] = X[p[i]][j] * inv % Mod[m];
		for (k = 0; k < d; k++) {
			if (k == i) continue;
			if (X[p[k]][i] != 0) {
				for (j = i + 1; j < dd; j++) {
					X[p[k]][j] -= X[p[k]][i] * X[p[i]][j] % Mod[m];
					if (X[p[k]][j] < 0) X[p[k]][j] += Mod[m];
				}
				X[p[k]][i] = 0;
			}
		}
	}
	for (i = 0; i < d; i++) for (j = 0; j < d; j++) A_inv[i][j] = X[p[i]][j+d];
	return 0;
}

// A = B
void copy_matrix(int d1, int d2, int A[][DIM], int B[][DIM])
{
	int i, j;
	for (i = 0; i < d1; i++) for (j = 0; j < d2; j++) A[i][j] = B[i][j];
}

// A += cB
void add_matrix(int d, int A[][DIM], int c, int B[][DIM], int m)
{
	int i, j;
	for (i = 0; i < d; i++) {
		for (j = 0; j < d; j++) {
			A[i][j] += B[i][j] * c % Mod[m];
			if (A[i][j] >= Mod[m]) A[i][j] -= Mod[m];
		}
	}
}

// A = A * B
void multiply_matrix_right(int d1, int d2, int d3, int A[][DIM], int B[][DIM], int m)
{
	static int i, j, k, C[DIM][DIM];
	for (i = 0; i < d1; i++) {
		for (j = 0; j < d3; j++) {
			for (k = 0, C[i][j] = 0; k < d2; k++) C[i][j] += A[i][k] * B[k][j] % Mod[m];
			C[i][j] %= Mod[m];
		}
	}
	for (i = 0; i < d1; i++) for (j = 0; j < d3; j++) A[i][j] = C[i][j];
}

// A = B * A
void multiply_matrix_left(int d1, int d2, int d3, int B[][DIM], int A[][DIM], int m)
{
	static int i, j, k, C[DIM][DIM];
	for (i = 0; i < d1; i++) {
		for (j = 0; j < d3; j++) {
			for (k = 0, C[i][j] = 0; k < d2; k++) C[i][j] += B[i][k] * A[k][j] % Mod[m];
			C[i][j] %= Mod[m];
		}
	}
	for (i = 0; i < d1; i++) for (j = 0; j < d3; j++) A[i][j] = C[i][j];
}

int SMW_rank2_update_inv(int d, int A_inv[][DIM], int U[][DIM], int VT[][DIM], int m)
{
	static int I[DIM][DIM] = {{1, 0}, {0, 1}}, B[DIM][DIM], B_inv[DIM][DIM];
	int i, j;
	copy_matrix(d, d, B, A_inv);
	multiply_matrix_left(2, d, d, VT, B, m);
	multiply_matrix_right(2, d, 2, B, U, m);
	add_matrix(2, B, 1, I, m);
	if (inv_matrix(2, B, B_inv, m) != 0) return -1;
	multiply_matrix_left(d, d, 2, A_inv, U, m);
	multiply_matrix_right(2, d, d, VT, A_inv, m);
	multiply_matrix_left(d, 2, 2, U, B_inv, m);
	multiply_matrix_right(d, 2, d, B_inv, VT, m);
	add_matrix(d, A_inv, Mod[m] - 1, B_inv, m);
	return 0;
}

void wedge_prod_vector(int d, int b[], int c[], int A[][DIM], int m)
{
	int i, j;
	for (i = 0; i < d; i++) {
		for (j = 0; j < d; j++) {
			A[i][j] = (b[i] * c[j] - c[i] * b[j]) % Mod[m];
			if (A[i][j] >= Mod[m]) A[i][j] -= Mod[m];
			else if (A[i][j] < 0) A[i][j] += Mod[m];
		}
	}
}

#define MT_N 624
#define MT_M 397
#define MT_MATRIX_A 0x9908b0dfUL
#define MT_UPPER_MASK 0x80000000UL
#define MT_LOWER_MASK 0x7fffffffUL

static unsigned int mt[MT_N];
static int mti = MT_N + 1;

void init_genrand(unsigned int s)
{
    mt[0] = s & 0xffffffffUL;
    for (mti = 1; mti < MT_N; mti++) {
        mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
        mt[mti] &= 0xffffffffUL;
    }
}

unsigned int genrand()
{
    unsigned int y;
    static unsigned int mag01[2] = {0x0UL, MT_MATRIX_A};

    if (mti >= MT_N) {
        int kk;
        if (mti == MT_N + 1) init_genrand(5489UL);
		
        for (kk = 0; kk < MT_N - MT_M; kk++) {
            y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK);
            mt[kk] = mt[kk+MT_M] ^ (y >> 1) ^ mag01[y&0x1UL];
        }
        for (; kk < MT_N - 1; kk++) {
            y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK);
            mt[kk] = mt[kk+(MT_M-MT_N)] ^ (y >> 1) ^ mag01[y&0x1UL];
        }
        y = (mt[MT_N-1] & MT_UPPER_MASK) | (mt[0] & MT_LOWER_MASK);
        mt[MT_N-1] = mt[MT_M-1] ^ (y >> 1) ^ mag01[y&0x1UL];

        mti = 0;
    }
  
    y = mt[mti++];

    y ^= (y >> 11);
    y ^= (y << 7) & 0x9d2c5680UL;
    y ^= (y << 15) & 0xefc60000UL;
    y ^= (y >> 18);

    return y;
}

void chmax(int* a, int b)
{
	if (*a < b) *a = b;
}

void naive(int N, int M, int u[], int v[], int w[])
{
	int i, j, m, ans = 0, Y[3][DIM][DIM] = {}, r;
	for (i = 1; i <= M; i++) {
		for (m = 0; m < 3; m++) {
			r = genrand() % Mod[m];
			Y[m][u[i]][v[i]] += r;
			if (Y[m][u[i]][v[i]] >= Mod[m]) Y[m][u[i]][v[i]] -= Mod[m];
			Y[m][u[i]][w[i]] -= r;
			if (Y[m][u[i]][w[i]] < 0) Y[m][u[i]][w[i]] += Mod[m];
			Y[m][v[i]][u[i]] -= r;
			if (Y[m][v[i]][u[i]] < 0) Y[m][v[i]][u[i]] += Mod[m];
			Y[m][v[i]][w[i]] += r;
			if (Y[m][v[i]][w[i]] >= Mod[m]) Y[m][v[i]][w[i]] -= Mod[m];
			Y[m][w[i]][u[i]] += r;
			if (Y[m][w[i]][u[i]] >= Mod[m]) Y[m][w[i]][u[i]] -= Mod[m];
			Y[m][w[i]][v[i]] -= r;
			if (Y[m][w[i]][v[i]] < 0) Y[m][w[i]][v[i]] += Mod[m];
			chmax(&ans, rank_matrix(N, Y[m], m));
		}
		printf("%d\n", ans / 2);
	}
}

void solve(int N, int M, int u[], int v[], int w[])
{
	int i, j, k, m, r, ans[601] = {}, x[601], Y[DIM][DIM], Y_inv[DIM][DIM], Z[DIM][DIM], b[DIM], c[DIM], U[DIM][DIM], VT[DIM][DIM];
	if (N % 2 != 0) N++;
	for (m = 0; m < 3; m++) {
		for (i = 0; i < N; i++) for (j = 0; j < N; j++) Y[i][j] = 0;
		for (i = 1; i <= M; i++) {
			r = genrand() % Mod[m];
			x[i] = r;
			Y[u[i]][v[i]] += r;
			if (Y[u[i]][v[i]] >= Mod[m]) Y[u[i]][v[i]] -= Mod[m];
			Y[u[i]][w[i]] -= r;
			if (Y[u[i]][w[i]] < 0) Y[u[i]][w[i]] += Mod[m];
			Y[v[i]][u[i]] -= r;
			if (Y[v[i]][u[i]] < 0) Y[v[i]][u[i]] += Mod[m];
			Y[v[i]][w[i]] += r;
			if (Y[v[i]][w[i]] >= Mod[m]) Y[v[i]][w[i]] -= Mod[m];
			Y[w[i]][u[i]] += r;
			if (Y[w[i]][u[i]] >= Mod[m]) Y[w[i]][u[i]] -= Mod[m];
			Y[w[i]][v[i]] -= r;
			if (Y[w[i]][v[i]] < 0) Y[w[i]][v[i]] += Mod[m];
		}
		r = rank_matrix(N, Y, m);
		chmax(&(ans[M]), r / 2);
		
		for (k = 0; k * 2 + r < N; k++) {
			for (j = 0; j < N; j++) {
				b[j] = genrand() % Mod[m];
				c[j] = genrand() % Mod[m];
			}
			wedge_prod_vector(N, b, c, Z, m);
			add_matrix(N, Y, genrand() % Mod[m], Z, m);
		}
		if (inv_matrix(N, Y, Y_inv, m) != 0) {
			m--;
			continue;
		}
		
		for (i = M, k = r / 2; i > 1; i--) {
			for (j = 0; j < N; j++) {
				U[j][0] = 0;
				U[j][1] = 0;
				VT[0][j] = 0;
				VT[1][j] = 0;
			}
			U[u[i]][0] = x[i];
			U[v[i]][0] = Mod[m] - x[i];
			U[u[i]][1] = x[i];
			U[w[i]][1] = Mod[m] - x[i];
			VT[0][u[i]] = Mod[m] - 1;
			VT[0][w[i]] = 1;
			VT[1][u[i]] = 1;
			VT[1][v[i]] = Mod[m] - 1;
			if (SMW_rank2_update_inv(N, Y_inv, U, VT, m) != 0) {
				chmax(&(ans[i-1]), ans[i] - 1);
				do {
					r = genrand() % Mod[m];
					for (j = 0; j < N; j++) {
						b[j] = genrand() % Mod[m];
						c[j] = genrand() % Mod[m];
						U[j][0] = b[j] * r % Mod[m];
						U[j][1] = c[j] * r % Mod[m];
						VT[0][j] = c[j];
						VT[1][j] = Mod[m] - b[j];
					}
					if (SMW_rank2_update_inv(N, Y_inv, U, VT, m) != 0) continue;
					
					for (j = 0; j < N; j++) {
						U[j][0] = 0;
						U[j][1] = 0;
						VT[0][j] = 0;
						VT[1][j] = 0;
					}
					U[u[i]][0] = x[i];
					U[v[i]][0] = Mod[m] - x[i];
					U[u[i]][1] = x[i];
					U[w[i]][1] = Mod[m] - x[i];
					VT[0][u[i]] = Mod[m] - 1;
					VT[0][w[i]] = 1;
					VT[1][u[i]] = 1;
					VT[1][v[i]] = Mod[m] - 1;
					if (SMW_rank2_update_inv(N, Y_inv, U, VT, m) == 0) break;
					
					for (j = 0; j < N; j++) {
						U[j][0] = b[j] * r % Mod[m];
						U[j][1] = c[j] * r % Mod[m];
						VT[0][j] = Mod[m] - c[j];
						VT[1][j] = b[j];
					}
					SMW_rank2_update_inv(N, Y_inv, U, VT, m);
				} while (1);
				wedge_prod_vector(N, b, c, Z, m);
				add_matrix(N, Y, r, Z, m);
				k--;
			}
			chmax(&(ans[i-1]), k);
			
			r = x[i];
			Y[u[i]][v[i]] -= r;
			if (Y[u[i]][v[i]] < 0) Y[u[i]][v[i]] += Mod[m];
			Y[u[i]][w[i]] += r;
			if (Y[u[i]][w[i]] >= Mod[m]) Y[u[i]][w[i]] -= Mod[m];
			Y[v[i]][u[i]] += r;
			if (Y[v[i]][u[i]] >= Mod[m]) Y[v[i]][u[i]] -= Mod[m];
			Y[v[i]][w[i]] -= r;
			if (Y[v[i]][w[i]] < 0) Y[v[i]][w[i]] += Mod[m];
			Y[w[i]][u[i]] -= r;
			if (Y[w[i]][u[i]] < 0) Y[w[i]][u[i]] += Mod[m];
			Y[w[i]][v[i]] += r;
			if (Y[w[i]][v[i]] >= Mod[m]) Y[w[i]][v[i]] -= Mod[m];
		}
	}
	for (i = 1; i <= M; i++) printf("%d\n", ans[i]);
}

int main()
{
    init_genrand(46);
	int i, N, M, u[601], v[601], w[601];
	scanf("%d %d", &N, &M);
	for (i = 1; i <= M; i++) scanf("%d %d %d", &(u[i]), &(v[i]), &(w[i]));
	solve(N, M, u, v, w);
	fflush(stdout);
	return 0;
}
0