Submission #473595

# Submission time Handle Problem Language Result Execution time Memory
473595 2021-09-15T17:07:49 Z rainboy Alkemija (COCI18_alkemija) C
80 / 80
78 ms 9040 KB
#include <stdio.h>
#include <stdlib.h>

#define N	100000
#define K	100000

int *hh[N], kk[N], *ii[K], nn[K];

void append(int i, int h) {
	int g = kk[i]++;

	if (g >= 2 && (g & g - 1) == 0)
		hh[i] = (int *) realloc(hh[i], g * 2 * sizeof *hh[i]);
	hh[i][g] = h;
}

int dd[K];

void dfs_i(int i);

void dfs_h(int h) {
	if (--dd[h] == 0) {
		int g;

		for (g = 0; g < nn[h]; g++)
			dfs_i(ii[h][g]);
	}
}

char in[N];

void dfs_i(int i) {
	if (in[i] != 1) {
		int g;

		in[i] = 1;
		for (g = 0; g < kk[i]; g++)
			dfs_h(hh[i][g]);
	}
}

int main() {
	int n, m, k, h, i, cnt;

	scanf("%d%d", &n, &m);
	while (m--) {
		scanf("%d", &i), i--;
		in[i] = -1;
	}
	for (i = 0; i < n; i++)
		hh[i] = (int *) malloc(2 * sizeof *hh[i]);
	scanf("%d", &k);
	for (h = 0; h < k; h++) {
		scanf("%d%d", &m, &nn[h]);
		dd[h] = m;
		while (m--) {
			scanf("%d", &i), i--;
			append(i, h);
		}
		ii[h] = (int *) malloc(nn[h] * sizeof *ii[h]);
		for (i = 0; i < nn[h]; i++)
			scanf("%d", &ii[h][i]), ii[h][i]--;
	}
	for (i = 0; i < n; i++)
		if (in[i] == -1)
			dfs_i(i);
	cnt = 0;
	for (i = 0; i < n; i++)
		if (in[i])
			cnt++;
	printf("%d\n", cnt);
	for (i = 0; i < n; i++)
		if (in[i])
			printf("%d ", i + 1);
	printf("\n");
	return 0;
}

Compilation message

alkemija.c: In function 'append':
alkemija.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   12 |  if (g >= 2 && (g & g - 1) == 0)
      |                     ~~^~~
alkemija.c: In function 'main':
alkemija.c:45:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
alkemija.c:47:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |   scanf("%d", &i), i--;
      |   ^~~~~~~~~~~~~~~
alkemija.c:52:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |  scanf("%d", &k);
      |  ^~~~~~~~~~~~~~~
alkemija.c:54:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |   scanf("%d%d", &m, &nn[h]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~
alkemija.c:57:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |    scanf("%d", &i), i--;
      |    ^~~~~~~~~~~~~~~
alkemija.c:62:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |    scanf("%d", &ii[h][i]), ii[h][i]--;
      |    ^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2568 KB Output is correct
2 Correct 22 ms 3236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 4916 KB Output is correct
2 Correct 60 ms 5912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 7364 KB Output is correct
2 Correct 55 ms 6324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 8948 KB Output is correct
2 Correct 78 ms 9040 KB Output is correct