Submission #477699

#TimeUsernameProblemLanguageResultExecution timeMemory
477699InternetPerson10Spring cleaning (CEOI20_cleaning)C++17
9 / 100
1094 ms7632 KiB
#include <bits/stdc++.h>
typedef long long ll;
 
using namespace std;
 
vector<vector<int>> adj;
vector<int> par, chi, le, bad;
 
int dfs(int n, int pa = -1) {
    int childs = 0;
    par[n] = pa;
    for(int ch : adj[n]) {
        if(ch == pa) continue;
        childs += dfs(ch, n);
    }
    if(adj[n].size() == 1)  {
        le[n] = 1;
        return chi[n] = 1;
    }
    return chi[n] = childs;
}
 
int main() {
    int n, q;
    scanf("%d %d", &n, &q);
    adj.resize(n);
    par.resize(n);
    chi.resize(n);
    le.resize(n);
    for(int i = 1; i < n; i++) {
        int x, y;
        scanf("%d %d", &x, &y);
        x--; y--;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    int r = 0;
    while(adj[r].size() == 1) r++;
    dfs(r);
    while(q--) {
        int k;
        scanf("%d", &k);
        bad.resize(k);
        for(int i = 0; i < k; i++) {
            int x;
            scanf("%d", &x);
            x--;
            bad[i] = x;
            while(x != -1) {
                if(le[x] == 1) {
                    le[x] = -1;
                    break;
                }
                chi[x]++;
                x = par[x];
            }
        }
        int ans = n-1+bad.size();
        for(int i = 0; i < n; i++) {
            if(i == r) continue;
            if(chi[i]%2 == 0) ans++;
        }
        if(chi[r]%2) ans = -1;
        printf("%d\n", ans);
        for(int g : bad) {
            int x = g;
            while(x != -1) {
                if(le[x] == -1) {
                    le[x] = 1;
                    break;
                }
                chi[x]--;
                x = par[x];
            }
        }
        vector<int>().swap(bad);
    }
}

Compilation message (stderr)

cleaning.cpp: In function 'int main()':
cleaning.cpp:25:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
cleaning.cpp:32:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
cleaning.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         scanf("%d", &k);
      |         ~~~~~^~~~~~~~~~
cleaning.cpp:46:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |             scanf("%d", &x);
      |             ~~~~~^~~~~~~~~~
#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...