Submission #342424

#TimeUsernameProblemLanguageResultExecution timeMemory
342424mjhmjh1104Spring cleaning (CEOI20_cleaning)C++14
18 / 100
107 ms21868 KiB
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 100006;

int n, q, d, childs[MAX], cntChilds[MAX], depth[MAX], totalChilds[MAX];
vector<int> adj[MAX], c[MAX];

void dfs(int x, int prev = -1) {
    for (auto &i: adj[x]) if (i != prev) {
        depth[i] = depth[x] + 1;
        dfs(i, x);
        c[x].push_back(i);
    }
}

void dfs1(int x) {
    totalChilds[x] = cntChilds[x];
    for (auto &i: c[x]) {
        dfs1(i);
        totalChilds[x] += totalChilds[i];
    }
}

int main() {
    scanf("%d%d", &n, &q);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--, b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(0);

    scanf("%d", &d);
    for (int i = 0; i < d; i++) {
        int x;
        scanf("%d", &x); x--;
        childs[x]++;
    }
    cntChilds[0] += childs[0];
    if (!childs[0] && (int)c[0].size() == 1) cntChilds[0]++;
    for (int i = 1; i < n; i++) {
        cntChilds[i] += childs[i];
        if (!childs[i] && c[i].empty()) cntChilds[i]++;
    }
    dfs1(0);
    if (totalChilds[0] % 2) return puts("-1"), 0;
    long long res = d;
    for (int i = 1; i < n; i++) {
        int t = totalChilds[i];
        if (t > 2) t -= (t - 1) / 2 * 2;
        res += t;
    }
    printf("%lld", res);
}

Compilation message (stderr)

cleaning.cpp: In function 'int main()':
cleaning.cpp:28:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   28 |     scanf("%d%d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~
cleaning.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   31 |         scanf("%d%d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~
cleaning.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   38 |     scanf("%d", &d);
      |     ~~~~~^~~~~~~~~~
cleaning.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   41 |         scanf("%d", &x); 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...