結果

問題 No.1774 Love Triangle (Hard)
ユーザー 👑 ygussanyygussany
提出日時 2021-10-03 22:45:20
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,270 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 95,216 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-10-17 12:25:26
合計ジャッジ時間 20,859 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>

#define DIM 500
#define M_MAX 1000
const int Mod = 19997;

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])
{
	if (d <= 0) return 1;
	
	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;
			if (X[i][j] < 0) X[i][j] += Mod;
		}
	}
	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);
		for (k = i; k < d; k++) tmp[k] = X[k][p[r]] * inv % Mod;
		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;
					if (X[k][p[j]] < 0) X[k][p[j]] += Mod;
				}
				X[i][p[j]] = 0;
			}
		}
		r++;
	}
	return r;
}

#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, ans = 0, Y[DIM][DIM] = {}, r;
	for (i = 1; i <= M; i++) {
		r = genrand() % Mod;
		Y[u[i]][v[i]] += r;
		if (Y[u[i]][v[i]] >= Mod) Y[u[i]][v[i]] -= Mod;
		Y[u[i]][w[i]] -= r;
		if (Y[u[i]][w[i]] < 0) Y[u[i]][w[i]] += Mod;
		Y[v[i]][u[i]] -= r;
		if (Y[v[i]][u[i]] < 0) Y[v[i]][u[i]] += Mod;
		Y[v[i]][w[i]] += r;
		if (Y[v[i]][w[i]] >= Mod) Y[v[i]][w[i]] -= Mod;
		Y[w[i]][u[i]] += r;
		if (Y[w[i]][u[i]] >= Mod) Y[w[i]][u[i]] -= Mod;
		Y[w[i]][v[i]] -= r;
		if (Y[w[i]][v[i]] < 0) Y[w[i]][v[i]] += Mod;
		chmax(&ans, rank_matrix(N, Y));
		printf("%d\n", ans / 2);
	}
}

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