This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 100000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
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 dd[N], pp[N], qq[N], sz[N], ta[N], dp[N];
void dfs1(int p, int i, int d) {
static int time;
int o, j_;
dd[i] = d, pp[i] = p, ta[i] = time++;
dp[i] = eo[i] == 1;
j_ = -1, sz[i] = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs1(i, j, d + 1);
if (j_ == -1 || sz[j_] < sz[j])
j_ = j;
sz[i] += sz[j];
dp[i] += dp[j];
}
}
if (j_ != -1)
qq[j_] = -1;
}
int ans;
void dfs2(int p, int i) {
int o;
if (p != -1) {
ans += dp[i] % 2 == 0 ? 2 : 1;
dp[i] = dp[p] + (dp[i] % 2 == 0 ? -1 : 1);
} else
dp[i] = 0;
qq[i] = qq[i] ? qq[pp[i]] : i;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs2(i, j);
}
}
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 (ii[j] == 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() {
int n, q, h, i, j, l, r;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
}
l = 0, r = -1;
for (i = 0; i < n; i++)
if (eo[i] == 1)
l++;
else
r = i;
dfs1(-1, r, 0);
dfs2(-1, r);
while (q--) {
static int kk[N], ii[N], qu[N];
int k, k_, h, cnt, ans_;
scanf("%d", &k);
for (h = 0; h < k; h++) {
scanf("%d", &ii[h]), ii[h]--;
kk[ii[h]]++;
}
ans_ = ans + k;
k_ = 0;
for (h = 0; h < k; h++) {
if (kk[ii[h]] && (kk[ii[h]] - (eo[ii[h]] == 1 ? 1 : 0)) % 2 != 0)
ii[k_++] = ii[h];
kk[ii[h]] = 0;
}
k = k_;
if ((l + k) % 2 != 0) {
printf("-1\n");
continue;
}
sort(ii, 0, k);
if (k != 0) {
cnt = 0;
if (r != ii[0])
qu[cnt++] = r, qu[cnt++] = ii[0];
for (h = 1; h <= k; h++) {
int a = h == k ? r : lca(ii[h - 1], ii[h]), flip;
flip = 1;
while (cnt >= 2 && dd[qu[cnt - 2]] >= dd[a]) {
if (flip)
ans_ += dp[qu[cnt - 1]] - dp[qu[cnt - 2]];
flip ^= 1;
cnt--;
}
if (cnt > 0) {
if (flip)
ans_ += dp[qu[cnt - 1]] - dp[a], qu[cnt - 1] = a;
else
cnt--;
qu[cnt++] = ii[h];
} else
qu[cnt++] = a, qu[cnt++] = ii[h];
}
}
printf("%d\n", ans_);
}
return 0;
}
Compilation message (stderr)
cleaning.c: In function 'append':
cleaning.c:18:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
18 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
cleaning.c: In function 'main':
cleaning.c:97:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
cleaning.c:101:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
cleaning.c:116:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | scanf("%d", &k);
| ^~~~~~~~~~~~~~~
cleaning.c:118:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d", &ii[h]), ii[h]--;
| ^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |