Submission #342427

# Submission time Handle Problem Language Result Execution time Memory
342427 2021-01-02T06:14:38 Z mjhmjh1104 Spring cleaning (CEOI20_cleaning) C++14
34 / 100
1000 ms 20452 KB
#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);
    while (q--) {
        for (int i = 0; i < n; i++) childs[i] = cntChilds[i] = 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) {
            puts("-1");
            continue;
        }
        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\n", res);
    }
}

Compilation message

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:39:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   39 |         scanf("%d", &d);
      |         ~~~~~^~~~~~~~~~
cleaning.cpp:42:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   42 |             scanf("%d", &x); x--;
      |             ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4972 KB Output is correct
2 Execution timed out 1081 ms 7020 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 13 ms 5228 KB Output is correct
2 Correct 13 ms 5228 KB Output is correct
3 Correct 42 ms 10600 KB Output is correct
4 Correct 38 ms 8932 KB Output is correct
5 Correct 49 ms 10600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 5740 KB Output is correct
2 Correct 15 ms 5740 KB Output is correct
3 Correct 75 ms 20452 KB Output is correct
4 Correct 74 ms 19052 KB Output is correct
5 Correct 66 ms 19052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 214 ms 7020 KB Output is correct
2 Correct 141 ms 6636 KB Output is correct
3 Correct 206 ms 6652 KB Output is correct
4 Correct 179 ms 7148 KB Output is correct
5 Correct 199 ms 7276 KB Output is correct
6 Correct 217 ms 7532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1074 ms 9196 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1079 ms 11372 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4972 KB Output is correct
2 Execution timed out 1081 ms 7020 KB Time limit exceeded