Submission #670405

# Submission time Handle Problem Language Result Execution time Memory
670405 2022-12-08T22:00:39 Z rainboy Making Friends on Joitter is Fun (JOI20_joitter2) C
100 / 100
856 ms 56248 KB
#include <stdio.h>
#include <stdlib.h>

#define N	100000
#define M	300000
#define X	0x3f3f3f3f
#define MD	0x7fffffff

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

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

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

	for (o = eo_[h]; o--; )
		if (eu[h][o] == u && ev[h][o] == v) {
			ew[h][o] += w;
			return;
		}
	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]);
		ew[h] = (int *) realloc(ew[h], o * 2 * sizeof *ew[h]);
	}
	eu[h][o] = u, ev[h][o] = v, ew[h][o] = w;
}

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], ew[h][o] = ew[h][o + 1], o++;
			return;
		}
}

int get(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 ew[h][o];
	return 0;
}

int ds[N], sz[N], in[N], *ej[N], eo[N], n;
int uu[M], vv[M], cnt;
long long ans;

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 find(int i) {
	return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}

void join(int i, int j) {
	int o, tmp, d, w;

	i = find(i);
	j = find(j);
	if (i == j)
		return;
	if (sz[i] > sz[j])
		tmp = i, i = j, j = tmp;
	if ((w = get(i, j)))
		remove_(i, j), in[j] -= w, ans -= (long long) w * sz[j];
	if ((w = get(j, i)))
		remove_(j, i), in[i] -= w, ans -= (long long) w * sz[i];
	d = in[i] + in[j];
	for (o = eo[i]; o--; ) {
		int k = ej[i][o];

		if (k < n) {
			int k_ = find(k), wik = get(i, k_), wki = get(k_, i), wjk = get(j, k_), wkj = get(k_, j);

			remove_(i, k_), remove_(k_, i);
			if (wik && wkj || wki && wjk)
				uu[cnt] = j, vv[cnt] = k_, cnt++;
			add(j, k_, wik), add(k_, j, wki);
			append(j, k_);
		}
	}
	for (o = eo[i]; o--; ) {
		int k = ej[i][o];

		if (k >= n) {
			int k_ = find(k - n);

			if (k_ == j || k_ == i)
				continue;
			if (get(k, i)) {
				remove_(k, i);
				if (get(k, j))
					add(k_, j, -1), d--;
				else
					add(k, j, 1);
				append(j, k);
			}
		}
	}
	free(ej[i]);
	ans -= (long long) in[i] * sz[i] + (long long) in[j] * sz[j];
	ans += (long long) sz[i] * sz[j] * 2;
	ds[i] = j, sz[j] += sz[i], in[j] = d;
	ans += (long long) in[j] * sz[j];
}

