Submission #544758

# Submission time Handle Problem Language Result Execution time Memory
544758 2022-04-02T16:29:56 Z rainboy Inside information (BOI21_servers) C
100 / 100
402 ms 48472 KB
/* upsolve using centroid decomposition */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define N	120000
#define LN	16	/* LN = floor(log2(N)) */
#define Q	240000
 
int min(int a, int b) { return a < b ? a : b; }
 
int uu[N - 1], vv[N - 1], m;
 
int *eh[N], eo[N];
 
void append(int i, int h) {
	int o = eo[i]++;
 
	if (o >= 2 && (o & o - 1) == 0)
		eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
	eh[i][o] = h;
}
 
int cc[N][LN], xx[N][LN], kk[N], *ft[N]; char bb[N][LN];
int sz[N], c_;
 
void dfs(int f, int i, int n, int k, int c, int x, int up, int dn) {
	int o, centroid;
 
	if (k >= 0)
		cc[i][k] = c, xx[i][k] = x, bb[i][k] = up | dn << 1, kk[i]++;
	sz[i] = 1, centroid = 1;
	for (o = eo[i]; o--; ) {
		int h = eh[i][o], j = i ^ uu[h] ^ vv[h];

		if (h != f) {
			dfs(h, j, n, k, c, x, up && f > h, dn && f < h);
			if (sz[j] * 2 > n)
				centroid = 0;
			sz[i] += sz[j];
		}
	}
	if ((n - sz[i]) * 2 > n)
		centroid = 0;
	if (centroid)
		c_ = i;
}
 
void delete_(int i, int h) {
	int o;

	for (o = eo[i]; o--; )
		if (eh[i][o] == h) {
			eo[i]--;
			while (o < eo[i])
				eh[i][o] = eh[i][o + 1], o++;
			return;
		}
}

void cd(int f, int x, int i, int n, int k, int c) {
	int o;

	dfs(f, i, n, k, c, x, 1, 1), c = c_;
	ft[c] = (int *) calloc(eo[c], sizeof *ft[c]);
	x = 0;
	for (o = 0; o < eo[c]; o++) {
		int h = eh[c][o], j = c ^ uu[h] ^ vv[h];

		delete_(j, h);
		cd(h, x++, j, sz[j] < sz[c] ? sz[j] : n - sz[c], k + 1, c);
	}
}

int ds[N];

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

void update(int *ft, int i, int n) {
	while (i < n) {
		ft[i]++;
		i |= i + 1;
	}
}

int query(int *ft, int i) {
	int x = 0;

	while (i >= 0) {
		x += ft[i];
		i &= i + 1, i--;
	}
	return x;
}

void update_(int i, int j) {
	int k, i_, j_, c;

	i_ = find(i), j_ = find(j);
	for (k = 0; k < kk[i]; k++) {
		c = cc[i][k];
		if ((bb[i][k] & 2) != 0 && find(c) == j_)
			update(ft[c], eo[c] - 1 - xx[i][k], eo[c]);
	}
	for (k = 0; k < kk[j]; k++) {
		c = cc[j][k];
		if ((bb[j][k] & 2) != 0 && find(c) == i_)
			update(ft[c], eo[c] - 1 - xx[j][k], eo[c]);
	}
	join(i, j);
}

int query_(int i, int j) {
	int k;

	if (i == j)
		return 1;
	if (find(i) != find(j))
		return 0;
	if (kk[i] > kk[j] && cc[i][kk[j]] == j)
		return (bb[i][kk[j]] & 1) != 0;
	if (kk[j] > kk[i] && cc[j][kk[i]] == i)
		return (bb[j][kk[i]] & 2) != 0;
	for (k = min(kk[i], kk[j]) - 1; k >= 0; k--)
		if (cc[i][k] == cc[j][k])
			return (bb[i][k] & 1) != 0 && (bb[j][k] & 2) != 0 && xx[i][k] < xx[j][k];
	return 0;
}

int count(int i) {
	int k, i_, c, ans;

	ans = query(ft[i], eo[i] - 1) + 1, i_ = find(i);
	for (k = 0; k < kk[i]; k++) {
		c = cc[i][k];
		if ((bb[i][k] & 1) != 0 && find(c) == i_)
			ans += query(ft[c], eo[c] - 1 - xx[i][k] - 1) + 1;
	}
	return ans;
}

int main() {
	static char tt[Q];
	static int ii[Q], jj[Q];
	int n, q, h, i;
 
	scanf("%d%d", &n, &q), q += n - 1;
	for (i = 0; i < n; i++)
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
	for (h = 0; h < q; h++) {
		static char s[2];
 
		scanf("%s", s), tt[h] = s[0];
		if (tt[h] == 'C')
			scanf("%d", &ii[h]), ii[h]--;
		else {
			scanf("%d%d", &ii[h], &jj[h]), ii[h]--, jj[h]--;
			if (tt[h] == 'S')
				uu[m] = ii[h], vv[m] = jj[h], append(ii[h], m), append(jj[h], m), m++;
		}
	}
	cd(-1, -1, 0, n, -1, -1);
	memset(ds, -1, n * sizeof *ds);
	for (h = 0; h < q; h++)
		if (tt[h] == 'S')
			update_(ii[h], jj[h]);
		else if (tt[h] == 'Q')
			printf(query_(jj[h], ii[h]) ? "yes\n" : "no\n");
		else
			printf("%d\n", count(ii[h]));
	return 0;
}

Compilation message

servers.c: In function 'append':
servers.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   19 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
servers.c: In function 'main':
servers.c:163:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  163 |  scanf("%d%d", &n, &q), q += n - 1;
      |  ^~~~~~~~~~~~~~~~~~~~~
servers.c:169:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  169 |   scanf("%s", s), tt[h] = s[0];
      |   ^~~~~~~~~~~~~~
servers.c:171:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |    scanf("%d", &ii[h]), ii[h]--;
      |    ^~~~~~~~~~~~~~~~~~~
servers.c:173:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  173 |    scanf("%d%d", &ii[h], &jj[h]), ii[h]--, jj[h]--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 32 ms 2600 KB Output is correct
2 Correct 43 ms 4124 KB Output is correct
3 Correct 40 ms 4232 KB Output is correct
4 Correct 39 ms 4292 KB Output is correct
5 Correct 44 ms 4476 KB Output is correct
6 Correct 43 ms 4148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 2600 KB Output is correct
2 Correct 43 ms 4124 KB Output is correct
3 Correct 40 ms 4232 KB Output is correct
4 Correct 39 ms 4292 KB Output is correct
5 Correct 44 ms 4476 KB Output is correct
6 Correct 43 ms 4148 KB Output is correct
7 Correct 32 ms 2644 KB Output is correct
8 Correct 47 ms 3772 KB Output is correct
9 Correct 48 ms 3916 KB Output is correct
10 Correct 43 ms 3944 KB Output is correct
11 Correct 50 ms 4260 KB Output is correct
12 Correct 38 ms 4008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 2696 KB Output is correct
2 Correct 189 ms 35652 KB Output is correct
3 Correct 200 ms 35656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 2696 KB Output is correct
2 Correct 189 ms 35652 KB Output is correct
3 Correct 200 ms 35656 KB Output is correct
4 Correct 34 ms 2628 KB Output is correct
5 Correct 188 ms 35508 KB Output is correct
6 Correct 174 ms 34776 KB Output is correct
7 Correct 169 ms 34928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 2636 KB Output is correct
2 Correct 247 ms 48356 KB Output is correct
3 Correct 257 ms 48344 KB Output is correct
4 Correct 178 ms 48312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 2636 KB Output is correct
2 Correct 247 ms 48356 KB Output is correct
3 Correct 257 ms 48344 KB Output is correct
4 Correct 178 ms 48312 KB Output is correct
5 Correct 32 ms 2636 KB Output is correct
6 Correct 249 ms 47968 KB Output is correct
7 Correct 175 ms 47976 KB Output is correct
8 Correct 227 ms 47500 KB Output is correct
9 Correct 234 ms 47472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 2632 KB Output is correct
2 Correct 161 ms 35100 KB Output is correct
3 Correct 226 ms 35108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 2632 KB Output is correct
2 Correct 161 ms 35100 KB Output is correct
3 Correct 226 ms 35108 KB Output is correct
4 Correct 31 ms 2532 KB Output is correct
5 Correct 164 ms 34824 KB Output is correct
6 Correct 221 ms 34692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2636 KB Output is correct
2 Correct 228 ms 48388 KB Output is correct
3 Correct 236 ms 48300 KB Output is correct
4 Correct 165 ms 48204 KB Output is correct
5 Correct 28 ms 2652 KB Output is correct
6 Correct 153 ms 35172 KB Output is correct
7 Correct 211 ms 35148 KB Output is correct
8 Correct 310 ms 35816 KB Output is correct
9 Correct 291 ms 35916 KB Output is correct
10 Correct 347 ms 40068 KB Output is correct
11 Correct 369 ms 39152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2636 KB Output is correct
2 Correct 228 ms 48388 KB Output is correct
3 Correct 236 ms 48300 KB Output is correct
4 Correct 165 ms 48204 KB Output is correct
5 Correct 28 ms 2652 KB Output is correct
6 Correct 153 ms 35172 KB Output is correct
7 Correct 211 ms 35148 KB Output is correct
8 Correct 310 ms 35816 KB Output is correct
9 Correct 291 ms 35916 KB Output is correct
10 Correct 347 ms 40068 KB Output is correct
11 Correct 369 ms 39152 KB Output is correct
12 Correct 34 ms 2564 KB Output is correct
13 Correct 233 ms 47928 KB Output is correct
14 Correct 185 ms 47992 KB Output is correct
15 Correct 241 ms 47508 KB Output is correct
16 Correct 230 ms 47448 KB Output is correct
17 Correct 28 ms 2648 KB Output is correct
18 Correct 171 ms 34744 KB Output is correct
19 Correct 228 ms 34668 KB Output is correct
20 Correct 309 ms 35356 KB Output is correct
21 Correct 301 ms 35424 KB Output is correct
22 Correct 363 ms 38092 KB Output is correct
23 Correct 349 ms 40424 KB Output is correct
24 Correct 378 ms 40532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2556 KB Output is correct
2 Correct 38 ms 4136 KB Output is correct
3 Correct 39 ms 4204 KB Output is correct
4 Correct 38 ms 4188 KB Output is correct
5 Correct 41 ms 4624 KB Output is correct
6 Correct 41 ms 4148 KB Output is correct
7 Correct 30 ms 2636 KB Output is correct
8 Correct 163 ms 35728 KB Output is correct
9 Correct 180 ms 35788 KB Output is correct
10 Correct 29 ms 2636 KB Output is correct
11 Correct 230 ms 48344 KB Output is correct
12 Correct 232 ms 48472 KB Output is correct
13 Correct 163 ms 48204 KB Output is correct
14 Correct 31 ms 2656 KB Output is correct
15 Correct 150 ms 35172 KB Output is correct
16 Correct 218 ms 35228 KB Output is correct
17 Correct 277 ms 35836 KB Output is correct
18 Correct 311 ms 35880 KB Output is correct
19 Correct 402 ms 40064 KB Output is correct
20 Correct 356 ms 39160 KB Output is correct
21 Correct 183 ms 36260 KB Output is correct
22 Correct 184 ms 36300 KB Output is correct
23 Correct 233 ms 36216 KB Output is correct
24 Correct 244 ms 36404 KB Output is correct
25 Correct 297 ms 42244 KB Output is correct
26 Correct 251 ms 35504 KB Output is correct
27 Correct 230 ms 35456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2556 KB Output is correct
2 Correct 38 ms 4136 KB Output is correct
3 Correct 39 ms 4204 KB Output is correct
4 Correct 38 ms 4188 KB Output is correct
5 Correct 41 ms 4624 KB Output is correct
6 Correct 41 ms 4148 KB Output is correct
7 Correct 30 ms 2636 KB Output is correct
8 Correct 163 ms 35728 KB Output is correct
9 Correct 180 ms 35788 KB Output is correct
10 Correct 29 ms 2636 KB Output is correct
11 Correct 230 ms 48344 KB Output is correct
12 Correct 232 ms 48472 KB Output is correct
13 Correct 163 ms 48204 KB Output is correct
14 Correct 31 ms 2656 KB Output is correct
15 Correct 150 ms 35172 KB Output is correct
16 Correct 218 ms 35228 KB Output is correct
17 Correct 277 ms 35836 KB Output is correct
18 Correct 311 ms 35880 KB Output is correct
19 Correct 402 ms 40064 KB Output is correct
20 Correct 356 ms 39160 KB Output is correct
21 Correct 183 ms 36260 KB Output is correct
22 Correct 184 ms 36300 KB Output is correct
23 Correct 233 ms 36216 KB Output is correct
24 Correct 244 ms 36404 KB Output is correct
25 Correct 297 ms 42244 KB Output is correct
26 Correct 251 ms 35504 KB Output is correct
27 Correct 230 ms 35456 KB Output is correct
28 Correct 29 ms 2636 KB Output is correct
29 Correct 41 ms 3728 KB Output is correct
30 Correct 39 ms 3956 KB Output is correct
31 Correct 44 ms 3884 KB Output is correct
32 Correct 41 ms 4192 KB Output is correct
33 Correct 38 ms 3960 KB Output is correct
34 Correct 30 ms 2596 KB Output is correct
35 Correct 167 ms 35492 KB Output is correct
36 Correct 149 ms 34832 KB Output is correct
37 Correct 139 ms 34912 KB Output is correct
38 Correct 30 ms 2612 KB Output is correct
39 Correct 223 ms 47876 KB Output is correct
40 Correct 176 ms 48084 KB Output is correct
41 Correct 234 ms 47436 KB Output is correct
42 Correct 222 ms 47464 KB Output is correct
43 Correct 29 ms 2644 KB Output is correct
44 Correct 166 ms 34844 KB Output is correct
45 Correct 220 ms 34712 KB Output is correct
46 Correct 304 ms 35404 KB Output is correct
47 Correct 290 ms 35504 KB Output is correct
48 Correct 352 ms 38280 KB Output is correct
49 Correct 362 ms 40480 KB Output is correct
50 Correct 357 ms 40484 KB Output is correct
51 Correct 170 ms 35904 KB Output is correct
52 Correct 169 ms 35748 KB Output is correct
53 Correct 149 ms 35276 KB Output is correct
54 Correct 154 ms 35900 KB Output is correct
55 Correct 155 ms 35692 KB Output is correct
56 Correct 232 ms 36044 KB Output is correct
57 Correct 295 ms 41752 KB Output is correct
58 Correct 307 ms 34728 KB Output is correct
59 Correct 227 ms 35420 KB Output is correct