Submission #993792

#TimeUsernameProblemLanguageResultExecution timeMemory
993792TimDeeRailway (BOI17_railway)C++17
100 / 100
74 ms19372 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 unsigned 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.cpp: In function 'void append(int, int)':
railway.cpp:14:31: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   14 |          if (o >= 2 && (o & o - 1) == 0)
      |                             ~~^~~
railway.cpp: In function 'int main()':
railway.cpp:75:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |          scanf("%d%d%d", &n, &m, &k);
      |          ~~~~~^~~~~~~~~~~~~~~~~~~~~~
railway.cpp:79:16: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |           scanf("%d%d", &i, &j), i--, j--;
      |           ~~~~~^~~~~~~~~~~~~~~~
railway.cpp:91:16: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |           scanf("%d", &cnt);
      |           ~~~~~^~~~~~~~~~~~
railway.cpp:93:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |            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...