Submission #993790

#TimeUsernameProblemLanguageResultExecution timeMemory
993790TimDeeRailway (BOI17_railway)C++17
Compilation error
0 ms0 KiB
// I solved this problem in contest (it appeared on our TST2024), but i'm too lazy to code it again, so it's a copy of rainboy's submission.
    #include <stdio.h>
    #include <stdlib.h>
    #define N	100000
     
    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.cpp: In function 'void append(int, int)':
railway.cpp:12:27: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   12 |      if (o >= 2 && (o & o - 1) == 0)
      |                         ~~^~~
railway.cpp: In function 'void sort(int*, int, int)':
railway.cpp:51:44: error: 'rand_' was not declared in this scope; did you mean 'rand_r'?
   51 |       int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
      |                                            ^~~~~
      |                                            rand_r
railway.cpp:57:9: error: 'tmp' was not declared in this scope
   57 |         tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
      |         ^~~
railway.cpp:61:9: error: 'tmp' was not declared in this scope
   61 |         tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
      |         ^~~
railway.cpp: In function 'int main()':
railway.cpp:73:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |      scanf("%d%d%d", &n, &m, &k);
      |      ~~~~~^~~~~~~~~~~~~~~~~~~~~~
railway.cpp:77:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |       scanf("%d%d", &i, &j), i--, j--;
      |       ~~~~~^~~~~~~~~~~~~~~~
railway.cpp:89:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |       scanf("%d", &cnt);
      |       ~~~~~^~~~~~~~~~~~
railway.cpp:91:13: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |        scanf("%d", &ii[h]), ii[h]--;
      |        ~~~~~^~~~~~~~~~~~~~