// ¯\_(ツ)_/¯
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 1e6 + 10;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;
int n, q, sub_g[MAXN], r;
ll ans = 0;
vector<int> adj[MAXN];
void dfs(int v, int p) {
if (adj[v].size() <= 1) {
ans++;
sub_g[v] = 1;
return;
}
for (int u : adj[v]) {
if (u == p) continue;
dfs(u, v);
sub_g[v] += sub_g[u];
}
if (v == r) return;
if (sub_g[v] & 1) ans++;
else ans += 2;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[v].push_back(u);
adj[u].push_back(v);
if (adj[v].size() > 1) r = v;
if (adj[u].size() > 1) r = u;
}
while (q--) {
int k;
cin >> k;
int t = n + 1;
for (int i = 0; i < k; i++) {
int v;
cin >> v;
adj[v].push_back(t++);
}
ans = 0;
dfs(r, 0);
cout << (sub_g[r] % 2 == 0 ? ans : -1) << endl;
for (int i = 1; i <= t + 10; i++) {
while (!adj[i].empty() && adj[i].back() > n)
adj[i].pop_back();
sub_g[i] = 0;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23756 KB |
Output is correct |
2 |
Execution timed out |
1078 ms |
25104 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
25076 KB |
Output is correct |
2 |
Correct |
32 ms |
25096 KB |
Output is correct |
3 |
Correct |
56 ms |
28484 KB |
Output is correct |
4 |
Correct |
71 ms |
28048 KB |
Output is correct |
5 |
Correct |
120 ms |
29420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
25556 KB |
Output is correct |
2 |
Correct |
31 ms |
25436 KB |
Output is correct |
3 |
Correct |
76 ms |
33536 KB |
Output is correct |
4 |
Correct |
92 ms |
34376 KB |
Output is correct |
5 |
Correct |
72 ms |
33316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
26008 KB |
Output is correct |
2 |
Correct |
196 ms |
24944 KB |
Output is correct |
3 |
Correct |
271 ms |
24656 KB |
Output is correct |
4 |
Correct |
328 ms |
25604 KB |
Output is correct |
5 |
Correct |
263 ms |
25420 KB |
Output is correct |
6 |
Correct |
323 ms |
25664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1092 ms |
26984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1075 ms |
28740 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23756 KB |
Output is correct |
2 |
Execution timed out |
1078 ms |
25104 KB |
Time limit exceeded |