이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// ¯\_(ツ)_/¯
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 1e6 + 10;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;
int n, q, sub_g[MAXN], r;
ll ans = 0;
vector<int> adj[MAXN];
void dfs(int v, int p) {
if (adj[v].size() <= 1) {
ans++;
sub_g[v] = 1;
return;
}
for (int u : adj[v]) {
if (u == p) continue;
dfs(u, v);
sub_g[v] += sub_g[u];
}
if (v == r) return;
if (sub_g[v] & 1) ans++;
else ans += 2;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[v].push_back(u);
adj[u].push_back(v);
if (adj[v].size() > 1) r = v;
if (adj[u].size() > 1) r = u;
}
while (q--) {
int k;
cin >> k;
int t = n + 1;
for (int i = 0; i < k; i++) {
int v;
cin >> v;
adj[v].push_back(t++);
}
ans = 0;
dfs(r, 0);
cout << (sub_g[r] % 2 == 0 ? ans : -1) << endl;
for (int i = 1; i <= t + 10; i++) {
while (!adj[i].empty() && adj[i].back() > n)
adj[i].pop_back();
sub_g[i] = 0;
}
}
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... |