답안 #477698

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
477698 2021-10-03T07:12:34 Z InternetPerson10 Spring cleaning (CEOI20_cleaning) C++17
18 / 100
1000 ms 16228 KB
#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

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);
      |             ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Execution timed out 1085 ms 2100 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 844 KB Output is correct
2 Correct 9 ms 844 KB Output is correct
3 Correct 38 ms 8532 KB Output is correct
4 Correct 36 ms 6552 KB Output is correct
5 Correct 53 ms 8884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1432 KB Output is correct
2 Correct 10 ms 1452 KB Output is correct
3 Correct 71 ms 16228 KB Output is correct
4 Correct 70 ms 15224 KB Output is correct
5 Correct 53 ms 14660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 451 ms 2964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1042 ms 4940 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1086 ms 7468 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Execution timed out 1085 ms 2100 KB Time limit exceeded