Submission #945536

# Submission time Handle Problem Language Result Execution time Memory
945536 2024-03-14T04:04:25 Z berr Spring cleaning (CEOI20_cleaning) C++17
100 / 100
132 ms 29532 KB
#include <bits/stdc++.h>
using namespace std;
  
struct LCA {                                                
        int root, n, cnt;
 
        vector<vector<int>> adj;  
        vector<array<int, 21>> par;
        vector<int> ti, to, d;
 
        LCA(vector<vector<int>> aadj, int r){ 
            adj = aadj; n = adj.size();
            par.resize(n);
            d.assign(n, 0);
            root = r; cnt = 0;
            ti.resize(n); to.resize(n);
 
            d[root] = -1;
            dfs(root, root);
 
            for (int i = 1; i < 21; i++){
                for(int l = 0; l<n; l++){
                    par[l][i] = par[par[l][i-1]][i-1];
                }
            }
        }
        
        void dfs(int v, int p){
            par[v][0] = p;
            ti[v] = cnt++;
            d[v] = d[p]+1;
            for (auto i: adj[v]){
                if (i != p) dfs(i, v); 
            }
            to[v] = cnt++;
        }
 
        bool ia(int u, int v){
            return ti[u] <= ti[v] && to[u] >= to[v];
        }
 
        int operator()(int u, int v){
 
            if(ia(u, v)) return u;
            if(ia(v, u)) return v;
 
            for(int i = 20; i>=0; i--){
                if(!ia(par[u][i], v)) u=par[u][i];
            }
 
            return par[u][0];
        }
      
      int distance(int u, int v){
        return d[u]+d[v]-2*d[(*this)(u, v)];
      }
};  
 
int32_t main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
 
    int n, q; cin >> n >> q;
 
    vector<vector<int>> g(n+5);
    vector<int> c(n);
    vector<array<int, 2>> count(n);
    int leafs=0, root=0;
    for(int i =0; i<n-1; i++){
        int x, y; cin >> x >> y;
        x--; y--;
        g[x].push_back(y);
        g[y].push_back(x);
 
        if(g[x].size()>1) root = x;
        if(g[y].size()>1) root = y;
    }
 
   LCA lca(g, root);
    vector<int> vis(n), tin(n), tout(n);
 
    int cnt = 0;
    auto dfs=[&](int v, int p, auto&& dfs)->void{
        tin[v] = cnt++;
        for(auto i: g[v])
            if(i!=p) dfs(i, v, dfs), c[v]+=c[i];
        
        if(g[v].size()==1) c[v]++, leafs++;
        else vis[v] = 1;
 
        tout[v] = cnt++;
    };
    
    int ans = 0;
 
    auto dfs2=[&](int v, int p, auto&& dfs2)->void{
        //cout<<v+1<<" "<<c[v]<<"\n";
        if(c[v] %2) count[v][1]++;
        else count[v][0]++, ans++;
        for(auto i: g[v]){
            if(i!=p){
                count[i][0]+=count[v][0];
                count[i][1]+=count[v][1];
                dfs2(i, v, dfs2);
            }
        }
    };
 
    dfs(root, root, dfs);
    dfs2(root, root, dfs2) ;
 
    int tmpans = ans, tmpleafs=leafs;
    while(q--){
        int d; cin >> d;
        vector<int> ar(d),a, up;
 
        for(auto &i: ar) cin >> i, i--;
 
        sort(ar.begin(), ar.end(), [&](int x, int y){
            return tin[x] < tin[y];
        }); 
 
        int k=1;
        for(int i=1; i<d; i++){
            if(ar[i]==ar[i-1])k++;
            else{
                 if(g[ar[i-1]].size()==1){
                    leafs+=k-1;
                    if(k%2==0) a.push_back(ar[i-1]);
 
                    k = 1;
                }
                else{
                    leafs+=k;
                    if(k%2==1) a.push_back(ar[i-1]);
                    k=1;
                }
            }
        }
 
        if(g[ar[d-1]].size()==1){
            leafs+=k-1;
            if(k%2==0)  a.push_back(ar[d-1]);
        }
        else{
            leafs+=k;
            if(k%2==1)  a.push_back(ar[d-1]);
        }
 
        up = a; 
        int check =0;
        if(up.size()){
           
            for(int i=0; i<a.size()-1; i++){
                up.push_back(lca(a[i], a[i+1]));
            }   
           
     
            sort(up.begin(), up.end(), [&](int x, int y){
                return tin[x] < tin[y];
            });
            
            vector<int> upp;
 
            for(int i=0; i<up.size(); i++){
                if(i==0||up[i]!=up[i-1]) upp.push_back(up[i]);
            }
            up=upp;
            if(up.size()){
                stack<int> st;
 
                st.push(0);
 
                vector<int> p(up.size()), v(up.size());
                vector<vector<int>> adj(up.size());
                vector<int> aa(up.size());
 
                for(int i=1; i<up.size(); i++) {
                    while(st.size() && tout[up[st.top()]] < tin[up[i]]){
                        st.pop();
                    }
                    if(st.size())
                    adj[st.top()].push_back(i), aa[i]++, p[i]=st.top();
                    else p[i]=i;
                    st.push(i);
                }
 
                auto dfsa=[&](int ve, int p, auto&&dfsa)->void{
 
                    for(auto i: adj[ve]){
                        if(i!=p) dfsa(i, ve, dfsa), v[ve]+=v[i];
                    }
                };
                int pos=0;
 
                for(int l=0; l<a.size(); l++){
                    while(pos<up.size()&&up[pos]!=a[l]) pos++;
                    if(pos<up.size()&&up[pos]==a[l]) v[pos]++;
                }
 
                for(int i=0; i<up.size(); i++){
                    if(aa[i]==0) dfsa(i, i, dfsa);
                }
 
 
 
                 for(int i=0; i<up.size(); i++){
                    if(p[i]==i){
                        auto x = count[up[i]];
                        if(v[i]%2){
                            check=1;
                            ans-=x[0]; ans+=x[1];
 
                        }
                    }
                    else if(v[i]%2){
                        auto x=count[up[i]], y=count[up[p[i]]];
                        x[0]-=y[0];
                        x[1]-=y[1];
                        ans-=x[0]; ans+=x[1];
                    }
                }   
            }
        }
 
        if((leafs%2)!=0) cout<<-1<<"\n";
        else cout<<ans-1+d+n-1<<"\n";
 
        ans=tmpans; leafs=tmpleafs;
    }
}

