#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;
std::vector <int> g[MAXN];
std::pair <int,int> dfs(int node, int par)
{
if (g[node].size() == 1)
{
return {0, 1};
}
int cnt = 0;
int cost = 0;
for (const int &u : g[node])
{
if (u == par) continue;
auto [res, rem] = dfs(u, node);
cost += res;
if (rem == 0)
{
cnt += 2;
cost += 2;
} else
{
cnt += rem;
cost += rem;
}
}
return {cost, cnt & 1};
}
int calc()
{
int root = 1;
if (g[1].size() == 1) root = g[1][0];
if (g[root].size() == 1) return 1;
auto [res, rem] = dfs(root, 0);
if (rem == 0) return res;
return -1;
}
int a[MAXN];
void solve()
{
for (int i = 1 ; i <= q ; ++i)
{
int d;
int cnt = n;
std::cin >> d;
for (int j = 1 ; j <= d ; ++j)
{
std::cin >> a[j];
g[a[j]].push_back(++cnt);
g[cnt].push_back(a[j]);
}
std::cout << calc() << '\n';
for (int j = 1 ; j <= d ; ++j)
{
g[a[j]].pop_back();
g[cnt--].pop_back();
}
}
}
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 |
5724 KB |
Output is correct |
2 |
Execution timed out |
1095 ms |
6700 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
8792 KB |
Output is correct |
2 |
Correct |
15 ms |
8796 KB |
Output is correct |
3 |
Correct |
22 ms |
9448 KB |
Output is correct |
4 |
Correct |
33 ms |
11080 KB |
Output is correct |
5 |
Correct |
47 ms |
12172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
9308 KB |
Output is correct |
2 |
Correct |
15 ms |
9308 KB |
Output is correct |
3 |
Correct |
48 ms |
15068 KB |
Output is correct |
4 |
Correct |
77 ms |
17264 KB |
Output is correct |
5 |
Correct |
41 ms |
14164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
6964 KB |
Output is correct |
2 |
Correct |
104 ms |
6552 KB |
Output is correct |
3 |
Correct |
140 ms |
6384 KB |
Output is correct |
4 |
Correct |
187 ms |
6792 KB |
Output is correct |
5 |
Correct |
162 ms |
6748 KB |
Output is correct |
6 |
Correct |
190 ms |
6872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1093 ms |
7792 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1006 ms |
8784 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Execution timed out |
1095 ms |
6700 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |