#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<vv> vvv;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef pair<ll,ll> p;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef pair<ll, p> tri;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
ll getLeafCount(ll i, vv &graph, v &res)
{
if (res[i] != -1) return 0;
res[i] = 0;
if (graph[i].size() == 1)
{
res[i] = 1;
return 1;
}
for (ll x : graph[i]) res[i] += getLeafCount(x, graph, res);
return res[i];
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll N, Q;
cin >> N >> Q;
vv graph(N);
for (ll i = 0; i < N - 1; i++)
{
ll a, b;
cin >> a >> b; a--; b--;
graph[a].pb(b);
graph[b].pb(a);
}
for (ll j = 0; j < Q; j++)
{
ll amt;
cin >> amt;
vv newGraph(N + amt);
for (ll i = 0; i < N; i++) for (ll x : graph[i]) newGraph[i].pb(x);
for (ll j = 0; j < amt; j++)
{
ll par;
cin >> par; par--;
newGraph[N + j].pb(par);
newGraph[par].pb(N + j);
}
ll root = 0;
while (newGraph[root].size() == 1) root++;
v leafCount(N + amt, -1);
getLeafCount(root, newGraph, leafCount);
if (leafCount[root] % 2 == 1)
{
cout << "-1\n";
continue;
}
ll res = N + amt - 2;
for (ll i= 0 ; i < N; i++) if (leafCount[i] % 2 == 0) res++;
cout << res << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
1097 ms |
3924 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
7004 KB |
Output is correct |
2 |
Correct |
11 ms |
7124 KB |
Output is correct |
3 |
Correct |
24 ms |
14672 KB |
Output is correct |
4 |
Correct |
38 ms |
16908 KB |
Output is correct |
5 |
Correct |
41 ms |
21064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
7772 KB |
Output is correct |
2 |
Correct |
12 ms |
7732 KB |
Output is correct |
3 |
Correct |
34 ms |
17948 KB |
Output is correct |
4 |
Correct |
46 ms |
23384 KB |
Output is correct |
5 |
Correct |
28 ms |
16076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
576 ms |
4300 KB |
Output is correct |
2 |
Correct |
362 ms |
3576 KB |
Output is correct |
3 |
Correct |
489 ms |
3284 KB |
Output is correct |
4 |
Correct |
592 ms |
3504 KB |
Output is correct |
5 |
Correct |
550 ms |
3764 KB |
Output is correct |
6 |
Correct |
578 ms |
4092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1006 ms |
10072 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1025 ms |
14672 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
1097 ms |
3924 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |