This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 12, MOD = (int)1e9 + 7;
int n, q, rt;
ll cur = 0;
int dp[N], s[N];
vector<int> g[N];
void dfs(int v, int pr = -1) {
s[v] = 0;
bool is = 1;
for(int to:g[v]) {
if(to == pr) continue;
is = 0;
dfs(to, v);
s[v] += s[to];
cur += (s[to] & 1 ? 1 : 2);
}
s[v] += (is);
}
void test() {
cin >> n >> q;
for(int i = 1; i <= n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i = 1; i <= n; i++) {
if((int)g[i].size() != 1) {
rt = i;
break;
}
}
while(q--) {
cur =0 ;
int d;
cin >> d;
vector<int> a(d);
for(int j = 0; j < d; j++) {
cin >> a[j];
g[a[j]].push_back(n + j + 1);
}
dfs(rt);
if(s[rt] & 1) {
cur = -1;
}
cout << cur << '\n';
for(int i = 0; i < d; ++i) {
g[a[i]].pop_back();
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |