結果

問題 No.2985 May Count Induced C4 Subgraphs
ユーザー ygussanyygussany
提出日時 2024-12-10 23:39:45
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 3,196 ms / 5,000 ms
コード長 7,393 bytes
コンパイル時間 1,932 ms
コンパイル使用メモリ 37,232 KB
実行使用メモリ 61,440 KB
最終ジャッジ日時 2024-12-10 23:40:13
合計ジャッジ時間 25,454 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 2,940 ms
61,440 KB
testcase_07 AC 2,780 ms
61,440 KB
testcase_08 AC 2,786 ms
61,440 KB
testcase_09 AC 3,196 ms
61,440 KB
testcase_10 AC 2,698 ms
61,312 KB
testcase_11 AC 451 ms
60,544 KB
testcase_12 AC 183 ms
58,112 KB
testcase_13 AC 177 ms
58,112 KB
testcase_14 AC 169 ms
58,112 KB
testcase_15 AC 177 ms
58,112 KB
testcase_16 AC 162 ms
58,112 KB
testcase_17 AC 1,411 ms
60,032 KB
testcase_18 AC 1,255 ms
59,904 KB
testcase_19 AC 1,524 ms
60,160 KB
testcase_20 AC 1,695 ms
60,160 KB
testcase_21 AC 1,548 ms
60,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 998244353;

typedef struct HList {
	struct HList *next;
	int u, w;
} hlist;

#define HASH 5000011
const int H_Mod = HASH;

int hash_func(int u, int w)
{
	return (((long long)u << 30) + w) % H_Mod;
}

int hn = 0;
hlist *hash[HASH] = {}, hd[200001];

int is_adjacent(int u, int w)
{
	if (u > w) {
		u ^= w;
		w ^= u;
		u ^= w;
	}
	
	int h;
	hlist *hp;
	h = hash_func(u, w);
	for (hp = hash[h]; hp != NULL; hp = hp->next) if (hp->u == u && hp->w == w) return 1;
	return 0;
}

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

typedef struct {
	int key, id;
} data;

typedef struct {
	data obj[400001];
	int size;
} min_heap;

void push(min_heap* h, data x)
{
	int i = ++(h->size), j = i >> 1;
	data tmp;
	h->obj[i] = x;
	while (j > 0) {
		if (h->obj[i].key < h->obj[j].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j >>= 1;
		} else break;
	}
}

data pop(min_heap* h)
{
	int i = 1, j = 2;
	data output = h->obj[1], tmp;
	h->obj[1] = h->obj[(h->size)--];
	while (j <= h->size) {
		if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1;
		if (h->obj[j].key < h->obj[i].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j <<= 1;
		} else break;
	}
	return output;
}

int count_triangles(int n, int m, int adj_mat[][1001])
{
	if (n < 3) return 0;
	
	int i, j, u, w, nn = n, tail;
	long long ans;
	static int deg[200001], flag[200001], q[200001];
	static edge *adj[200001], e[400001], *ep;
	static min_heap he;
	data d;
	for (u = 1; u <= n; u++) {
		deg[u] = 0;
		flag[u] = 0;
		adj[u] = NULL;
	}
	if (m <= n * (n - 1) / 2 / 2) {
		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].next = adj[u];
				adj[u] = &(e[i++]);
				e[i].v = u;
				e[i].next = adj[w];
				adj[w] = &(e[i++]);
				deg[u]++;
				deg[w]++;
			}
		}

		ans = 0;
		he.size = 0;
		for (u = 1; u <= n; u++) {
			d.key = deg[u];
			d.id = u;
			push(&he, d);
		}
		while (he.size > 0) {
			d = pop(&he);
			u = d.id;
			if (flag[u] != 0) continue;
			flag[u] = 1;
			nn--;
			if (deg[u] == nn) {
				ans += (nn + 1) * nn * (nn - 1) / 6;
				break;
			}
			for (ep = adj[u], tail = 0; ep != NULL; ep = ep->next) {
				w = ep->v;
				if (flag[w] == 0) {
					q[++tail] = w;
					deg[w]--;
					d.key = deg[w];
					d.id = w;
					push(&he, d);
				}
			}
			for (i = 1; i <= tail; i++) for (j = i + 1; j <= tail; j++) ans += adj_mat[q[i]][q[j]];
		}
	} else {
		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].next = adj[u];
				adj[u] = &(e[i++]);
				e[i].v = u;
				e[i].next = adj[w];
				adj[w] = &(e[i++]);
				deg[u]++;
				deg[w]++;
			}
		}
		
		ans = n * (n - 1) * (n - 2) / 6;
		ans -= (n * (n - 1) / 2 - m) * (n - 2);
		for (u = 1; u <= n; u++) ans += deg[u] * (deg[u] - 1) / 2;
		he.size = 0;
		for (u = 1; u <= n; u++) {
			d.key = deg[u];
			d.id = u;
			push(&he, d);
		}
		while (he.size > 0) {
			d = pop(&he);
			u = d.id;
			if (flag[u] != 0) continue;
			flag[u] = 1;
			nn--;
			if (deg[u] == nn) {
				ans -= (nn + 1) * nn * (nn - 1) / 6;
				break;
			}
			for (ep = adj[u], tail = 0; ep != NULL; ep = ep->next) {
				w = ep->v;
				if (flag[w] == 0) {
					q[++tail] = w;
					deg[w]--;
					d.key = deg[w];
					d.id = w;
					push(&he, d);
				}
			}
			for (i = 1; i <= tail; i++) for (j = i + 1; j <= tail; j++) ans -= 1 - adj_mat[q[i]][q[j]];
		}
	}
	return ans % Mod;
}

void count_triangles_sub(int n, int v[], long long *num_edge, long long *num_triangle)
{
	int h, i, j, k;
	static int adj_mat[1001][1001] = {};
	for (i = 1, *num_edge = 0; i <= n; i++) {
		for (j = i + 1; j <= n; j++) {
			if (is_adjacent(v[i], v[j]) != 0) {
				adj_mat[i][j] = 1;
				adj_mat[j][i] = 1;
				(*num_edge)++;
			}
		}
	}
	if (*num_edge == n * (n - 1) / 2) *num_triangle = n * (n - 1) * (n - 2) / 6;
	else if (*num_edge == n * (n - 1) / 2 - 1) *num_triangle = n * (n - 1) * (n - 2) / 6 - (n - 2);
	else *num_triangle = count_triangles(n, *num_edge, adj_mat);
	for (i = 1; i <= n; i++) for (j = i + 1; j <= n; j++) { adj_mat[i][j] = 0; adj_mat[j][i] = 0; }
}

int solve(int N, int M, int U[], int V[])
{
	int i;
	for (i = 1; i <= M; i++) {
		if (U[i] > V[i]) {
			U[i] ^= V[i];
			V[i] ^= U[i];
			U[i] ^= V[i];
		}
	}
	if (N <= 3) return 0;
	
	int h, u, w;
	static int ed[200001][2];
	for (i = 1, hn = 0; i <= M; i++) {
		u = U[i];
		w = V[i];
		ed[i][0] = u;
		ed[i][1] = w;
		h = hash_func(u, w);
		hd[hn].u = u;
		hd[hn].w = w;
		hd[hn].next = hash[h];
		hash[h] = &(hd[hn++]);
	}
	
	static int deg[200001];
	static edge *adj[200001], e[400001], *ep;
	for (u = 1; u <= N; u++) {
		deg[u] = 0;
		adj[u] = NULL;
	}
	for (i = 1; i <= M; i++) {
		u = ed[i][0];
		w = ed[i][1];
		e[i*2-1].v = w;
		e[i*2-1].next = adj[u];
		adj[u] = &(e[i*2-1]);
		e[i*2].v = u;
		e[i*2].next = adj[w];
		adj[w] = &(e[i*2]);
		deg[u]++;
		deg[w]++;
	}
	
	int r, j, tail;
	static int flag[200001], q[200001];
	long long ans, tmp;
	ans = (__int128)N * (N - 1) * (N - 2) * (N - 3) / 24 % Mod;
	ans += Mod - (long long)M * (N - 2) * (N - 3) / 2 % Mod;
	for (u = 1, tmp = (long long)M * (M - 1) / 2; u <= N; u++) {
		flag[u] = 0;
		tmp -= (long long)deg[u] * (deg[u] - 1) / 2;
		ans += (long long)deg[u] * (deg[u] - 1) / 2 * (N - 3) % Mod;
		ans += Mod - (long long)deg[u] * (deg[u] - 1) * (deg[u] - 2) / 6 % Mod;
	}
	ans += tmp;
	for (i = 1; i <= M; i++) {
		u = ed[i][0];
		w = ed[i][1];
		ans += Mod - (long long)(deg[u] - 1) * (deg[w] - 1) % Mod;
	}
	for (u = 1; u <= N; u++) {
		for (ep = adj[u], tail = 0; ep != NULL; ep = ep->next) {
			w = ep->v;
			flag[w] = 1;
			q[++tail] = w;
		}
		if (tail >= 3) {
			if (tail <= 500) {
				for (i = 1, tmp = 0; i <= tail; i++) for (j = i + 1; j <= tail; j++) tmp += is_adjacent(q[i], q[j]);
			} else {
				for (i = 1, tmp = 0; i <= M; i++) if (flag[ed[i][0]] != 0 && flag[ed[i][1]] != 0) tmp++;
			}
			ans += tmp * (tail - 2) % Mod;
		}
		for (i = 1; i <= tail; i++) flag[q[i]] = 0;
	}
	
	int n = N;
	long long num_triangle = 0, tmp_num_edge, tmp_num_triangle;
	static min_heap he;
	data d;
	he.size = 0;
	for (u = 1; u <= N; u++) {
		d.key = deg[u];
		d.id = u;
		push(&he, d);
	}
	while (he.size > 0) {
		d = pop(&he);
		u = d.id;
		if (flag[u] != 0) continue;
		flag[u] = 1;
		n--;
		if (deg[u] == n) {
			num_triangle += (n + 1) * n * (n - 1) / 6;
			ans += Mod - (long long)(n + 1) * n * (n - 1) * (n - 2) / 24 * 2 % Mod;
			break;
		}
		for (ep = adj[u], tail = 0; ep != NULL; ep = ep->next) {
			w = ep->v;
			if (flag[w] == 0) {
				q[++tail] = w;
				deg[w]--;
				d.key = deg[w];
				d.id = w;
				push(&he, d);
			}
		}
		// for (i = 1; i <= tail; i++) for (j = i + 1; j <= tail; j++) num_triangle += is_adjacent(q[i], q[j]);
		count_triangles_sub(tail, q, &tmp_num_edge, &tmp_num_triangle);
		num_triangle += tmp_num_edge;
		if (tail >= 3) ans += Mod - tmp_num_triangle * 2 % Mod;
	}
	ans += num_triangle * 3;
	ans += Mod - num_triangle * (N - 3) % Mod;
	for (i = 1; i <= M; i++) {
		u = ed[i][0];
		w = ed[i][1];
		h = hash_func(u, w);
		hash[h] = NULL;
	}
	return ans % Mod;
}

int main()
{
	int i, N, M, U[200001], V[200001];
	scanf("%d %d", &N, &M);
	for (i = 1; i <= M; i++) scanf("%d %d", &(U[i]), &(V[i]));
	printf("%d %d\n", Mod - 1, solve(N, M, U, V));
	fflush(stdout);
	return 0;
}
0