Compilation message

cleaning.cpp: In function 'int32_t main()':
cleaning.cpp:153:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  153 |             for(int i=0; i<a.size()-1; i++){
      |                          ~^~~~~~~~~~~
cleaning.cpp:164:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |             for(int i=0; i<up.size(); i++){
      |                          ~^~~~~~~~~~
cleaning.cpp:177:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  177 |                 for(int i=1; i<up.size(); i++) {
      |                              ~^~~~~~~~~~
cleaning.cpp:195:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  195 |                 for(int l=0; l<a.size(); l++){
      |                              ~^~~~~~~~~
cleaning.cpp:196:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  196 |                     while(pos<up.size()&&up[pos]!=a[l]) pos++;
      |                           ~~~^~~~~~~~~~
cleaning.cpp:197:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  197 |                     if(pos<up.size()&&up[pos]==a[l]) v[pos]++;
      |                        ~~~^~~~~~~~~~
cleaning.cpp:200:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  200 |                 for(int i=0; i<up.size(); i++){
      |                              ~^~~~~~~~~~
cleaning.cpp:206:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  206 |                  for(int i=0; i<up.size(); i++){
      |                               ~^~~~~~~~~~
cleaning.cpp:150:13: warning: variable 'check' set but not used [-Wunused-but-set-variable]
  150 |         int check =0;
      |             ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 34 ms 5916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1628 KB Output is correct
2 Correct 11 ms 1636 KB Output is correct
3 Correct 45 ms 28532 KB Output is correct
4 Correct 50 ms 20176 KB Output is correct
5 Correct 63 ms 28628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2136 KB Output is correct
2 Correct 12 ms 2140 KB Output is correct
3 Correct 60 ms 29532 KB Output is correct
4 Correct 68 ms 28176 KB Output is correct
5 Correct 54 ms 27216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 6236 KB Output is correct
2 Correct 18 ms 5984 KB Output is correct
3 Correct 11 ms 5720 KB Output is correct
4 Correct 12 ms 6236 KB Output is correct
5 Correct 12 ms 6236 KB Output is correct
6 Correct 27 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 18128 KB Output is correct
2 Correct 62 ms 18008 KB Output is correct
3 Correct 44 ms 9568 KB Output is correct
4 Correct 66 ms 18000 KB Output is correct
5 Correct 63 ms 18012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 27472 KB Output is correct
2 Correct 96 ms 29268 KB Output is correct
3 Correct 132 ms 28996 KB Output is correct
4 Correct 103 ms 28496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 34 ms 5916 KB Output is correct
3 Correct 11 ms 1628 KB Output is correct
4 Correct 11 ms 1636 KB Output is correct
5 Correct 45 ms 28532 KB Output is correct
6 Correct 50 ms 20176 KB Output is correct
7 Correct 63 ms 28628 KB Output is correct
8 Correct 13 ms 2136 KB Output is correct
9 Correct 12 ms 2140 KB Output is correct
10 Correct 60 ms 29532 KB Output is correct
11 Correct 68 ms 28176 KB Output is correct
12 Correct 54 ms 27216 KB Output is correct
13 Correct 28 ms 6236 KB Output is correct
14 Correct 18 ms 5984 KB Output is correct
15 Correct 11 ms 5720 KB Output is correct
16 Correct 12 ms 6236 KB Output is correct
17 Correct 12 ms 6236 KB Output is correct
18 Correct 27 ms 5980 KB Output is correct
19 Correct 64 ms 18128 KB Output is correct
20 Correct 62 ms 18008 KB Output is correct
21 Correct 44 ms 9568 KB Output is correct
22 Correct 66 ms 18000 KB Output is correct
23 Correct 63 ms 18012 KB Output is correct
24 Correct 92 ms 27472 KB Output is correct
25 Correct 96 ms 29268 KB Output is correct
26 Correct 132 ms 28996 KB Output is correct
27 Correct 103 ms 28496 KB Output is correct
28 Correct 58 ms 16732 KB Output is correct
29 Correct 91 ms 29008 KB Output is correct
30 Correct 61 ms 28616 KB Output is correct
31 Correct 67 ms 28068 KB Output is correct
32 Correct 67 ms 18180 KB Output is correct
33 Correct 84 ms 23384 KB Output is correct
34 Correct 116 ms 28500 KB Output is correct
35 Correct 101 ms 29340 KB Output is correct