Submission #477698

#TimeUsernameProblemLanguageResultExecution timeMemory
477698InternetPerson10Spring cleaning (CEOI20_cleaning)C++17
18 / 100
1086 ms16228 KiB
#include <bits/stdc++.h>
typedef long long ll;

using namespace std;

vector<vector<int>> adj;
vector<int> par, chi, le, ct;

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 br = 1;

int dfs2(int n) {
    int bads = ct[n];
    for(int ch : adj[n]) {
        if(ch == par[n]) continue;
        bads += dfs2(ch);
    }
    chi[n] += br * bads;
    return bads;
}

int main() {
    int n, q;
    scanf("%d %d", &n, &q);
    adj.resize(n);
    par.resize(n);
    chi.resize(n);
    le.resize(n);
    ct.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);
        for(int i = 0; i < k; i++) {
            int x;
            scanf("%d", &x);
            x--;
            if(le[x] == 1) {
                le[x] = -1;
                continue;
            }
            ct[x]++;
        }
        int ans = n-1+k;
        dfs2(r);
        for(int i = 0; i < n; i++) {
            if(i == r) continue;
            if(chi[i]%2 == 0) ans++;
        }
        if(chi[r]%2) ans = -1;
        br = -1;
        dfs2(r);
        br = 1;
        printf("%d\n", ans);
        for(int i = 0; i < n; i++) {
            if(le[i] == -1) {
                le[i] = 1;
                continue;
            }
            ct[i] = 0;
        }
    }
}

Compilation message (stderr)

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