Submission #966704

# Submission time Handle Problem Language Result Execution time Memory
966704 2024-04-20T08:40:07 Z anton Spring cleaning (CEOI20_cleaning) C++17
28 / 100
1000 ms 12116 KB
#include<bits/stdc++.h>

using namespace std;
#define int long long
#define pii pair<int, int>
typedef complex<int> point;
const int MAX_N = 100000;

vector<vector<int>> adj;
vector<int> parity;
vector<int> anc;
vector<int> cost = {2, 1};
int total_cost= 0;

int count_leaves(int u, int a){
    int s= 0;
    for(auto v: adj[u]){
        if(v!=a){
            anc[v] = u;
            s+=count_leaves(v, u);
        }
    }
    if(s==0){
        s=1;
    }
    total_cost+= cost[s%2];
    
    //cout<<u+1<<" "<<s<<endl;
    parity[u] = s%2;
    return s;
}

void flip(int u){
    //cout<<"flip "<<u+1<<endl;
    total_cost -= cost[parity[u]];
    parity[u] ^= 1;
    total_cost+=cost[parity[u]];
    if(anc[u]!=-1){
        flip(anc[u]);
    }
}
signed main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int n, k;
    cin>>n>>k;
    adj.resize(n);
    parity.resize(n);
    anc.resize(n, -1);
    int node = 0;
    int deg=  0;

    for(int i = 0; i<n-1; i++){
        int a, b;
        cin>>a>>b;
        a--;b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
        if(adj[a].size()>deg){
            node =a;
            deg = adj[a].size();
        }
        if(adj[b].size()>deg){
            node =b;
            deg = adj[b].size();
        }
    }

    
    int r=count_leaves(node, -1);
    //cout<<"base "<<total_cost<<endl;
    for(int i = 0; i<k; i++){
        int d;
        cin>>d;
        vector<int> parents;
        for(int j = 0; j<d; j++){
            int v;
            cin>>v;
            v--;
            parents.push_back(v);
            adj[parents.back()].push_back(adj.size());
            total_cost++;
            if(adj[parents.back()].size()>2){
                flip(parents.back());
                r^=1;
            }
        }
        if(parity[node]==1){
            cout<<-1<<endl;
        }
        else{
            cout<<total_cost-2<<endl;
        }
        
        adj.resize(n);
        for(auto e: parents){
            if(adj[e].size()>2){
                flip(e);
                r^=1;
            }
            total_cost--;
            adj[e].pop_back();
        }
    }
}

Compilation message

cleaning.cpp: In function 'int main()':
cleaning.cpp:59:25: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   59 |         if(adj[a].size()>deg){
      |            ~~~~~~~~~~~~~^~~~
cleaning.cpp:63:25: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   63 |         if(adj[b].size()>deg){
      |            ~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 36 ms 2988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3024 KB Output is correct
2 Correct 10 ms 2772 KB Output is correct
3 Correct 20 ms 9016 KB Output is correct
4 Correct 38 ms 8800 KB Output is correct
5 Correct 32 ms 11224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 770 ms 3208 KB Output is correct
2 Correct 899 ms 3376 KB Output is correct
3 Correct 581 ms 11184 KB Output is correct
4 Execution timed out 1055 ms 11460 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1025 ms 3288 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 123 ms 7540 KB Output is correct
2 Correct 56 ms 7248 KB Output is correct
3 Correct 43 ms 4188 KB Output is correct
4 Correct 59 ms 7464 KB Output is correct
5 Correct 57 ms 7248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 227 ms 11600 KB Output is correct
2 Execution timed out 1054 ms 12116 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 36 ms 2988 KB Output is correct
3 Correct 9 ms 3024 KB Output is correct
4 Correct 10 ms 2772 KB Output is correct
5 Correct 20 ms 9016 KB Output is correct
6 Correct 38 ms 8800 KB Output is correct
7 Correct 32 ms 11224 KB Output is correct
8 Correct 770 ms 3208 KB Output is correct
9 Correct 899 ms 3376 KB Output is correct
10 Correct 581 ms 11184 KB Output is correct
11 Execution timed out 1055 ms 11460 KB Time limit exceeded
12 Halted 0 ms 0 KB -