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 <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <stack>
typedef long long llong;
const int MAXN = 200000 + 10;
const int MOD = 1e9 + 7;
const int INF = 2e9;
int n, q;
int cntEven;
int res[MAXN];
int par[MAXN];
bool was[MAXN];
bool isLeaf[MAXN];
std::vector <int> g[MAXN];
int dfs(int node, int p)
{
par[node] = p;
if (g[node].size() == 1)
{
isLeaf[node] = true;
res[node] = 1;
return res[node];
}
res[node] = 0;
for (const int &u : g[node])
{
if (u == p)
{
continue;
}
res[node] ^= dfs(u, node);
}
cntEven += !res[node];
return res[node];
}
void climb(int node)
{
cntEven -= !res[node];
res[node] ^= 1;
cntEven += !res[node];
if (par[node] != 0)
{
climb(par[node]);
}
}
int a[MAXN];
void solve()
{
int root = 1;
if (g[1].size() == 1) root = g[1][0];
dfs(root, 0);
for (int i = 1 ; i <= q ; ++i)
{
int d;
std::cin >> d;
for (int j = 1 ; j <= d ; ++j)
{
std::cin >> a[j];
if (isLeaf[a[j]] && !was[a[j]]) was[a[j]] = true;
else
{
climb(a[j]);
}
}
if (res[root] == 1) std::cout << -1 << '\n';
else std::cout << n + d + cntEven - 2 << '\n';
for (int j = 1 ; j <= d ; ++j)
{
if (isLeaf[a[j]] && was[a[j]]) was[a[j]] = false;
else
{
climb(a[j]);
}
}
}
}
void input()
{
std::cin >> n >> q;
for (int i = 2 ; i <= n ; ++i)
{
int u, v;
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
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... |