Submission #294228

#TimeUsernameProblemLanguageResultExecution timeMemory
294228spdskatrSpring cleaning (CEOI20_cleaning)C++14
100 / 100
259 ms26992 KiB
#include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> using namespace std; int N, Q, leaf[100005], lvs[100005], depth[100005], evens[100005], jp[18][100005], rawtot, seen[100005]; int sz[100005], ind[100005], offset[100005], cnt, newans; vector<int> graph[100005], g2[100005]; vector<int> nl, newnodes, comps; void dfs1(int x, int p) { //printf("dfs1(%d, %d)\n", x, p); leaf[x] = 1; ind[x] = cnt++; sz[x] = 1; jp[0][x] = p; for (int v : graph[x]) { if (v != p) { leaf[x] = 0; depth[v] = depth[x] + 1; dfs1(v, x); lvs[x] += lvs[v]; sz[x] += sz[v]; } } if (x == 1 && graph[x].size() <= 1) leaf[x] = 1; lvs[x] += leaf[x]; } void dfs2(int x, int p) { if (x != 1) rawtot += 1 + ((lvs[x] % 2) ^ 1); evens[x] += ((lvs[x] % 2) ^ 1); for (int v : graph[x]) { if (v != p) { evens[v] = evens[x]; dfs2(v, x); } } } void dfs3(int x, int p) { //printf("dfs3(%d, %d)\n", x, p); for (int v : g2[x]) { dfs3(v, x); offset[x] += offset[v]; } if (offset[x] & 1) { newans += depth[x] - depth[p] - 2 * (evens[x] - evens[p]); } } int isanc(int a, int b) { return ind[a] <= ind[b] && ind[a] + sz[a] > ind[b]; } int lca(int u, int v) { if (isanc(u, v)) return u; if (isanc(v, u)) return v; int cur = u; for (int i = 17; i >= 0; i--) { if (!isanc(jp[i][cur], v)) cur = jp[i][cur]; } return jp[0][cur]; } int main() { scanf("%d %d", &N, &Q); for (int i = 0; i < N-1; i++) { int u, v; scanf("%d %d", &u, &v); graph[u].push_back(v); graph[v].push_back(u); } dfs1(1, 1); dfs2(1, 1); for (int b = 1; b < 18; b++) { for (int i = 1; i <= N; i++) { jp[b][i] = jp[b-1][jp[b-1][i]]; } } for (int q = 0; q < Q; q++) { //printf("==========\n"); int d; scanf("%d", &d); nl.clear(); for (int i = 0; i < d; i++) { int a; scanf("%d", &a); nl.push_back(a); } int createdleaves = 0; sort(nl.begin(), nl.end(), [](int l, int r){ return ind[l] < ind[r]; }); for (int i = 0; i < nl.size(); i++) { if (i > 0 && nl[i-1] == nl[i]) { offset[nl[i]]++; createdleaves++; } else { //printf("new node: %d\n", nl[i]); seen[nl[i]] = 1; newnodes.push_back(nl[i]); if (i == 0) { comps.push_back(nl[i]); } else { int l = lca(nl[i], nl[i-1]); if (!seen[l]) newnodes.push_back(l); seen[l] = 1; if (l != nl[i-1]) { while (comps.size() && depth[l] < depth[comps.back()]) { if (comps.size() > 1 && depth[comps[comps.size() - 2]] > depth[l]) { g2[comps[comps.size() - 2]].push_back(comps.back()); //printf("Added compressed tree edge %d -> %d\n", comps[comps.size() - 2], comps.back()); } else { g2[l].push_back(comps.back()); //printf("Added compressed tree edge %d -> %d\n", l, comps.back()); } comps.pop_back(); } if (comps.empty() || comps.back() != l) comps.push_back(l); } comps.push_back(nl[i]); } if (!leaf[nl[i]]) createdleaves++; offset[nl[i]] = 1 - leaf[nl[i]]; } } while (comps.size() > 1) { g2[comps[comps.size()-2]].push_back(comps.back()); //printf("Added compressed tree edge %d -> %d\n", comps[comps.size() - 2], comps.back()); comps.pop_back(); } newans = rawtot + d; dfs3(comps[0], 1); if ((createdleaves + lvs[1]) & 1) { printf("-1\n"); } else { printf("%d\n", newans); } // CLEANUP!!! newans = 0; for (int v : newnodes) { //printf("New nodes: %d\n", v); g2[v].clear(); offset[v] = 0; seen[v] = 0; } newnodes.clear(); comps.clear(); } }

Compilation message (stderr)

cleaning.cpp: In function 'int main()':
cleaning.cpp:97:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |   for (int i = 0; i < nl.size(); i++) {
      |                   ~~^~~~~~~~~~~
cleaning.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |  scanf("%d %d", &N, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
cleaning.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   72 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
cleaning.cpp:86:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   86 |   scanf("%d", &d);
      |   ~~~~~^~~~~~~~~~
cleaning.cpp:90:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   90 |    scanf("%d", &a);
      |    ~~~~~^~~~~~~~~~
#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...