Submission #1038552

# Submission time Handle Problem Language Result Execution time Memory
1038552 2024-07-29T23:58:40 Z vjudge1 Spring cleaning (CEOI20_cleaning) C++17
0 / 100
1000 ms 24296 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC target("popcnt")
using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using lld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using ii = pair<int,int>;
using pll = pair<ll, ll>;
using vii = vector<ii>;
using vpll = vector<pll>;
 
#define endl '\n'
#define all(x) x.begin(),x.end()
#define lsb(x) x&(-x)
#define gcd(a,b) __gcd(a,b)
#define sz(x) (int)x.size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fls cout.flush()
 
#define fore(i,l,r) for(auto i=l;i<r;i++)
#define fo(i,n) fore(i,0,n)
#define forex(i,r,l) for(auto i=r; i>=l;i--)
#define ffo(i,n) forex(i,n-1,0)
 
bool cmin(int &a, int b){if(b<a){a=b;return 1;}return 0;}
bool cmax(int &a, int b){if(b>a){a=b;return 1;}return 0;}
void valid(ll in){cout<<((in)?"YES\n":"NO\n");}
ll lcm(ll a, ll b){return (a/gcd(a,b))*b;}
ll gauss(ll n){return (n*(n+1))/2;}
const int N = 2e5 + 7, LOG=20;
vll graph[N];
ll ni[N],szl[N],cn[N],pa[N],ans=0,r=0,n,q,k;
void dfs(ll u,ll p=0){
    szl[u]=0;
    for(ll v:graph[u]){
        if(v==p)continue;
        ni[v]=ni[u]+1;
        pa[v]=u;
        dfs(v, u);
        szl[u]+=szl[v];
    }
    if(sz(graph[u])==1){
        szl[u]=1;
        ans+=ni[u];
        return;
    }
    assert(szl[u]>=1);
    ans-=ni[u]*((szl[u]+1)/2-1)*2;
    szl[u]=(szl[u]&1?1:2);
}
ll dp[N][2],tin[N],tout[N],timer=1;
/*
u, el siguiente alternado que busco es 1 o 2
*/
void dfs2(ll u,ll p=0){
    dp[u][0]=(szl[u]==1?2*ni[u]+dp[p][1]:dp[p][0]);
    dp[u][1]=(szl[u]==2?-2*ni[u]+dp[p][0]:dp[p][1]);
    tin[u]=timer++;
    for(ll v:graph[u]){
        if(v==p)continue;
        dfs2(v, u);
    }
    tout[u]=timer-1;
}
struct Fenwick{
    vll ft;
    ll n;
    Fenwick(ll n):n(n){ft.resize(n+5,0);}
    void update(ll i,ll v){for(;i<n+2;i+=lsb(i))ft[i]+=v;}
    ll query(ll i){ll r=0;for(;i>0;i-=lsb(i))r+=ft[i];return r;}
};
void test_case(){
    cin>>n>>q;
    fo(i,n-1){
        ll a,b;
        cin>>a>>b;
        cn[a]++;
        cn[b]++;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    fore(i,1,n+1)if(sz(graph[i])>1)r=i;
    dfs(r);
    dfs2(r);
    Fenwick s(n+2);
    ll h=szl[r]-1;
    vll xd(n+2, 0);
    while(q--){
        cin>>k;
        vll u(k);
        fo(i,k)cin>>u[i];
        ll ans2=ans;
        fo(i,k){
            cn[u[i]]++;
            if(cn[u[i]]==2){
                ans2++;
                continue;
            }
            ans2+=ni[u[i]]+1;
            // ll t=s.query(tout[u[i]])-s.query(tin[u[i]]-1);
            ans2+=dp[u[i]][xd[u[i]]&1^1];
            h^=1;
            ll v=u[i];
            while(v!=0){
                xd[v]^=1;
                v=pa[v];
            }
            // s.update(tin[u[i]], +1);
        }
        if(h)cout<<ans2<<endl;
        else cout<<-1<<endl;
        fo(i,k){
            cn[u[i]]--;
            if(cn[u[i]]==1)continue;
            // s.update(tin[u[i]], -1);
            ll v=u[i];
            while(v!=0){
                xd[v]^=1;
                v=pa[v];
            }
            h^=1;
        }
    }
}
int main(){cin.tie(0)->sync_with_stdio(0);
    int t=1;
    // cin >> t;
    while(t--)test_case();
}

Compilation message

cleaning.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    4 | #pragma GCC optimization ("O3")
      | 
cleaning.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    5 | #pragma GCC optimization ("unroll-loops")
      | 
cleaning.cpp: In function 'void test_case()':
cleaning.cpp:111:36: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  111 |             ans2+=dp[u[i]][xd[u[i]]&1^1];
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13400 KB Output is correct
2 Incorrect 19 ms 14936 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 14172 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 632 ms 14680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1052 ms 15956 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 32 ms 19116 KB Output is correct
2 Incorrect 34 ms 19036 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 50 ms 21212 KB Output is correct
2 Execution timed out 1077 ms 24296 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13400 KB Output is correct
2 Incorrect 19 ms 14936 KB Output isn't correct
3 Halted 0 ms 0 KB -