結果

問題 No.1775 Love Triangle 2
ユーザー 👑 ygussanyygussany
提出日時 2021-11-28 14:48:55
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 307 ms / 8,000 ms
コード長 5,891 bytes
コンパイル時間 398 ms
コンパイル使用メモリ 34,160 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-16 22:26:07
合計ジャッジ時間 5,072 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 307 ms
4,380 KB
testcase_05 AC 303 ms
4,380 KB
testcase_06 AC 241 ms
4,376 KB
testcase_07 AC 102 ms
4,376 KB
testcase_08 AC 287 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 6 ms
4,376 KB
testcase_16 AC 6 ms
4,376 KB
testcase_17 AC 7 ms
4,376 KB
testcase_18 AC 12 ms
4,376 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 5 ms
4,376 KB
testcase_26 AC 4 ms
4,376 KB
testcase_27 AC 4 ms
4,380 KB
testcase_28 AC 5 ms
4,380 KB
testcase_29 AC 13 ms
4,380 KB
testcase_30 AC 21 ms
4,376 KB
testcase_31 AC 33 ms
4,376 KB
testcase_32 AC 35 ms
4,380 KB
testcase_33 AC 32 ms
4,376 KB
testcase_34 AC 31 ms
4,376 KB
testcase_35 AC 26 ms
4,376 KB
testcase_36 AC 19 ms
4,376 KB
testcase_37 AC 12 ms
4,380 KB
testcase_38 AC 4 ms
4,376 KB
testcase_39 AC 17 ms
4,376 KB
testcase_40 AC 28 ms
4,376 KB
testcase_41 AC 31 ms
4,380 KB
testcase_42 AC 25 ms
4,380 KB
testcase_43 AC 18 ms
4,380 KB
testcase_44 AC 14 ms
4,380 KB
testcase_45 AC 29 ms
4,380 KB
testcase_46 AC 12 ms
4,376 KB
testcase_47 AC 4 ms
4,376 KB
testcase_48 AC 7 ms
4,376 KB
testcase_49 AC 12 ms
4,380 KB
testcase_50 AC 20 ms
4,376 KB
testcase_51 AC 34 ms
4,376 KB
testcase_52 AC 44 ms
4,376 KB
testcase_53 AC 36 ms
4,376 KB
testcase_54 AC 36 ms
4,376 KB
testcase_55 AC 33 ms
4,376 KB
testcase_56 AC 21 ms
4,376 KB
testcase_57 AC 14 ms
4,376 KB
testcase_58 AC 9 ms
4,380 KB
testcase_59 AC 18 ms
4,376 KB
testcase_60 AC 22 ms
4,380 KB
testcase_61 AC 26 ms
4,380 KB
testcase_62 AC 14 ms
4,380 KB
testcase_63 AC 14 ms
4,380 KB
testcase_64 AC 13 ms
4,376 KB
testcase_65 AC 6 ms
4,376 KB
testcase_66 AC 6 ms
4,376 KB
testcase_67 AC 5 ms
4,376 KB
testcase_68 AC 4 ms
4,380 KB
testcase_69 AC 4 ms
4,380 KB
testcase_70 AC 7 ms
4,380 KB
testcase_71 AC 7 ms
4,376 KB
testcase_72 AC 9 ms
4,376 KB
testcase_73 AC 9 ms
4,376 KB
testcase_74 AC 8 ms
4,376 KB
testcase_75 AC 8 ms
4,376 KB
testcase_76 AC 7 ms
4,376 KB
testcase_77 AC 8 ms
4,380 KB
testcase_78 AC 8 ms
4,376 KB
testcase_79 AC 6 ms
4,376 KB
testcase_80 AC 6 ms
4,376 KB
testcase_81 AC 7 ms
4,380 KB
testcase_82 AC 7 ms
4,380 KB
testcase_83 AC 8 ms
4,376 KB
testcase_84 AC 7 ms
4,380 KB
testcase_85 AC 8 ms
4,380 KB
testcase_86 AC 6 ms
4,376 KB
testcase_87 AC 8 ms
4,380 KB
testcase_88 AC 10 ms
4,376 KB
testcase_89 AC 5 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

#define K_MAX 3
#define BIT_K_MAX 4 // 2^(K-1)
#define N_MAX 100
#define M_MAX 5000

const int bit[6] = {1, 2, 4, 8, 16, 32};

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

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

int complement_graph(int N, int M, int A[], int B[], edge* adj[], edge e[])
{
	static char adj_mat[N_MAX + 1][N_MAX + 1];
	static int i, u, w;
	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].id = i;
			e[i].next = adj[u];
			adj[u] = &(e[i++]);
			e[i].v = u;
			e[i].id = i;
			e[i].next = adj[w];
			adj[w] = &(e[i++]);
		}
	}
	return i / 2;
}



#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 4 // 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];
}



// Computing the length of a shortest cycle through K specified vertices in O(2^K LM) time
int shortest_cycle_through_specified_vertices(int N, edge* adj[], edge e[], int K, int T[])
{
	static char flag[N_MAX + 1];
	static int i, u, w, s;
	static edge *p;
	for (u = 1; u <= N; u++) flag[u] = -1; // nonterminals
	for (i = 0, s = T[K-1]; i < K; i++) flag[T[i]] = i; // terminals
	for (u = 1; u <= N; u++) {
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (w < u) continue;
			p->label = genrand() % (powd[POWX] - 1) + 1;
			if (u != s && w != s) e[p->id ^ 1].label = p->label; 
			else e[p->id ^ 1].label = genrand() % (powd[POWX] - 1) + 1; // around s
		}
	}
	
	static int k, l, cur, prev;
	static unsigned int dp[2][BIT_K_MAX][M_MAX * 2], tmp;
	for (k = 0; k < bit[K-1]; k++) {
		for (u = 1; u <= N; u++) {
			for (p = adj[u]; p != NULL; p = p->next) {
				dp[0][k][p->id] = 0;
				dp[1][k][p->id] = 0;
			}
		}
	}
	for (p = adj[s]; p != NULL; p = p->next) dp[0][0][p->id] = p->label;
	for (l = 1, cur = 1, prev = 0; l <= N; l++, cur ^= 1, prev ^= 1) {
		for (p = adj[s], tmp = 0; p != NULL; p = p->next) tmp ^= dp[prev][bit[K-1] - 1][p->id ^ 1];
		if (tmp != 0) return l;
		else if (l == N) return N + 1;
		
		for (k = 0; k < bit[K-1]; k++) {
			for (u = 1; u <= N; u++) {
				i = flag[u];
				if (u == s || (i >= 0 && (k & bit[i]) != 0)) continue;
				for (p = adj[u], tmp = 0; p != NULL; p = p->next) tmp ^= dp[prev][k][p->id ^ 1];
				for (p = adj[u]; p != NULL; p = p->next) {
					if (i < 0) dp[cur][k][p->id] = nim_product(tmp, p->label);
					else dp[cur][k | bit[i]][p->id] = nim_product(tmp ^ dp[prev][k][p->id ^ 1], p->label);
					dp[prev][k][p->id ^ 1] = 0;
				}
			}
		}
	}
}

int solve(int N, int M, int X, int Y, int Z, int A[], int B[])
{
	static int K = 3, T[K_MAX], ans[2];
	static edge *adj[N_MAX + 1] = {}, e[M_MAX * 2];
	complement_graph(N, M, A, B, adj, e);
	T[0] = X;
	T[1] = Y;
	T[2] = Z;
	ans[0] = shortest_cycle_through_specified_vertices(N, adj, e, K, T);
	ans[1] = shortest_cycle_through_specified_vertices(N, adj, e, K, T);
	chmin(&(ans[0]), ans[1]);
	// ans[1] = shortest_cycle_through_specified_vertices(N, adj, e, K, T);
	// chmin(&(ans[0]), ans[1]);
	return (ans[0] <= N)? ans[0]: -1;
}



int main()
{
	static int i, N, M, X, Y, Z, A[M_MAX + 1], B[M_MAX + 1];
	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]));
	printf("%d\n", solve(N, M, X, Y, Z, A, B));
	fflush(stdout);
	return 0;
}
0