답안 #342485

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
342485 2021-01-02T08:35:17 Z urd05 Spring cleaning (CEOI20_cleaning) C++14
9 / 100
140 ms 12396 KB
#include <bits/stdc++.h>
using namespace std;

int deg[100000];
vector<int> adj[100000];
vector<int> son[100000];
int leaf[100000];
int p[100000];
int dp[100000];
const int nn=-1e9;

int getleaf(int v) {
    if (leaf[v]!=-1) {
        return leaf[v];
    }
    if (son[v].empty()) {
        return leaf[v]=1;
    }
    leaf[v]=0;
    for(int i=0;i<son[v].size();i++) {
        leaf[v]+=getleaf(son[v][i]);
    }
    return leaf[v];
}

int ans(int v) {
    if (dp[v]!=nn) {
        return dp[v];
    }
    if (p[v]==-1) {
        return 0;
    }
    int got=ans(p[v]);
    if (getleaf(p[v])%2!=getleaf(v)%2) {
        got=-got;
    }
    if (getleaf(v)%2==0) {
        got--;
    }
    else {
        got++;
    }
    return dp[v]=got;
}

void dfs(int v,int prev) {
    p[v]=prev;
    for(int i=0;i<adj[v].size();i++) {
        int nt=adj[v][i];
        if (prev==nt) {
            continue;
        }
        son[v].push_back(nt);
        dfs(nt,v);
    }
}

int cnt[100000];

int main(void) {
    int n,q;
    scanf("%d %d",&n,&q);
    memset(leaf,-1,sizeof(leaf));
    for(int i=1;i<n;i++) {
        int u,v;
        scanf("%d %d",&u,&v);
        u--;
        v--;
        deg[u]++;
        deg[v]++;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    int r=0;
    for(int i=0;i<n;i++) {
        dp[i]=nn;
        p[i]=-1;
        if (deg[i]!=1) {
            r=i;
        }
    }
    dfs(r,-1);
    int ori=0;
    for(int i=0;i<n;i++) {
        if (i!=r) {
            if (getleaf(i)%2==0) {
                ori++;
            }
        }
    }
    for(int i=0;i<q;i++) {
        int k;
        vector<int> save;
        scanf("%d",&k);
        int ret=ori;
        int l=getleaf(r);
        for(int j=0;j<k;j++) {
            int v;
            scanf("%d",&v);
            v--;
            if (son[v].empty()&&cnt[v]==0) {
                
            }
            else {
                if (son[v].empty()) {
                    if (cnt[v]%2==0) {
                        ret-=ans(v);
                    }
                    else {
                        ret+=ans(v);
                    }
                }
                else {
                    if (cnt[v]%2==0) {
                        ret+=ans(v);
                    }
                    else {
                        ret-=ans(v);
                    }
                }
                l++;
            }
            cnt[v]++;
            save.push_back(v);
        }
        if (l%2!=0) {
            printf("-1\n");
        }
        else
            printf("%d\n",n-1+k+ret);
        for(int j=0;j<save.size();j++) {
            cnt[save[j]]--;
        }
    }
}

Compilation message

cleaning.cpp: In function 'int getleaf(int)':
cleaning.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for(int i=0;i<son[v].size();i++) {
      |                 ~^~~~~~~~~~~~~~
cleaning.cpp: In function 'void dfs(int, int)':
cleaning.cpp:48:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |     for(int i=0;i<adj[v].size();i++) {
      |                 ~^~~~~~~~~~~~~~
cleaning.cpp: In function 'int main()':
cleaning.cpp:131:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |         for(int j=0;j<save.size();j++) {
      |                     ~^~~~~~~~~~~~
cleaning.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   62 |     scanf("%d %d",&n,&q);
      |     ~~~~~^~~~~~~~~~~~~~~
cleaning.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   66 |         scanf("%d %d",&u,&v);
      |         ~~~~~^~~~~~~~~~~~~~~
cleaning.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   94 |         scanf("%d",&k);
      |         ~~~~~^~~~~~~~~
cleaning.cpp:99:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   99 |             scanf("%d",&v);
      |             ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5356 KB Output is correct
2 Incorrect 31 ms 6892 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 6268 KB Output is correct
2 Correct 15 ms 6268 KB Output is correct
3 Correct 41 ms 10984 KB Output is correct
4 Correct 46 ms 10084 KB Output is correct
5 Correct 58 ms 11752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 16 ms 6888 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 38 ms 7148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 9836 KB Output is correct
2 Incorrect 84 ms 9724 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 140 ms 12396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5356 KB Output is correct
2 Incorrect 31 ms 6892 KB Output isn't correct