#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define rep(i,a,b) for (lli i = (a); i <= (b); i++)
#define repa(i,a,b) for (lli i = (a); i >= (b); i--)
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define MAX 100000
lli n,a,b,q,d,res,total,raiz;
vector<lli> hijos[MAX+2];
lli val[MAX+2],borr[MAX+2],padres[MAX+2];
lli DFS(lli pos) {
if (hijos[pos].size() == 1) return 1;
for (auto h : hijos[pos]) {
if (h == padres[pos]) continue;
padres[h] = pos;
val[pos] += DFS(h);
}
res = !(val[pos] % 2) ? res + 1 : res;
return val[pos];
}
void agrega(lli pos) {
while (pos > 0) {
borr[pos]++;
if (borr[pos] == 1) break;
if (borr[pos]%2) ++res;
else --res;
pos = padres[pos];
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
rep(i,1,n-1) {
cin >> a >> b;
hijos[a].push_back(b);
hijos[b].push_back(a);
if (raiz != 0) continue;
if (hijos[a].size() > 1) raiz = a;
if (hijos[b].size() > 1) raiz = b;
}
res = 0;
a = DFS(raiz);
res += n - 1;
if (!(val[raiz] % 2)) --res;
rep(Q,1,q) {
rep(i,1,n) borr[i] = val[i];
total = res;
cin >> d;
rep(i,1,d) {
cin >> a;
total++;
agrega(a);
}
if (borr[raiz] % 2) cout << -1 << "\n";
else cout << total << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
2852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
308 ms |
3112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1082 ms |
4300 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1084 ms |
7256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1075 ms |
8804 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |