Submission #578137

#TimeUsernameProblemLanguageResultExecution timeMemory
578137tengiz05Spring cleaning (CEOI20_cleaning)C++17
34 / 100
1095 ms21336 KiB
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; vector<vector<int>> e(n); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--; v--; e[u].push_back(v); e[v].push_back(u); } while (q--) { int k; cin >> k; vector<int> d(k); for (int i = 0; i < k; i++) { cin >> d[i]; d[i]--; } auto adj = e; adj.resize(n + k); for (int i = 0; i < k; i++) { adj[d[i]].push_back(i + n); } int ans = 0; function<int(int, int)> dfs = [&](int u, int p) { int c = adj[u].size() <= 1; for (int v : adj[u]) { if (v != p) { int s = dfs(v, u); if (s % 2 == 0) { ans += 2; } else { ans += 1; } c += s; } } return c; }; int c = dfs(0, -1); if (c % 2 == 1) { cout << "-1\n"; } else { cout << ans << "\n"; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...