//Dost SEFEROĞLU
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define vi vector<int>
#define all(xx) xx.begin(),xx.end()
#define ps(xxx) cout << (xxx) << endl;
const int N = 1e5+1,inf = 1e16,MOD = 1e9+7;
vi edges[N],chi(N,0),sublf(N,0),par(N);
void dfs(int node,int p) {
par[node] = p;
for (auto it : edges[node]) {
if (it == p) continue;
chi[node]++;
dfs(it,node);
sublf[node]+=sublf[it];
}
if (!chi[node]) sublf[node] = 1;
}
void solve() {
int n,q;
cin >> n >> q;
vi deg(n+1,0);
for (int i=1;i<=n-1;i++) {
int a,b;
cin >> a >> b;
edges[a].push_back(b);
edges[b].push_back(a);
deg[a]++,deg[b]++;
}
int leaf = 0;
int root = -1;
for (int i=1;i<=n;i++) {
if (deg[i] >= 2) {
root = i;
break;
}
}
for (int i=1;i<=n;i++) if (deg[i] == 1) leaf++;
dfs(root,root);
vi v;
while (q--) {
int k;
cin >> k;
int ans = 0;
for (int j = 1;j<=k;j++) {
int x;
cin >> x;
v.push_back(x);
if (deg[x] != 1) {
leaf++;
int cur = x;
while (cur != root) {
sublf[cur]^=1;
cur = par[cur];
}
}
deg[x]++;
}
for (int i=1;i<=n;i++) if (i != root) ans+=(sublf[i]%2==0);
if (leaf%2) cout << -1 << '\n';
else cout << n+k-1+ans << '\n';
for (auto x : v) {
int cur = x;
if (deg[x] != 1) {
leaf--;
while (cur != root) {
sublf[cur]^=1;
cur = par[cur];
}
}
deg[x]--;
}
v.clear();
}
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4944 KB |
Output is correct |
2 |
Incorrect |
63 ms |
6768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
6608 KB |
Output is correct |
2 |
Correct |
15 ms |
6608 KB |
Output is correct |
3 |
Correct |
25 ms |
10692 KB |
Output is correct |
4 |
Correct |
33 ms |
10948 KB |
Output is correct |
5 |
Correct |
40 ms |
12476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1063 ms |
7312 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1048 ms |
7248 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1061 ms |
9800 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1052 ms |
11376 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4944 KB |
Output is correct |
2 |
Incorrect |
63 ms |
6768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |