Submission #730954

# Submission time Handle Problem Language Result Execution time Memory
730954 2023-04-26T16:48:12 Z rainboy Monthly railway pass (LMIO18_menesinis_bilietas) C
100 / 100
169 ms 32780 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	500000
#define M	500000

int ds[N], sz[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, sz[j] += sz[i];
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i, sz[i] += sz[j];
	}
}

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 main() {
	static int ii[M], jj[M];
	static char tt[M], used[N];
	int n, m, h, i, j, o, c, k, s;

	scanf("%d%d", &n, &m);
	memset(ds, -1, n * sizeof *ds);
	for (i = 0; i < n; i++)
		sz[i] = 1;
	for (h = 0; h < m; h++) {
		static char s[2];

		scanf("%d%d%s", &i, &j, s), i--, j--;
		ii[h] = i, jj[h] = j, tt[h] = s[0];
		if (tt[h] == 'T')
			join(i, j);
	}
	c = 0;
	for (i = 0; i < n; i++)
		if (ds[i] < 0) {
			ej[i] = (int *) malloc(2 * sizeof *ej[i]);
			c++;
		}
	for (h = 0; h < m; h++)
		if (tt[h] == 'A') {
			i = find(ii[h]), j = find(jj[h]);
			if (i != j)
				append(i, j), append(j, i);
		}
	s = 0;
	for (i = 0; i < n; i++)
		if (ds[i] < 0) {
			k = 0;
			for (o = eo[i]; o--; ) {
				j = ej[i][o];
				if (!used[j])
					used[j] = 1, k++;
			}
			if (k == c - 1)
				s += sz[i];
			for (o = eo[i]; o--; ) {
				j = ej[i][o];
				used[j] = 0;
			}
		}
	printf("%d\n", s);
	return 0;
}

Compilation message

menesinis_bilietas.c: In function 'append':
menesinis_bilietas.c:33:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   33 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
menesinis_bilietas.c: In function 'main':
menesinis_bilietas.c:43:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
menesinis_bilietas.c:50:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |   scanf("%d%d%s", &i, &j, s), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 158 ms 13472 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 26 ms 22324 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 57 ms 4464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 22324 KB Output is correct
2 Correct 1 ms 852 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 3 ms 560 KB Output is correct
6 Correct 109 ms 12508 KB Output is correct
7 Correct 169 ms 32780 KB Output is correct
8 Correct 5 ms 1492 KB Output is correct
9 Correct 10 ms 1840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 3 ms 560 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 4 ms 724 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 4 ms 724 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 3 ms 560 KB Output is correct
21 Correct 1 ms 852 KB Output is correct
22 Correct 5 ms 1492 KB Output is correct
23 Correct 10 ms 1840 KB Output is correct
24 Correct 57 ms 4464 KB Output is correct
25 Correct 10 ms 1328 KB Output is correct
26 Correct 103 ms 12380 KB Output is correct
27 Correct 38 ms 5100 KB Output is correct
28 Correct 53 ms 6716 KB Output is correct
29 Correct 28 ms 3892 KB Output is correct
30 Correct 51 ms 5800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 4 ms 724 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 10 ms 1328 KB Output is correct
17 Correct 103 ms 12380 KB Output is correct
18 Correct 38 ms 5100 KB Output is correct
19 Correct 53 ms 6716 KB Output is correct
20 Correct 28 ms 3892 KB Output is correct
21 Correct 51 ms 5800 KB Output is correct
22 Correct 158 ms 13472 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 26 ms 22324 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 292 KB Output is correct
28 Correct 3 ms 560 KB Output is correct
29 Correct 1 ms 852 KB Output is correct
30 Correct 109 ms 12508 KB Output is correct
31 Correct 169 ms 32780 KB Output is correct
32 Correct 5 ms 1492 KB Output is correct
33 Correct 10 ms 1840 KB Output is correct
34 Correct 57 ms 4464 KB Output is correct
35 Correct 20 ms 2772 KB Output is correct
36 Correct 135 ms 15864 KB Output is correct
37 Correct 75 ms 8864 KB Output is correct