Submission #1235272

#TimeUsernameProblemLanguageResultExecution timeMemory
1235272badge881Spring cleaning (CEOI20_cleaning)C++20
0 / 100
2 ms4160 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int MAX_N = 100000; vector<vector<int>> adj; vector<int> parity, anc, cost = {2, 1}; int total_cost = 0; int count_leaves(int u, int a) { int s = 0; for (auto v : adj[u]) { if (v != a) { anc[v] = u; s += count_leaves(v, u); } } if (s == 0) s = 1; total_cost += cost[s % 2]; parity[u] = s % 2; return s; } void flip(int u) { total_cost -= cost[parity[u]]; parity[u] ^= 1; total_cost += cost[parity[u]]; if (anc[u] != -1) flip(anc[u]); } signed main() { int n, q; scanf("%d%d", &n, &q); adj.resize(n); parity.resize(n); anc.resize(n, -1); int node = 0; int deg = 0; for (int i = 0; i < n - 1; i++) { int a, b; scanf("%d%d\n", &a, &b); adj[--a].push_back(--b); adj[b].push_back(a); if (adj[a].size() > deg) { node = a; deg = adj[a].size(); } if (adj[b].size() > deg) { node = b; deg = adj[b].size(); } } int r = count_leaves(node, -1); for (int i = 0; i < q; i++) { int d; scanf("%d\n", &d); vector<int> parents; for (int j = 0; j < d; j++) { int v; scanf("%d\n", &v); parents.push_back(--v); adj[parents.back()].push_back(adj.size()); total_cost++; if (adj[parents.back()].size() > 2) { flip(parents.back()); r ^= 1; } } if (parity[node] == 1) printf("-1\n"); else printf("%d\n", total_cost - 2); adj.resize(n); for (auto e : parents) { if (adj[e].size() > 2) { flip(e); r ^= 1; } total_cost--; adj[e].pop_back(); } } }

Compilation message (stderr)

cleaning.cpp: In function 'int main()':
cleaning.cpp:40:13: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
   40 |     scanf("%d%d", &n, &q);
      |            ~^     ~~
      |             |     |
      |             int*  long long int*
      |            %lld
cleaning.cpp:40:15: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'long long int*' [-Wformat=]
   40 |     scanf("%d%d", &n, &q);
      |              ~^       ~~
      |               |       |
      |               int*    long long int*
      |              %lld
cleaning.cpp:50:17: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
   50 |         scanf("%d%d\n", &a, &b);
      |                ~^       ~~
      |                 |       |
      |                 int*    long long int*
      |                %lld
cleaning.cpp:50:19: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'long long int*' [-Wformat=]
   50 |         scanf("%d%d\n", &a, &b);
      |                  ~^         ~~
      |                   |         |
      |                   int*      long long int*
      |                  %lld
cleaning.cpp:69:17: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
   69 |         scanf("%d\n", &d);
      |                ~^     ~~
      |                 |     |
      |                 int*  long long int*
      |                %lld
cleaning.cpp:74:21: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
   74 |             scanf("%d\n", &v);
      |                    ~^     ~~
      |                     |     |
      |                     int*  long long int*
      |                    %lld
cleaning.cpp:87:22: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
   87 |             printf("%d\n", total_cost - 2);
      |                     ~^     ~~~~~~~~~~~~~~
      |                      |                |
      |                      int              long long int
      |                     %lld
cleaning.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |     scanf("%d%d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~
cleaning.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         scanf("%d%d\n", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~~
cleaning.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         scanf("%d\n", &d);
      |         ~~~~~^~~~~~~~~~~~
cleaning.cpp:74:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |             scanf("%d\n", &v);
      |             ~~~~~^~~~~~~~~~~~
#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...