結果

問題 No.1900 Don't be Powers of 2
ユーザー 👑 ygussanyygussany
提出日時 2022-03-20 11:48:09
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 3,519 bytes
コンパイル時間 1,960 ms
コンパイル使用メモリ 31,744 KB
実行使用メモリ 48,852 KB
最終ジャッジ日時 2024-11-28 02:29:06
合計ジャッジ時間 4,448 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,820 KB
testcase_03 AC 43 ms
6,820 KB
testcase_04 AC 42 ms
6,816 KB
testcase_05 AC 45 ms
6,816 KB
testcase_06 AC 45 ms
6,820 KB
testcase_07 AC 45 ms
6,816 KB
testcase_08 AC 19 ms
6,816 KB
testcase_09 AC 9 ms
6,816 KB
testcase_10 AC 3 ms
6,816 KB
testcase_11 AC 5 ms
6,820 KB
testcase_12 AC 25 ms
6,816 KB
testcase_13 AC 2 ms
6,820 KB
testcase_14 AC 29 ms
6,820 KB
testcase_15 AC 1 ms
6,816 KB
testcase_16 AC 2 ms
6,816 KB
testcase_17 AC 40 ms
6,816 KB
testcase_18 AC 31 ms
6,816 KB
testcase_19 AC 43 ms
6,816 KB
testcase_20 AC 31 ms
6,816 KB
testcase_21 AC 28 ms
6,816 KB
testcase_22 AC 26 ms
6,816 KB
testcase_23 AC 44 ms
6,820 KB
testcase_24 AC 46 ms
6,816 KB
testcase_25 AC 44 ms
6,820 KB
testcase_26 AC 141 ms
48,848 KB
testcase_27 AC 53 ms
9,032 KB
testcase_28 AC 35 ms
6,816 KB
testcase_29 AC 32 ms
6,816 KB
testcase_30 AC 3 ms
6,816 KB
testcase_31 AC 1 ms
6,816 KB
testcase_32 AC 1 ms
6,816 KB
testcase_33 AC 135 ms
48,780 KB
testcase_34 AC 133 ms
48,640 KB
testcase_35 AC 151 ms
48,852 KB
testcase_36 AC 103 ms
38,200 KB
testcase_37 AC 44 ms
6,816 KB
testcase_38 AC 24 ms
13,644 KB
testcase_39 AC 18 ms
8,016 KB
testcase_40 AC 42 ms
6,816 KB
testcase_41 AC 50 ms
6,820 KB
testcase_42 AC 1 ms
6,820 KB
testcase_43 AC 1 ms
6,816 KB
testcase_44 AC 1 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

#define N_MAX 2000
#define M_MAX 2000000

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

int DFS_bipartite_matching(edge* aux[], int par[], int u)
{
	int w;
	for (; aux[u] != NULL; aux[u] = aux[u]->next) {
		w = aux[u]->v;
		if (par[w] == 0) { // w is a sink
			par[w] = u;
			return w;
		} else if (par[w] > 0) continue; // w is already checked
		par[w] = u;
		w = DFS_bipartite_matching(aux, par, w);
		if (w > 0) return w;
	}
	return 0;
}

int bipartite_matching_augmentation(int N, char color[], edge* adj[], edge e[], int mate[])
{
	static int i, u, w, depth[N_MAX + 1], par[N_MAX + 1], q[N_MAX + 1], head, tail;
	static edge *aux[N_MAX + 1], f[M_MAX * 2], *p;
	for (u = 1, tail = 0, par[0] = 0; u <= N; u++) {
		if (mate[u] == 0) { // u is a source of sink
			if (color[u] == 0) { // u is a source
				depth[u] = 0;
				q[tail++] = u;
			} else depth[u] = N;
			par[u] = 0;
		} else {
			depth[u] = N;
			par[u] = -1;
		}
	}
	
	// BFS for constructing the layered network
	for (head = 0, i = 0; head < tail; head++) {
		u = q[head];
		aux[u] = NULL;
		if (color[u] == 0) {
			for (p = adj[u]; p != NULL; p = p->next) {
				w = p->v;
				if (mate[u] == w) continue; // No arc in this direction
				if (depth[w] == N) { // w has been found for the first time
					depth[w] = depth[u] + 1;
					q[tail++] = w;
				}
				if (depth[w] == depth[u] + 1) { // Add the arc uw
					f[i].v = w;
					f[i].next = aux[u];
					aux[u] = &(f[i++]);
				}
			}
		} else if (mate[u] != 0) {
			w = mate[u];
			if (depth[w] == N) { // w has been found for the first time
				depth[w] = depth[u] + 1;
				q[tail++] = w;
			}
			if (depth[w] == depth[u] + 1) { // Add the arc uw
				f[i].v = w;
				f[i].next = aux[u];
				aux[u] = &(f[i++]);
			}
		}
	}

	// DFS for finding disjoint augmenting paths
	for (u = 1, tail = 0; u <= N; u++) {
		if (depth[u] != 0) continue;
		w = DFS_bipartite_matching(aux, par, u);
		if (w > 0) q[tail++] = w; // An augmenting path from u to w has been found
	}
	
	// Augmentation
	for (head = 0; head < tail; head++) {
		for (w = q[head], u = par[w]; u > 0; w = par[u], u = par[w]) {
			mate[u] = w;
			mate[w] = u;
		}
	}
	return tail;
}

int bipartite_matching(int N, char color[], edge* adj[], edge e[], int mate[])
{
	int i, u, dif, ans = 0;
	edge *p;
	for (u = 1; u <= N; u++) mate[u] = 0; // Initialization
	do { // Augmentation
		dif = bipartite_matching_augmentation(N, color, adj, e, mate);
		ans += dif;
	} while (dif != 0);
	return ans;
}

int main()
{
	int i, N, A[2001];
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d", &(A[i]));
	
	int k, m = 0, u, w, bit[31];
	edge *adj[2001] = {}, e[2000001], *p;
	for (i = 1, bit[0] = 1; i <= 30; i++) bit[i] = bit[i-1] << 1;
	for (u = 1; u <= N; u++) {
		for (k = 0; k < 30; k++) {
			for (w = u + 1; w <= N; w++) {
				if ((A[u] ^ A[w]) == bit[k]) {
					e[m].v = w;
					e[m].next = adj[u];
					adj[u] = &(e[m++]);
					e[m].v = u;
					e[m].next = adj[w];
					adj[w] = &(e[m++]);
				}
			}
		}
	}
	
	char color[2001];
	int q[2001], head, tail;
	for (u = 1; u <= N; u++) color[u] = -1;
	for (i = 1; i <= N; i++) {
		if (color[i] >= 0) continue;
		q[0] = i;
		color[i] = 0;
		for (head = 0, tail = 1; head < tail; head++) {
			u = q[head];
			for (p = adj[u]; p != NULL; p = p->next) {
				w = p->v;
				if (color[w] < 0) {
					color[w] = color[u] ^ 1;
					q[tail++] = w;
				}
			}
		}
	}
	
	int mate[2001];
	printf("%d\n", N - bipartite_matching(N, color, adj, e, mate));
	fflush(stdout);
	return 0;
}
0