結果

問題 No.1775 Love Triangle 2
ユーザー ygussanyygussany
提出日時 2021-11-19 23:52:02
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 3,710 ms / 8,000 ms
コード長 5,028 bytes
コンパイル時間 626 ms
コンパイル使用メモリ 35,072 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-04 17:19:41
合計ジャッジ時間 20,321 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2,796 ms
5,376 KB
testcase_05 AC 3,696 ms
5,376 KB
testcase_06 AC 3,710 ms
5,376 KB
testcase_07 AC 471 ms
5,376 KB
testcase_08 AC 3,471 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 8 ms
5,376 KB
testcase_16 AC 8 ms
5,376 KB
testcase_17 AC 14 ms
5,376 KB
testcase_18 AC 53 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 4 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 5 ms
5,376 KB
testcase_26 AC 5 ms
5,376 KB
testcase_27 AC 4 ms
5,376 KB
testcase_28 AC 6 ms
5,376 KB
testcase_29 AC 94 ms
5,376 KB
testcase_30 AC 143 ms
5,376 KB
testcase_31 AC 221 ms
5,376 KB
testcase_32 AC 209 ms
5,376 KB
testcase_33 AC 147 ms
5,376 KB
testcase_34 AC 148 ms
5,376 KB
testcase_35 AC 97 ms
5,376 KB
testcase_36 AC 46 ms
5,376 KB
testcase_37 AC 15 ms
5,376 KB
testcase_38 AC 3 ms
5,376 KB
testcase_39 AC 116 ms
5,376 KB
testcase_40 AC 190 ms
5,376 KB
testcase_41 AC 187 ms
5,376 KB
testcase_42 AC 139 ms
5,376 KB
testcase_43 AC 84 ms
5,376 KB
testcase_44 AC 62 ms
5,376 KB
testcase_45 AC 101 ms
5,376 KB
testcase_46 AC 26 ms
5,376 KB
testcase_47 AC 6 ms
5,376 KB
testcase_48 AC 5 ms
5,376 KB
testcase_49 AC 40 ms
5,376 KB
testcase_50 AC 125 ms
5,376 KB
testcase_51 AC 207 ms
5,376 KB
testcase_52 AC 136 ms
5,376 KB
testcase_53 AC 107 ms
5,376 KB
testcase_54 AC 121 ms
5,376 KB
testcase_55 AC 74 ms
5,376 KB
testcase_56 AC 43 ms
5,376 KB
testcase_57 AC 14 ms
5,376 KB
testcase_58 AC 6 ms
5,376 KB
testcase_59 AC 86 ms
5,376 KB
testcase_60 AC 148 ms
5,376 KB
testcase_61 AC 136 ms
5,376 KB
testcase_62 AC 61 ms
5,376 KB
testcase_63 AC 35 ms
5,376 KB
testcase_64 AC 33 ms
5,376 KB
testcase_65 AC 9 ms
5,376 KB
testcase_66 AC 8 ms
5,376 KB
testcase_67 AC 5 ms
5,376 KB
testcase_68 AC 3 ms
5,376 KB
testcase_69 AC 3 ms
5,376 KB
testcase_70 AC 4 ms
5,376 KB
testcase_71 AC 5 ms
5,376 KB
testcase_72 AC 5 ms
5,376 KB
testcase_73 AC 5 ms
5,376 KB
testcase_74 AC 5 ms
5,376 KB
testcase_75 AC 5 ms
5,376 KB
testcase_76 AC 5 ms
5,376 KB
testcase_77 AC 4 ms
5,376 KB
testcase_78 AC 5 ms
5,376 KB
testcase_79 AC 4 ms
5,376 KB
testcase_80 AC 5 ms
5,376 KB
testcase_81 AC 6 ms
5,376 KB
testcase_82 AC 6 ms
5,376 KB
testcase_83 AC 7 ms
5,376 KB
testcase_84 AC 5 ms
5,376 KB
testcase_85 AC 7 ms
5,376 KB
testcase_86 AC 5 ms
5,376 KB
testcase_87 AC 6 ms
5,376 KB
testcase_88 AC 8 ms
5,376 KB
testcase_89 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

#define N_MAX 100
#define M_MAX 5000



typedef struct Edge {
	struct Edge *next;
	int v;
	unsigned int label;
} edge;

void chmin(int* a, int b)
{
	if (*a > b) *a = b;
}



#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;
}



#define POWX 3 // 3 -> 2^8, 4 -> 2^16, 5 -> 2^32
const unsigned int powd[5] = {2, 4, 16, 256, 65536}, powe[5] = {1, 2, 4, 8, 16};

// Multiplication on a finite field of size 2^32 with XOR addition
unsigned int nim_product(unsigned int A, unsigned int B)
{
	if (A > B) return nim_product(B, A);
	else if (A <= 1) return A * B;
	
	static unsigned int memo[256][256] = {};
	if (B < 256 && memo[A][B] != 0) return memo[A][B];
	
	int i;
	for (i = 0; i < POWX; i++) {
		if (B == powd[i]) {
			if (A == powd[i]) return (B >> 1) * 3;
			else return A * B;
		}
	}

	unsigned int a[2], b[2], ans[2][2];
	for (i = POWX - 1; i >= 0; i--) if (B > powd[i]) break;
	a[1] = A & (powd[i] - 1);
	a[0] = (A ^ a[1]) >> powe[i];
	b[1] = B & (powd[i] - 1);
	b[0] = (B ^ b[1]) >> powe[i];
	ans[0][0] = nim_product(a[0], b[0]);
	ans[0][1] = nim_product(a[0], b[1]);
	ans[1][0] = nim_product(a[1], b[0]);
	ans[1][1] = nim_product(a[1], b[1]);
	if (B < 256) {
		memo[A][B] = (ans[0][0] ^ ans[0][1] ^ ans[1][0]) * powd[i] ^ nim_product(ans[0][0], powd[i] >> 1) ^ ans[1][1];
		return memo[A][B];
	} else return (ans[0][0] ^ ans[0][1] ^ ans[1][0]) * powd[i] ^ nim_product(ans[0][0], powd[i] >> 1) ^ ans[1][1];
}



// Solution example (O(N^4) time by splitting X)
int solve4_1(int N, int M, int X, int Y, int Z, int A[], int B[])
{
	static int i, u, w, adj_mat[N_MAX + 1][N_MAX + 1];
	static edge *adj[N_MAX + 1], e[M_MAX * 2], *p, *pp;
	for (u = 1; u <= N; u++) for (w = u + 1; w <= N; w++) adj_mat[u][w] = 0;
	for (i = 1; i <= M; i++) {
		u = A[i];
		w = B[i];
		adj_mat[u][w] = 1;
	}
	for (u = 1; u <= N; u++) adj[u] = NULL;
	for (u = 1, i = 0; u <= N; u++) {
		for (w = u + 1; w <= N; w++) {
			if (adj_mat[u][w] != 0) continue;
			e[i].v = w;
			e[i].label = genrand() % (powd[POWX] - 1) + 1;
			e[i].next = adj[u];
			adj[u] = &(e[i++]);
			e[i].v = u;
			e[i].label = (u == X || w == X)? genrand() % (powd[POWX] - 1) + 1: e[i-1].label;
			e[i].next = adj[w];
			adj[w] = &(e[i++]);
		}
	}
	
	static int k, kk, l, x, y, z, cur, prev;
	static unsigned int dp[2][4][N_MAX + 1][N_MAX + 1], tmp;
	for (k = 0; k < 4; k++) {
		for (u = 1; u <= N; u++) {
			for (p = adj[u]; p != NULL; p = p->next) {
				dp[0][k][u][p->v] = 0;
				dp[1][k][u][p->v] = 0;
			}
		}
	}
	for (p = adj[X]; p != NULL; p = p->next) {
		u = p->v;
		k = (u == Y)? 1: (u == Z)? 2: 0;
		dp[0][k][X][u] = p->label;
	}
	for (l = 2, cur = 1, prev = 0; l <= N; l++, cur ^= 1, prev ^= 1) {
		for (k = 0; k < 4; k++) {
			for (x = 1; x <= N; x++) {
				for (pp = adj[x]; pp != NULL; pp = pp->next) {
					y = pp->v;
					if (dp[prev][k][x][y] == 0) continue;
					tmp = dp[prev][k][x][y];
					dp[prev][k][x][y] = 0;
					
					for (p = adj[y]; p != NULL; p = p->next) {
						z = p->v;
						kk = k | ((z == Y)? 1: (z == Z)? 2: 0);
						if (((y == X || y == Y || y == Z) && z == x) || (z == X && kk != 3) || (z == Y && (k & 1) != 0) || (z == Z && (k & 2) != 0)) continue;
						dp[cur][kk][y][z] ^= nim_product(tmp, p->label);
					}
				}
			}
		}
		for (p = adj[X]; p != NULL; p = p->next) if (dp[cur][3][p->v][X] != 0) break;
		if (p != NULL) break;
	}
	if (l > N) return -1;
	else return l;
}



int main()
{
    init_genrand(1);
	int i, N, M, X, Y, Z, A[M_MAX + 1], B[M_MAX + 1], ans[2];
	scanf("%d %d", &N, &M);
	scanf("%d %d %d", &X, &Y, &Z);
	for (i = 1; i <= M; i++) scanf("%d %d", &(A[i]), &(B[i]));
	ans[0] = solve4_1(N, M, X, Y, Z, A, B);
	// ans[1] = solve4_1(N, M, X, Y, Z, A, B);
	// chmin(&(ans[0]), ans[1]);
	printf("%d\n", (ans[0] > N)? -1: ans[0]);
	fflush(stdout);
	return 0;
}
0