#include <bits/stdc++.h>
using namespace std;
#define int ll
#define endl '\n' //comment for interactive
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl << flush
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
typedef array<int, 3> ti;
typedef vector<ii> vii;
typedef vector<ti> vti;
typedef vector<vi> vvi;
typedef priority_queue<int> pq;
template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }
const int inf = 1e17;
const int maxn = 2e5+5;
vi adj[maxn];
void solve() {
int n, q;
cin >> n >> q;
loop1(i, n-1) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
while(q--) {
int d; cin >> d;
vi a(d+1);
loop1(i, d) cin >> a[i];
loop1(i, d) {
adj[a[i]].pb(n+i);
adj[n+i].pb(a[i]);
}
vi lv(n+d+1);
int res = 0;
auto dfs = [&] (int u, int p, auto dfs) -> void {
for(auto v : adj[u]) if(v != p) {
dfs(v, u, dfs);
lv[u] += lv[v];
}
if(adj[u].size() == 1) lv[u] += 1;
if(u != p) res += 1 + !(lv[u] & 1);
};
dfs(1, 1, dfs);
if(lv[1] & 1) cout << -1 << endl;
else cout << res << endl;
loop1(i, d) {
adj[a[i]].pop_back();
adj[n+i].pop_back();
}
}
}
signed main() {
fast_io;
int t = 1; //cin >> t;
while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Execution timed out |
1079 ms |
6496 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
10068 KB |
Output is correct |
2 |
Correct |
23 ms |
10068 KB |
Output is correct |
3 |
Correct |
29 ms |
10164 KB |
Output is correct |
4 |
Correct |
51 ms |
14084 KB |
Output is correct |
5 |
Correct |
60 ms |
15516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
10512 KB |
Output is correct |
2 |
Correct |
20 ms |
10452 KB |
Output is correct |
3 |
Correct |
42 ms |
13460 KB |
Output is correct |
4 |
Correct |
78 ms |
19244 KB |
Output is correct |
5 |
Correct |
39 ms |
13372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
6796 KB |
Output is correct |
2 |
Correct |
166 ms |
6360 KB |
Output is correct |
3 |
Correct |
232 ms |
6152 KB |
Output is correct |
4 |
Correct |
258 ms |
6356 KB |
Output is correct |
5 |
Correct |
229 ms |
6672 KB |
Output is correct |
6 |
Correct |
270 ms |
6860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1079 ms |
8544 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1039 ms |
9780 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Execution timed out |
1079 ms |
6496 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |