#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <stack>
typedef long long llong;
const int MAXN = 200000 + 10;
const int MOD = 1e9 + 7;
const int INF = 2e9;
int n, q;
int cntEven;
int res[MAXN];
int par[MAXN];
bool was[MAXN];
bool isLeaf[MAXN];
std::vector <int> g[MAXN];
int dfs(int node, int p)
{
par[node] = p;
if (g[node].size() == 1)
{
isLeaf[node] = true;
res[node] = 1;
return res[node];
}
res[node] = 0;
for (const int &u : g[node])
{
if (u == p)
{
continue;
}
res[node] ^= dfs(u, node);
}
cntEven += !res[node];
return res[node];
}
void climb(int node)
{
cntEven -= !res[node];
res[node] ^= 1;
cntEven += !res[node];
if (par[node] != 0)
{
climb(par[node]);
}
}
int a[MAXN];
void solve()
{
int root = 1;
if (g[1].size() == 1) root = g[1][0];
dfs(root, 0);
for (int i = 1 ; i <= q ; ++i)
{
int d;
std::cin >> d;
for (int j = 1 ; j <= d ; ++j)
{
std::cin >> a[j];
if (isLeaf[a[j]] && !was[a[j]]) was[a[j]] = true;
else
{
climb(a[j]);
}
}
if (res[root] == 1) std::cout << -1 << '\n';
else std::cout << n + d + cntEven - 2 << '\n';
for (int j = 1 ; j <= d ; ++j)
{
if (isLeaf[a[j]] && was[a[j]]) was[a[j]] = false;
else
{
climb(a[j]);
}
}
}
}
void input()
{
std::cin >> n >> q;
for (int i = 2 ; i <= n ; ++i)
{
int u, v;
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
19 ms |
7212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
6744 KB |
Output is correct |
2 |
Correct |
6 ms |
6748 KB |
Output is correct |
3 |
Correct |
17 ms |
10356 KB |
Output is correct |
4 |
Correct |
24 ms |
9428 KB |
Output is correct |
5 |
Correct |
25 ms |
10708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
927 ms |
7508 KB |
Output is correct |
2 |
Correct |
923 ms |
7204 KB |
Output is correct |
3 |
Correct |
482 ms |
16208 KB |
Output is correct |
4 |
Execution timed out |
1053 ms |
15664 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1043 ms |
7900 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
9044 KB |
Output is correct |
2 |
Correct |
32 ms |
10076 KB |
Output is correct |
3 |
Correct |
22 ms |
8540 KB |
Output is correct |
4 |
Correct |
31 ms |
10076 KB |
Output is correct |
5 |
Correct |
32 ms |
10064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
10580 KB |
Output is correct |
2 |
Execution timed out |
1089 ms |
13764 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
19 ms |
7212 KB |
Output is correct |
3 |
Correct |
6 ms |
6744 KB |
Output is correct |
4 |
Correct |
6 ms |
6748 KB |
Output is correct |
5 |
Correct |
17 ms |
10356 KB |
Output is correct |
6 |
Correct |
24 ms |
9428 KB |
Output is correct |
7 |
Correct |
25 ms |
10708 KB |
Output is correct |
8 |
Correct |
927 ms |
7508 KB |
Output is correct |
9 |
Correct |
923 ms |
7204 KB |
Output is correct |
10 |
Correct |
482 ms |
16208 KB |
Output is correct |
11 |
Execution timed out |
1053 ms |
15664 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |