#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
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 |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
27 ms |
2452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
1228 KB |
Output is correct |
2 |
Correct |
10 ms |
1192 KB |
Output is correct |
3 |
Correct |
47 ms |
8624 KB |
Output is correct |
4 |
Correct |
40 ms |
6996 KB |
Output is correct |
5 |
Correct |
65 ms |
9408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1712 KB |
Output is correct |
2 |
Correct |
16 ms |
1740 KB |
Output is correct |
3 |
Correct |
45 ms |
16164 KB |
Output is correct |
4 |
Correct |
66 ms |
15664 KB |
Output is correct |
5 |
Correct |
39 ms |
14636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
2932 KB |
Output is correct |
2 |
Correct |
14 ms |
2088 KB |
Output is correct |
3 |
Correct |
9 ms |
1964 KB |
Output is correct |
4 |
Correct |
21 ms |
2468 KB |
Output is correct |
5 |
Correct |
9 ms |
2388 KB |
Output is correct |
6 |
Correct |
23 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
6440 KB |
Output is correct |
2 |
Correct |
61 ms |
6212 KB |
Output is correct |
3 |
Correct |
38 ms |
3532 KB |
Output is correct |
4 |
Correct |
56 ms |
6188 KB |
Output is correct |
5 |
Correct |
61 ms |
6296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
9920 KB |
Output is correct |
2 |
Correct |
108 ms |
12652 KB |
Output is correct |
3 |
Correct |
95 ms |
12120 KB |
Output is correct |
4 |
Correct |
97 ms |
12512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
27 ms |
2452 KB |
Output is correct |
3 |
Correct |
9 ms |
1228 KB |
Output is correct |
4 |
Correct |
10 ms |
1192 KB |
Output is correct |
5 |
Correct |
47 ms |
8624 KB |
Output is correct |
6 |
Correct |
40 ms |
6996 KB |
Output is correct |
7 |
Correct |
65 ms |
9408 KB |
Output is correct |
8 |
Correct |
10 ms |
1712 KB |
Output is correct |
9 |
Correct |
16 ms |
1740 KB |
Output is correct |
10 |
Correct |
45 ms |
16164 KB |
Output is correct |
11 |
Correct |
66 ms |
15664 KB |
Output is correct |
12 |
Correct |
39 ms |
14636 KB |
Output is correct |
13 |
Correct |
21 ms |
2932 KB |
Output is correct |
14 |
Correct |
14 ms |
2088 KB |
Output is correct |
15 |
Correct |
9 ms |
1964 KB |
Output is correct |
16 |
Correct |
21 ms |
2468 KB |
Output is correct |
17 |
Correct |
9 ms |
2388 KB |
Output is correct |
18 |
Correct |
23 ms |
2808 KB |
Output is correct |
19 |
Correct |
44 ms |
6440 KB |
Output is correct |
20 |
Correct |
61 ms |
6212 KB |
Output is correct |
21 |
Correct |
38 ms |
3532 KB |
Output is correct |
22 |
Correct |
56 ms |
6188 KB |
Output is correct |
23 |
Correct |
61 ms |
6296 KB |
Output is correct |
24 |
Correct |
99 ms |
9920 KB |
Output is correct |
25 |
Correct |
108 ms |
12652 KB |
Output is correct |
26 |
Correct |
95 ms |
12120 KB |
Output is correct |
27 |
Correct |
97 ms |
12512 KB |
Output is correct |
28 |
Correct |
52 ms |
5984 KB |
Output is correct |
29 |
Correct |
68 ms |
11484 KB |
Output is correct |
30 |
Correct |
56 ms |
9452 KB |
Output is correct |
31 |
Correct |
57 ms |
15656 KB |
Output is correct |
32 |
Correct |
57 ms |
6292 KB |
Output is correct |
33 |
Correct |
76 ms |
10644 KB |
Output is correct |
34 |
Correct |
111 ms |
11336 KB |
Output is correct |
35 |
Correct |
86 ms |
12260 KB |
Output is correct |