제출 #795967

#제출 시각아이디문제언어결과실행 시간메모리
795967NeroZeinSpring cleaning (CEOI20_cleaning)C++17
9 / 100
45 ms8296 KiB
#include "bits/stdc++.h" using namespace std; #ifdef Nero #include "Deb.h" #else #define deb(...) #endif const int N = 100005; int leaves; bool leaf[N]; vector<int> g[N]; void dfs (int v, int p) { if (g[v].size() == 1) { leaves++; leaf[v] = true; } for (int u : g[v]) { if (u != p) { dfs(u, v); } } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1, 1); while (q--) { int d; cin >> d; vector<int> nodes(d); for (int i = 0; i < d; ++i) { cin >> nodes[i]; } int ans = leaves; int tleaves = leaves + d; sort(nodes.begin(), nodes.end()); for (int i = 0; i < d; ++i) { int j = i; while (j + 1 < d && nodes[j + 1] == nodes[j]) { j++; } if (leaf[nodes[i]]) { tleaves--; ans--; } int ch = j - i + 1; int match_together = ch - (2 - (ch % 2)); ans += match_together; ans += (ch - match_together) * 2; i = j; } if (tleaves % 2) { 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...