int main() {
	int m, h, i, j, u, v;

	scanf("%d%d", &n, &m);
	for (h = 0; h < M; h++) {
		eu[h] = (int *) malloc(2 * sizeof *eu[h]);
		ev[h] = (int *) malloc(2 * sizeof *ev[h]);
		ew[h] = (int *) malloc(2 * sizeof *ew[h]);
	}
	for (i = 0; i < n; i++) {
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
		ds[i] = -1, sz[i] = 1;
	}
	while (m--) {
		scanf("%d%d", &i, &j), i--, j--;
		u = find(i), v = find(j);
		if (u != v) {
			if (!get(n + i, v))
				in[v]++, ans += sz[v], add(n + i, v, 1), add(u, v, 1), append(v, n + i);
			append(u, v), append(v, u);
			if (get(v, u)) {
				cnt = 0;
				uu[cnt] = i, vv[cnt] = j, cnt++;
				while (cnt)
					cnt--, join(uu[cnt], vv[cnt]);
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}

Compilation message

joitter2.c: In function 'add':
joitter2.c:24:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   24 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
joitter2.c: In function 'append':
joitter2.c:60:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   60 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
joitter2.c: In function 'join':
joitter2.c:90:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   90 |    if (wik && wkj || wki && wjk)
      |        ~~~~^~~~~~
joitter2.c: In function 'main':
joitter2.c:124:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
joitter2.c:135:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 35540 KB Output is correct
2 Correct 27 ms 35544 KB Output is correct
3 Correct 26 ms 35560 KB Output is correct
4 Correct 27 ms 35572 KB Output is correct
5 Correct 35 ms 35532 KB Output is correct
6 Correct 27 ms 35540 KB Output is correct
7 Correct 27 ms 35776 KB Output is correct
8 Correct 28 ms 35820 KB Output is correct
9 Correct 33 ms 35904 KB Output is correct
10 Correct 28 ms 35780 KB Output is correct
11 Correct 27 ms 35820 KB Output is correct
12 Correct 26 ms 35880 KB Output is correct
13 Correct 26 ms 35872 KB Output is correct
14 Correct 28 ms 35796 KB Output is correct
15 Correct 26 ms 35796 KB Output is correct
16 Correct 27 ms 35792 KB Output is correct
17 Correct 26 ms 35784 KB Output is correct
18 Correct 27 ms 35812 KB Output is correct
19 Correct 28 ms 35836 KB Output is correct
20 Correct 26 ms 35816 KB Output is correct
21 Correct 26 ms 35804 KB Output is correct
22 Correct 26 ms 35808 KB Output is correct
23 Correct 27 ms 35860 KB Output is correct
24 Correct 27 ms 35848 KB Output is correct
25 Correct 27 ms 35820 KB Output is correct
26 Correct 26 ms 35788 KB Output is correct
27 Correct 26 ms 35880 KB Output is correct
28 Correct 30 ms 35800 KB Output is correct
29 Correct 27 ms 35868 KB Output is correct
30 Correct 27 ms 35872 KB Output is correct
31 Correct 28 ms 35872 KB Output is correct
32 Correct 27 ms 35796 KB Output is correct
33 Correct 29 ms 35788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 35540 KB Output is correct
2 Correct 27 ms 35544 KB Output is correct
3 Correct 26 ms 35560 KB Output is correct
4 Correct 27 ms 35572 KB Output is correct
5 Correct 35 ms 35532 KB Output is correct
6 Correct 27 ms 35540 KB Output is correct
7 Correct 27 ms 35776 KB Output is correct
8 Correct 28 ms 35820 KB Output is correct
9 Correct 33 ms 35904 KB Output is correct
10 Correct 28 ms 35780 KB Output is correct
11 Correct 27 ms 35820 KB Output is correct
12 Correct 26 ms 35880 KB Output is correct
13 Correct 26 ms 35872 KB Output is correct
14 Correct 28 ms 35796 KB Output is correct
15 Correct 26 ms 35796 KB Output is correct
16 Correct 27 ms 35792 KB Output is correct
17 Correct 26 ms 35784 KB Output is correct
18 Correct 27 ms 35812 KB Output is correct
19 Correct 28 ms 35836 KB Output is correct
20 Correct 26 ms 35816 KB Output is correct
21 Correct 26 ms 35804 KB Output is correct
22 Correct 26 ms 35808 KB Output is correct
23 Correct 27 ms 35860 KB Output is correct
24 Correct 27 ms 35848 KB Output is correct
25 Correct 27 ms 35820 KB Output is correct
26 Correct 26 ms 35788 KB Output is correct
27 Correct 26 ms 35880 KB Output is correct
28 Correct 30 ms 35800 KB Output is correct
29 Correct 27 ms 35868 KB Output is correct
30 Correct 27 ms 35872 KB Output is correct
31 Correct 28 ms 35872 KB Output is correct
32 Correct 27 ms 35796 KB Output is correct
33 Correct 29 ms 35788 KB Output is correct
34 Correct 31 ms 35960 KB Output is correct
35 Correct 114 ms 41000 KB Output is correct
36 Correct 133 ms 42508 KB Output is correct
37 Correct 131 ms 42540 KB Output is correct
38 Correct 139 ms 42676 KB Output is correct
39 Correct 30 ms 36692 KB Output is correct
40 Correct 32 ms 36836 KB Output is correct
41 Correct 31 ms 36904 KB Output is correct
42 Correct 28 ms 36360 KB Output is correct
43 Correct 32 ms 36436 KB Output is correct
44 Correct 29 ms 36608 KB Output is correct
45 Correct 29 ms 36660 KB Output is correct
46 Correct 29 ms 36816 KB Output is correct
47 Correct 38 ms 36816 KB Output is correct
48 Correct 31 ms 36792 KB Output is correct
49 Correct 39 ms 37052 KB Output is correct
50 Correct 130 ms 42564 KB Output is correct
51 Correct 32 ms 36940 KB Output is correct
52 Correct 121 ms 42160 KB Output is correct
53 Correct 39 ms 36984 KB Output is correct
54 Correct 124 ms 42332 KB Output is correct
55 Correct 31 ms 36788 KB Output is correct
56 Correct 31 ms 36808 KB Output is correct
57 Correct 31 ms 36772 KB Output is correct
58 Correct 31 ms 36752 KB Output is correct
59 Correct 32 ms 36860 KB Output is correct
60 Correct 123 ms 41964 KB Output is correct
61 Correct 31 ms 36864 KB Output is correct
62 Correct 127 ms 42516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 35540 KB Output is correct
2 Correct 27 ms 35544 KB Output is correct
3 Correct 26 ms 35560 KB Output is correct
4 Correct 27 ms 35572 KB Output is correct
5 Correct 35 ms 35532 KB Output is correct
6 Correct 27 ms 35540 KB Output is correct
7 Correct 27 ms 35776 KB Output is correct
8 Correct 28 ms 35820 KB Output is correct
9 Correct 33 ms 35904 KB Output is correct
10 Correct 28 ms 35780 KB Output is correct
11 Correct 27 ms 35820 KB Output is correct
12 Correct 26 ms 35880 KB Output is correct
13 Correct 26 ms 35872 KB Output is correct
14 Correct 28 ms 35796 KB Output is correct
15 Correct 26 ms 35796 KB Output is correct
16 Correct 27 ms 35792 KB Output is correct
17 Correct 26 ms 35784 KB Output is correct
18 Correct 27 ms 35812 KB Output is correct
19 Correct 28 ms 35836 KB Output is correct
20 Correct 26 ms 35816 KB Output is correct
21 Correct 26 ms 35804 KB Output is correct
22 Correct 26 ms 35808 KB Output is correct
23 Correct 27 ms 35860 KB Output is correct
24 Correct 27 ms 35848 KB Output is correct
25 Correct 27 ms 35820 KB Output is correct
26 Correct 26 ms 35788 KB Output is correct
27 Correct 26 ms 35880 KB Output is correct
28 Correct 30 ms 35800 KB Output is correct
29 Correct 27 ms 35868 KB Output is correct
30 Correct 27 ms 35872 KB Output is correct
31 Correct 28 ms 35872 KB Output is correct
32 Correct 27 ms 35796 KB Output is correct
33 Correct 29 ms 35788 KB Output is correct
34 Correct 31 ms 35960 KB Output is correct
35 Correct 114 ms 41000 KB Output is correct
36 Correct 133 ms 42508 KB Output is correct
37 Correct 131 ms 42540 KB Output is correct
38 Correct 139 ms 42676 KB Output is correct
39 Correct 30 ms 36692 KB Output is correct
40 Correct 32 ms 36836 KB Output is correct
41 Correct 31 ms 36904 KB Output is correct
42 Correct 28 ms 36360 KB Output is correct
43 Correct 32 ms 36436 KB Output is correct
44 Correct 29 ms 36608 KB Output is correct
45 Correct 29 ms 36660 KB Output is correct
46 Correct 29 ms 36816 KB Output is correct
47 Correct 38 ms 36816 KB Output is correct
48 Correct 31 ms 36792 KB Output is correct
49 Correct 39 ms 37052 KB Output is correct
50 Correct 130 ms 42564 KB Output is correct
51 Correct 32 ms 36940 KB Output is correct
52 Correct 121 ms 42160 KB Output is correct
53 Correct 39 ms 36984 KB Output is correct
54 Correct 124 ms 42332 KB Output is correct
55 Correct 31 ms 36788 KB Output is correct
56 Correct 31 ms 36808 KB Output is correct
57 Correct 31 ms 36772 KB Output is correct
58 Correct 31 ms 36752 KB Output is correct
59 Correct 32 ms 36860 KB Output is correct
60 Correct 123 ms 41964 KB Output is correct
61 Correct 31 ms 36864 KB Output is correct
62 Correct 127 ms 42516 KB Output is correct
63 Correct 365 ms 52876 KB Output is correct
64 Correct 351 ms 52652 KB Output is correct
65 Correct 364 ms 52712 KB Output is correct
66 Correct 204 ms 48588 KB Output is correct
67 Correct 528 ms 52880 KB Output is correct
68 Correct 195 ms 48648 KB Output is correct
69 Correct 252 ms 48616 KB Output is correct
70 Correct 207 ms 48656 KB Output is correct
71 Correct 206 ms 48612 KB Output is correct
72 Correct 421 ms 50144 KB Output is correct
73 Correct 435 ms 50276 KB Output is correct
74 Correct 856 ms 56248 KB Output is correct
75 Correct 344 ms 51044 KB Output is correct
76 Correct 568 ms 52416 KB Output is correct
77 Correct 570 ms 52124 KB Output is correct
78 Correct 199 ms 46600 KB Output is correct
79 Correct 328 ms 50596 KB Output is correct
80 Correct 207 ms 46788 KB Output is correct
81 Correct 339 ms 51048 KB Output is correct
82 Correct 417 ms 50436 KB Output is correct
83 Correct 429 ms 50380 KB Output is correct
84 Correct 422 ms 50500 KB Output is correct
85 Correct 412 ms 50540 KB Output is correct
86 Correct 482 ms 51344 KB Output is correct
87 Correct 513 ms 53664 KB Output is correct
88 Correct 426 ms 50024 KB Output is correct
89 Correct 567 ms 52524 KB Output is correct