답안 #554139

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
554139 2022-04-27T17:26:15 Z Stickfish Spring cleaning (CEOI20_cleaning) C++17
9 / 100
1000 ms 15692 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 1e5 + 123;
int leafcnt[MAXN];
vector<int> edg[MAXN];
int rt[MAXN];

void dfs(int v) {
    if (edg[v].size() == 1)
        leafcnt[v] = 1;
    for (auto u : edg[v]) {
        if (rt[v] == u)
            continue;
        rt[u] = v;
        dfs(u);
        leafcnt[v] += leafcnt[u];
    }
}

signed main() {
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }
    int root = 0;
    for (int i = 0; i < n; ++i) {
        if (edg[i].size() > 1) {
            root = i;
            break;
        }
    }
    rt[root] = -1;
    dfs(root);
    int base = 0;
    for (int i = 0; i < n; ++i) {
        if (i == root)
            continue;
        if (leafcnt[i] % 2)
            ++base;
        else
            base += 2;
    }
        //for (int i = 0; i < n; ++i)
            //cout << leafcnt[i] << ' ';
        //cout << endl;
    while (q--) {
        int d;
        cin >> d;
        vector<int> addv(d);
        for (int i = 0; i < d; ++i) {
            cin >> addv[i];
            --addv[i];
        }
        vector<int> leafrm = addv;
        sort(leafrm.begin(), leafrm.end());
        leafrm.resize(unique(leafrm.begin(), leafrm.end()) - leafrm.begin());
        vector<int> leafadd(leafrm.size());
        for (auto v : addv)
            ++leafadd[lower_bound(leafrm.begin(), leafrm.end(), v) - leafrm.begin()];
        for (int i = 0; i < leafadd.size(); ++i) {
            int u = leafrm[i];
            if (edg[u].size() == 1) {
                --leafadd[i];
            }
            int w = u;
            while (w >= 0) {
                leafcnt[w] += leafadd[i];
                w = rt[w];
            }
        }
        //for (int i = 0; i < n; ++i)
            //cout << leafcnt[i] << ' ';
        //cout << endl;
        int ans = d;
        if (leafcnt[root] % 2) {
            cout << -1 << '\n';
        } else {
            for (int i = 0; i < n; ++i) {
                if (i == root)
                    continue;
                if (leafcnt[i] % 2)
                    ++ans;
                else
                    ans += 2;
            }
            cout << ans << '\n';
        }
        for (int i = 0; i < leafadd.size(); ++i) {
            int u = leafrm[i];
            int w = u;
            while (w >= 0) {
                leafcnt[w] -= leafadd[i];
                w = rt[w];
            }
        }

    }
}

Compilation message

cleaning.cpp: In function 'int main()':
cleaning.cpp:68:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |         for (int i = 0; i < leafadd.size(); ++i) {
      |                         ~~^~~~~~~~~~~~~~~~
cleaning.cpp:96:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |         for (int i = 0; i < leafadd.size(); ++i) {
      |                         ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 134 ms 4248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 3752 KB Output is correct
2 Correct 31 ms 3756 KB Output is correct
3 Correct 52 ms 7744 KB Output is correct
4 Correct 74 ms 7540 KB Output is correct
5 Correct 98 ms 9172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 4444 KB Output is correct
2 Correct 86 ms 4300 KB Output is correct
3 Correct 494 ms 15440 KB Output is correct
4 Execution timed out 1090 ms 15692 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1081 ms 4788 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1073 ms 6792 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1083 ms 8424 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 134 ms 4248 KB Output is correct
3 Correct 30 ms 3752 KB Output is correct
4 Correct 31 ms 3756 KB Output is correct
5 Correct 52 ms 7744 KB Output is correct
6 Correct 74 ms 7540 KB Output is correct
7 Correct 98 ms 9172 KB Output is correct
8 Correct 87 ms 4444 KB Output is correct
9 Correct 86 ms 4300 KB Output is correct
10 Correct 494 ms 15440 KB Output is correct
11 Execution timed out 1090 ms 15692 KB Time limit exceeded
12 Halted 0 ms 0 KB -