Submission #927683

# Submission time Handle Problem Language Result Execution time Memory
927683 2024-02-15T08:37:19 Z 12345678 Spring cleaning (CEOI20_cleaning) C++17
28 / 100
171 ms 21400 KB
#include <bits/stdc++.h>

using namespace std;

const int nx=1e5+5;
int n, q, dp[nx], sz[nx], id[nx], pa[nx], hd[nx], t, u, v, rt, vs[nx], k[nx];
vector<int> d[nx];

struct segtree
{
    int a[4*nx], b[4*nx], lz[4*nx];
    void pushlz(int l, int r, int i)
    {
        if (!lz[i]) return;
        lz[i]=0;
        swap(a[i], b[i]);
        if (l!=r) lz[2*i]=!lz[2*i], lz[2*i+1]=!lz[2*i+1];        
    }
    void build(int l, int r, int i)
    {
        if (l==r)
        {
            if (k[l]==1) a[i]++;
            else b[i]++;
            return;
        }
        int md=(l+r)/2;
        build(l, md, 2*i);
        build(md+1, r, 2*i+1);
        a[i]=a[2*i]+a[2*i+1];
        b[i]=b[2*i]+b[2*i+1];
    }
    void update(int l, int r, int i, int ql, int qr)
    {
        pushlz(l, r, i);
        if (r<ql||qr<l) return;
        if (ql<=l&&r<=qr) return lz[i]=1, pushlz(l, r, i), void();
        int md=(l+r)/2;
        update(l, md, 2*i, ql, qr);
        update(md+1, r, 2*i+1, ql, qr);
        a[i]=a[2*i]+a[2*i+1];
        b[i]=b[2*i]+b[2*i+1];
    }
    bool query(int l, int r, int i)
    {
        pushlz(l, r, i);
        if (r!=1) return query(l, (l+r)/2, 2*i);
        return a[i]==1; 
    }
    void show(int l, int r, int i)
    {
        pushlz(l, r, i);
        if (l==r) return void(cout<<"show "<<l<<' '<<a[i]<<' '<<b[i]<<'\n');
        int md=(l+r)/2;
        show(l, md, 2*i);
        show(md+1, r, 2*i+1);
    }
} s;

void dfs(int u, int p)
{
    sz[u]=1;
    pa[u]=p;
    if (d[u].size()==1) dp[u]++;
    for (auto v:d[u]) if (v!=p) dfs(v, u), sz[u]+=sz[v], dp[u]+=dp[v];
    dp[v]%=2;
}

void dfshld(int u, int p, int h)
{
    id[u]=++t;
    hd[u]=h;
    pair<int, int> hv={-1, -1};
    for (auto v:d[u]) if (v!=p) hv=max(hv, {sz[v], v});
    if (hv.second!=-1) dfshld(hv.second, u, h);
    for (auto v:d[u]) if (v!=p&&v!=hv.second) dfshld(v, u, v);
}

void flip(int u)
{
    //cout<<"update "<<u<<' '<<hd[u]<<' '<<id[u]<<' '<<id[hd[u]]<<'\n';
    s.update(1, n, 1, id[hd[u]], id[u]);
    if (hd[u]==rt) return;
    flip(pa[hd[u]]);
}

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>q;
    for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
    for (int i=1; i<=n; i++) if (d[i].size()>1) rt=i;
    dfs(rt, rt);
    dfshld(rt, rt, rt);
    for (int i=1; i<=n; i++) k[id[i]]=dp[i];
    s.build(1, n, 1);
    //s.show(1, n, 1);
    //for (int i=1; i<=n; i++) cout<<"id "<<i<<' '<<id[i]<<'\n';
    while (q--)
    {
        cin>>t;
        vector<int> vl(t);
        for (auto &x:vl) cin>>x;
        for (int i=0; i<t; i++) 
        {
            if (d[vl[i]].size()==1&&!vs[vl[i]]) vs[vl[i]]=1;
            else flip(vl[i]);
        }
        //s.show(1, n, 1);
        if (s.query(1, n, 1)) cout<<-1<<'\n';
        else cout<<s.a[1]+2*s.b[1]+t-2<<'\n';
        for (int i=0; i<t; i++)
        {
            if (d[vl[i]].size()==1&&vs[vl[i]]) vs[vl[i]]=0;
            else flip(vl[i]);
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Incorrect 76 ms 8460 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 46 ms 7588 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 36 ms 8024 KB Output is correct
2 Correct 36 ms 8028 KB Output is correct
3 Correct 35 ms 21400 KB Output is correct
4 Correct 88 ms 21072 KB Output is correct
5 Correct 30 ms 19460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 71 ms 9040 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 95 ms 11668 KB Output is correct
2 Correct 171 ms 11556 KB Output is correct
3 Correct 133 ms 9612 KB Output is correct
4 Correct 147 ms 11460 KB Output is correct
5 Correct 149 ms 11344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 144 ms 15200 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Incorrect 76 ms 8460 KB Output isn't correct
3 Halted 0 ms 0 KB -