Submission #649959

#TimeUsernameProblemLanguageResultExecution timeMemory
649959enerelt14Cats or Dogs (JOI18_catdog)C++14
0 / 100
9 ms11604 KiB
#include "catdog.h" #include<bits/stdc++.h> #define pb push_back using namespace std; const int MX=1e5+5; int n, sz[MX], par[MX], num[MX]; int hldpar[MX], pos[MX], st[MX]; vector<int>adj[MX]; void dfs(int u, int pre){ sz[u]=1; par[u]=pre; for (int i=0;i<adj[u].size();i++){ int v=adj[u][i]; if (v==pre)continue; dfs(v, u); sz[u]+=sz[v]; } } void hld(int u, int pre){ num[u]++; pos[u]=num[pre]; hldpar[u]=pre; for (int i=0;i<adj[u].size();i++){ int v=adj[u][i]; if (v==par[u])continue; if (sz[v]*2>=sz[u])hld(v, pre); else hld(v, v); } } struct node{ int a00, a01, a10, a11; friend node operator + (const node& a, const node& b){ node c; c.a00=min(a.a00+b.a00, a.a01+b.a10); c.a01=min(a.a00+b.a01, a.a01+b.a11); c.a10=min(a.a10+b.a00, a.a11+b.a10); c.a11=min(a.a10+b.a01, a.a11+b.a11); return c; } }; struct segtree{ int s; vector<node>cost; segtree(){} segtree(int n){ while(s<n)s*=2; cost.resize(2*s); for (int i=0;i<2*s;i++)cost[i].a01=cost[i].a10=1; } void update(int i, int& ch0, int& ch1){ i+=s; cost[i].a00+=ch0; cost[i].a01+=ch0; cost[i].a10+=ch1; cost[i].a11+=ch1; i/=2; while(i!=0){ cost[i]=cost[i*2]+cost[i*2+1]; i/=2; } } void calc(int& x, int& y){ x=min(cost[1].a00, cost[1].a01); y=min(cost[1].a10, cost[1].a11); } } seg[MX]; void initialize(int N, vector<int> A, vector<int> B){ n=N; for (int i=0;i<n;i++){ adj[A[i]].pb(B[i]); adj[B[i]].pb(A[i]); } dfs(1, 0); hld(1, 1); for (int i=1;i<=n;i++){ if (i==hldpar[i])seg[i]=segtree(num[i]); } } void update(int u, int x, int y){ while(u!=0){ int hd=hldpar[u]; int bef0, bef1, aft0, aft1; seg[hd].calc(bef0, bef1); seg[hd].update(pos[u], x, y); seg[hd].calc(aft0, aft1); x=min(bef0, bef1+1)-min(aft0, aft1+1); y=min(bef0+1, bef1)-min(aft0+1, aft1); u=par[hd]; } } int query(){ int x, y; seg[1].calc(x, y); return min(x, y); } int cat(int v){ st[v]=1; update(v, 0, n); return query(); } int dog(int v){ st[v]=2; update(v, n, 0); return query(); } int neighbor(int v){ if (st[v]==1)update(v, 0, -n); else update(v, -n, 0); st[v]=0; return query(); }

Compilation message (stderr)

catdog.cpp: In function 'void dfs(int, int)':
catdog.cpp:12:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |     for (int i=0;i<adj[u].size();i++){
      |                  ~^~~~~~~~~~~~~~
catdog.cpp: In function 'void hld(int, int)':
catdog.cpp:23:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |     for (int i=0;i<adj[u].size();i++){
      |                  ~^~~~~~~~~~~~~~
catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:46:15: warning: '<anonymous>.segtree::s' may be used uninitialized in this function [-Wmaybe-uninitialized]
   46 |         while(s<n)s*=2;
      |               ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...