답안 #722790

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
722790 2023-04-12T21:13:37 Z rainboy Marshmallow Molecules (CCO19_day2problem2) C
25 / 25
230 ms 16124 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	100000
#define M	100000
#define MD	0x7fffffff

int *ej[N], eo[N];

void append(int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

int X = 123454321;

int hash(int u, int v) {
	return ((long long) u * X + v) % MD % M;
}

int *eu[M], *ev[M], eo_[M];

void init() {
	int h;

	for (h = 0; h < M; h++) {
		eu[h] = (int *) malloc(2 * sizeof *eu[h]);
		ev[h] = (int *) malloc(2 * sizeof *ev[h]);
	}
}

void add(int u, int v) {
	int h = hash(u, v), o = eo_[h]++;

	if (o >= 2 && (o & o - 1) == 0) {
		eu[h] = (int *) realloc(eu[h], o * 2 * sizeof *eu[h]);
		ev[h] = (int *) realloc(ev[h], o * 2 * sizeof *ev[h]);
	}
	eu[h][o] = u, ev[h][o] = v;
}

void remove_(int u, int v) {
	int h = hash(u, v), o;

	for (o = eo_[h]; o--; )
		if (eu[h][o] == u && ev[h][o] == v) {
			eo_[h]--;
			while (o < eo_[h])
				eu[h][o] = eu[h][o + 1], ev[h][o] = ev[h][o + 1], o++;
			return;
		}
}

int contains(int u, int v) {
	int h = hash(u, v), o;

	for (o = eo_[h]; o--; )
		if (eu[h][o] == u && ev[h][o] == v)
			return 1;
	return 0;
}

int ds[N];

int find(int i) {
	return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}

int dd[N];

void merge(int i, int j) {
	int o;

	if (contains(i, j))
		remove_(i, j), remove_(j, i), dd[i]--, dd[j]--;
	for (o = eo[i]; o--; ) {
		int k = find(ej[i][o]);

		if (!contains(i, k))
			continue;
		append(j, k);
		remove_(i, k), remove_(k, i), dd[i]--, dd[k]--;
		if (!contains(j, k))
			add(j, k), add(k, j), dd[j]++, dd[k]++;
	}
	free(ej[i]);
}

void join(int i, int j) {
	i = find(i);
	j = find(j);
	if (i == j)
		return;
	if (ds[i] > ds[j])
		ds[i] = j, merge(i, j);
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i, merge(j, i);
	}
}

int main() {
	static int ii[N];
	int n, m, cnt, h, i, j, o;
	long long ans;

	init();
	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (h = 0; h < m; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j), append(j, i);
		add(i, j), add(j, i), dd[i]++, dd[j]++;
	}
	memset(ds, -1, n * sizeof *ds);
	ans = 0;
	for (j = 0; j < n; j++) {
		cnt = 0;
		for (o = eo[j]; o--; ) {
			i = ej[j][o];
			if (i < j)
				ii[cnt++] = i;
		}
		while (cnt--)
			join(ii[cnt], j);
		ans += dd[find(j)];
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   14 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'add':
Main.c:39:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   39 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
Main.c: In function 'main':
Main.c:113:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
Main.c:117:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8148 KB Output is correct
2 Correct 6 ms 8124 KB Output is correct
3 Correct 10 ms 8532 KB Output is correct
4 Correct 7 ms 8436 KB Output is correct
5 Correct 7 ms 8384 KB Output is correct
6 Correct 8 ms 8404 KB Output is correct
7 Correct 9 ms 8404 KB Output is correct
8 Correct 8 ms 8392 KB Output is correct
9 Correct 7 ms 8376 KB Output is correct
10 Correct 8 ms 8404 KB Output is correct
11 Correct 7 ms 8404 KB Output is correct
12 Correct 7 ms 8392 KB Output is correct
13 Correct 8 ms 8404 KB Output is correct
14 Correct 10 ms 8452 KB Output is correct
15 Correct 7 ms 8276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8148 KB Output is correct
2 Correct 6 ms 8124 KB Output is correct
3 Correct 10 ms 8532 KB Output is correct
4 Correct 7 ms 8436 KB Output is correct
5 Correct 7 ms 8384 KB Output is correct
6 Correct 8 ms 8404 KB Output is correct
7 Correct 9 ms 8404 KB Output is correct
8 Correct 8 ms 8392 KB Output is correct
9 Correct 7 ms 8376 KB Output is correct
10 Correct 8 ms 8404 KB Output is correct
11 Correct 7 ms 8404 KB Output is correct
12 Correct 7 ms 8392 KB Output is correct
13 Correct 8 ms 8404 KB Output is correct
14 Correct 10 ms 8452 KB Output is correct
15 Correct 7 ms 8276 KB Output is correct
16 Correct 76 ms 11648 KB Output is correct
17 Correct 83 ms 11680 KB Output is correct
18 Correct 80 ms 11592 KB Output is correct
19 Correct 75 ms 11672 KB Output is correct
20 Correct 76 ms 11704 KB Output is correct
21 Correct 11 ms 8788 KB Output is correct
22 Correct 105 ms 11668 KB Output is correct
23 Correct 79 ms 11672 KB Output is correct
24 Correct 74 ms 11644 KB Output is correct
25 Correct 74 ms 11656 KB Output is correct
26 Correct 75 ms 11696 KB Output is correct
27 Correct 10 ms 8788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 139 ms 15900 KB Output is correct
2 Correct 131 ms 15848 KB Output is correct
3 Correct 111 ms 15564 KB Output is correct
4 Correct 115 ms 15836 KB Output is correct
5 Correct 128 ms 15732 KB Output is correct
6 Correct 109 ms 15564 KB Output is correct
7 Correct 128 ms 15944 KB Output is correct
8 Correct 127 ms 15860 KB Output is correct
9 Correct 108 ms 15564 KB Output is correct
10 Correct 139 ms 16124 KB Output is correct
11 Correct 141 ms 15732 KB Output is correct
12 Correct 123 ms 15596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8148 KB Output is correct
2 Correct 6 ms 8124 KB Output is correct
3 Correct 10 ms 8532 KB Output is correct
4 Correct 7 ms 8436 KB Output is correct
5 Correct 7 ms 8384 KB Output is correct
6 Correct 8 ms 8404 KB Output is correct
7 Correct 9 ms 8404 KB Output is correct
8 Correct 8 ms 8392 KB Output is correct
9 Correct 7 ms 8376 KB Output is correct
10 Correct 8 ms 8404 KB Output is correct
11 Correct 7 ms 8404 KB Output is correct
12 Correct 7 ms 8392 KB Output is correct
13 Correct 8 ms 8404 KB Output is correct
14 Correct 10 ms 8452 KB Output is correct
15 Correct 7 ms 8276 KB Output is correct
16 Correct 76 ms 11648 KB Output is correct
17 Correct 83 ms 11680 KB Output is correct
18 Correct 80 ms 11592 KB Output is correct
19 Correct 75 ms 11672 KB Output is correct
20 Correct 76 ms 11704 KB Output is correct
21 Correct 11 ms 8788 KB Output is correct
22 Correct 105 ms 11668 KB Output is correct
23 Correct 79 ms 11672 KB Output is correct
24 Correct 74 ms 11644 KB Output is correct
25 Correct 74 ms 11656 KB Output is correct
26 Correct 75 ms 11696 KB Output is correct
27 Correct 10 ms 8788 KB Output is correct
28 Correct 139 ms 15900 KB Output is correct
29 Correct 131 ms 15848 KB Output is correct
30 Correct 111 ms 15564 KB Output is correct
31 Correct 115 ms 15836 KB Output is correct
32 Correct 128 ms 15732 KB Output is correct
33 Correct 109 ms 15564 KB Output is correct
34 Correct 128 ms 15944 KB Output is correct
35 Correct 127 ms 15860 KB Output is correct
36 Correct 108 ms 15564 KB Output is correct
37 Correct 139 ms 16124 KB Output is correct
38 Correct 141 ms 15732 KB Output is correct
39 Correct 123 ms 15596 KB Output is correct
40 Correct 173 ms 12444 KB Output is correct
41 Correct 174 ms 12404 KB Output is correct
42 Correct 214 ms 13676 KB Output is correct
43 Correct 230 ms 13672 KB Output is correct
44 Correct 191 ms 14756 KB Output is correct
45 Correct 179 ms 14680 KB Output is correct
46 Correct 105 ms 13780 KB Output is correct
47 Correct 107 ms 13704 KB Output is correct
48 Correct 129 ms 15628 KB Output is correct
49 Correct 133 ms 15648 KB Output is correct
50 Correct 126 ms 15852 KB Output is correct
51 Correct 125 ms 15724 KB Output is correct
52 Correct 124 ms 15516 KB Output is correct
53 Correct 150 ms 15940 KB Output is correct
54 Correct 142 ms 15556 KB Output is correct
55 Correct 120 ms 15948 KB Output is correct
56 Correct 103 ms 15260 KB Output is correct
57 Correct 85 ms 14892 KB Output is correct