Submission #888553

# Submission time Handle Problem Language Result Execution time Memory
888553 2023-12-17T17:42:26 Z SzypkiBill Spring cleaning (CEOI20_cleaning) C++17
28 / 100
1000 ms 15980 KB
#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vii vector<pii>
#define siz(x) (int)x.size()
#define pb push_back
#define rep(i,x) for(int i=0; i<x; i++)
#define rep1(i,x) for(int i=1; i<=x; i++)
using namespace std;
const int inf = 1e9, maxn = 1e5+5, mod = 1e9+7;
int n,q,ileparzystych,ilelisci,korzen = 1;
int liscie[maxn];
bool parzyste[maxn];
int parent[maxn];
int aktsiz[maxn];
vi graf[maxn];

void DFS(int v, int p){
    parent[v] = p;
    if(siz(graf[v])==1){liscie[v] = 1;ilelisci++;}
    for(auto u:graf[v])if(u!=p){
        DFS(u,v);
        liscie[v]+=liscie[u];
    }
    parzyste[v] = !(liscie[v]%2);
    if(parzyste[v] && v!=korzen)ileparzystych++;
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n>>q;
    rep(i,n-1){
        int a,b;
        cin>>a>>b;
        graf[a].pb(b);
        graf[b].pb(a);
        aktsiz[a]++;
        aktsiz[b]++;
    }
    DFS(korzen,0);
  //  cout<<"pr ileparzystych: "<<ileparzystych<<"   ilelisci: "<<ilelisci<<"   "; for(int i=1; i<=n; i++)cout<<aktsiz[i]<<' ';cout<<'\n';
    rep(i,q){
        int d;
        cin>>d;
        unordered_map<int,int> M;
        rep(j,d){
            int x;
            cin>>x;
            M[x]++;

        }
        for(auto [a,b] : M){
            if((b%2 == 0 && siz(graf[a])>1)||(b%2 && siz(graf[a])==1))continue;



            int x = a,x2 = a;
            if(aktsiz[x] > 1)ilelisci+=b;
            else ilelisci+=(b-1);

            int f = 1;
            if(b>1)f--;
            if(aktsiz[x] > f)
            while(x!=korzen){
                parzyste[x] = !parzyste[x];
                if(parzyste[x])ileparzystych++;
                else ileparzystych--;
                x = parent[x];
            }
            aktsiz[x2]+=b;
        }


      //  cout<<"in ileparzystych: "<<ileparzystych<<"   ilelisci: "<<ilelisci<<"   "; for(int i=1; i<=n; i++)cout<<aktsiz[i]<<' ';cout<<'\n';

        if((ilelisci)&1)cout<<-1<<'\n';
        else cout<<n-1+d+ileparzystych<<'\n';

        for(auto [a,b] : M){
            if((b%2 == 0 && siz(graf[a])>1)||(b%2 && siz(graf[a])==1))continue;
            int x = a;
            aktsiz[x]-=b;
            if(aktsiz[x] > 1)ilelisci-=b;
            else ilelisci-=(b-1);

            int f = 1;
            if(b>1)f--;
            if(aktsiz[x] > f)
            while(x!=korzen){
                parzyste[x] = !parzyste[x];
                if(parzyste[x])ileparzystych++;
                else ileparzystych--;
                x = parent[x];
            }
        }

      //  cout<<"po ileparzystych: "<<ileparzystych<<"   ilelisci: "<<ilelisci<<"   "; for(int i=1; i<=n; i++)cout<<aktsiz[i]<<' ';cout<<'\n';
    }

}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 30 ms 5708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 4956 KB Output is correct
3 Correct 18 ms 9168 KB Output is correct
4 Correct 24 ms 9924 KB Output is correct
5 Correct 33 ms 11460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 5408 KB Output is correct
2 Correct 30 ms 5212 KB Output is correct
3 Correct 387 ms 14604 KB Output is correct
4 Execution timed out 1022 ms 15980 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1045 ms 6196 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 36 ms 7912 KB Output is correct
2 Correct 40 ms 7828 KB Output is correct
3 Correct 29 ms 6232 KB Output is correct
4 Correct 38 ms 7772 KB Output is correct
5 Correct 37 ms 7904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 9296 KB Output is correct
2 Execution timed out 1074 ms 11732 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 30 ms 5708 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 6 ms 4956 KB Output is correct
5 Correct 18 ms 9168 KB Output is correct
6 Correct 24 ms 9924 KB Output is correct
7 Correct 33 ms 11460 KB Output is correct
8 Correct 29 ms 5408 KB Output is correct
9 Correct 30 ms 5212 KB Output is correct
10 Correct 387 ms 14604 KB Output is correct
11 Execution timed out 1022 ms 15980 KB Time limit exceeded
12 Halted 0 ms 0 KB -