Submission #544731

#TimeUsernameProblemLanguageResultExecution timeMemory
544731rainboyRailway (BOI17_railway)C11
100 / 100
119 ms18992 KiB
#include <stdio.h>
#include <stdlib.h>

#define N	100000

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int ij[N];
int *eh[N], eo[N];

void append(int i, int j) {
	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] = j;
}

int dd[N], pp[N], qq[N], ff[N], sz[N], ta[N], tb[N], qu[N], cnt;

void dfs(int p, int f, int i, int d) {
	int o, j_;

	qu[ta[i] = cnt++] = i;
	dd[i] = d, pp[i] = p, ff[i] = f;
	j_ = -1, sz[i] = 1;
	for (o = 0; o < eo[i]; o++) {
		int h = eh[i][o], j = i ^ ij[h];

		if (h != f) {
			dfs(i, h, j, d + 1);
			if (j_ == -1 || sz[j_] < sz[j])
				j_ = j;
			sz[i] += sz[j];
		}
	}
	if (j_ != -1)
		qq[j_] = -1;
	tb[i] = cnt;
}

int lca(int i, int j) {
	while (qq[i] != qq[j])
		if (dd[qq[i]] > dd[qq[j]])
			i = pp[qq[i]];
		else
			j = pp[qq[j]];
	return dd[i] < dd[j] ? i : j;
}

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (ta[ii[j]] == ta[i_])
				j++;
			else if (ta[ii[j]] < ta[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}

int main() {
	static int kk[N];
	static char used[N];
	int n, m, k, h, i, j;

	scanf("%d%d%d", &n, &m, &k);
	for (i = 0; i < n; i++)
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
	for (h = 0; h < n - 1; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		ij[h] = i ^ j;
		append(i, h), append(j, h);
	}
	dfs(-1, -1, 0, 0);
	for (h = 0; h < cnt; h++) {
		i = qu[h];
		qq[i] = qq[i] ? qq[pp[i]] : i;
	}
	while (m--) {
		static int ii[N];

		scanf("%d", &cnt);
		for (h = 0; h < cnt; h++)
			scanf("%d", &ii[h]), ii[h]--;
		sort(ii, 0, cnt);
		for (h = 0; h < cnt; h++) {
			i = ii[h], j = lca(ii[h], ii[(h - 1 + cnt) % cnt]);
			kk[ta[i]]++, kk[ta[j]]--;
		}
	}
	for (i = 1; i < n; i++)
		kk[i] += kk[i - 1];
	cnt = 0;
	for (i = 0; i < n; i++)
		if (kk[tb[i] - 1] - (ta[i] == 0 ? 0 : kk[ta[i] - 1]) >= k)
			used[ff[i]] = 1, cnt++;
	printf("%d\n", cnt);
	for (h = 0; h < n - 1; h++)
		if (used[h])
			printf("%d ", h + 1);
	printf("\n");
	return 0;
}

Compilation message (stderr)

railway.c: In function 'append':
railway.c:18:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   18 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
railway.c: In function 'main':
railway.c:79:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |  scanf("%d%d%d", &n, &m, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
railway.c:83:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
railway.c:95:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |   scanf("%d", &cnt);
      |   ^~~~~~~~~~~~~~~~~
railway.c:97:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |    scanf("%d", &ii[h]), ii[h]--;
      |    ^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...