답안 #887972

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
887972 2023-12-15T16:00:29 Z amirhoseinfar1385 Cats or Dogs (JOI18_catdog) C++17
100 / 100
611 ms 35044 KB
#include "catdog.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
vector<int>adj[maxn];
int timea=0,val[maxn],kaf=(1<<17),n,inf=1e6+5,sz[maxn],part[maxn],parh[maxn],akh[maxn];
pair<int,int>stf[maxn];
set<int>allr[maxn],allb[maxn];
int mainres=0;

struct node{
    int res[2][2],ted;
    node(){
        res[0][1]=res[1][0]=0;
        res[0][0]=res[1][1]=0;
        ted=0;
    }
}fn;
struct segment{
    node seg[(1<<18)];

    node merge(node a,node b){
        if(a.ted==0){
            return b;
        }
        if(b.ted==0){
            return a;
        }
        node ret;
        ret.res[0][0]=ret.res[1][0]=ret.res[1][1]=ret.res[0][1]=inf;
        ret.ted=a.ted+b.ted;
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                for(int ii=0;ii<2;ii++){
                    for(int jj=0;jj<2;jj++){
                        ret.res[i][j]=min(ret.res[i][j],a.res[i][ii]+b.res[jj][j]+(ii!=jj));
                    }
                }
            }
        }
        return ret;
    }
    segment(){
        for(int i=(1<<18)-1;i>=0;i--){
            if(i>=kaf){
                seg[i].res[0][1]=seg[i].res[1][0]=inf;
                seg[i].ted=1;
                seg[i].res[0][0]=seg[i].res[1][1]=0;
            }
            else{
                seg[i]=merge(seg[(i<<1)],seg[(i<<1)^1]);
            }
        }
    }
    void add(int i,int wr,int wb,int to){
        if(i==0){
            return ;
        }
        if(i>=kaf){
           // //cout<<i<<" "<<i-kaf<<" "<<wr<<" "<<wb<<" "<<to<<"\n";
            seg[i].ted=1;
            if(to==0){
                seg[i].res[0][0]=seg[i].res[1][1]=seg[i].res[1][0]=seg[i].res[0][1]=inf;
                seg[i].res[0][0]=wr;
                seg[i].res[1][1]=wb;
                return add((i>>1),wr,wb,to);
            }
            if(to==1){
                seg[i].res[0][0]=seg[i].res[1][1]=seg[i].res[1][0]=seg[i].res[0][1]=inf;
                seg[i].res[1][1]=wb;
                return add((i>>1),wr,wb,to);
            }
            seg[i].res[0][0]=seg[i].res[1][1]=seg[i].res[1][0]=seg[i].res[0][1]=inf;
            seg[i].res[0][0]=wr;
            return add((i>>1),wr,wb,to);
        }
        seg[i]=merge(seg[(i<<1)],seg[(i<<1)^1]);
        return add((i>>1),wr,wb,to);
    }
    node pors(int i,int l,int r,int tl,int tr){
        if(l>r||l>tr||r<tl||tl>tr){
            return fn;
        }
        if(l>=tl&&r<=tr){
            return seg[i];
        }
        int m=(l+r)>>1;
        return merge(pors((i<<1),l,m,tl,tr),pors((i<<1)^1,m+1,r,tl,tr));
    }
}seg;

bool cmp(int a,int b){
    return sz[a]>sz[b];
}

void pre(int u,int par=0){
    sz[u]=1;
    part[u]=par;
    for(auto x:adj[u]){
        if(x==par){
            continue;
        }
        pre(x,u);
        sz[u]+=sz[x];
    }
    sort(adj[u].begin(),adj[u].end());
    if(u!=1){
        adj[u].erase(lower_bound(adj[u].begin(),adj[u].end(),par));
    }
    sort(adj[u].begin(),adj[u].end(),cmp);
}

void makehld(int u=1,int par=1){
   // //cout<<u<<" salam "<<par<<endl;
    parh[u]=par;
    timea++;
    stf[u].first=timea;
    akh[par]=u;
    if((int)adj[u].size()>0){
        makehld(adj[u][0],par);
        for(int i=1;i<(int)adj[u].size();i++){
            makehld(adj[u][i],adj[u][i]);
        }
    }
    stf[u].second=timea;
}

void upd(int u){
    //cout<<u<<" haha "<<" "<<allr[u].size()<<" "<<allb[u].size()<<endl;
    node av=seg.pors(1,0,kaf-1,stf[parh[u]].first,stf[akh[parh[u]]].first);
    int z=min(min(av.res[0][0],av.res[0][1]),min(av.res[1][0],av.res[1][1]));
    mainres-=z;
    if(parh[u]!=1){
        if((av.res[0][0]==z||av.res[0][1]==z)&&(av.res[1][0]==z||av.res[1][1]==z)){
            //hehe
        }
        else if(av.res[0][0]==z||av.res[0][1]==z){
            allb[part[parh[u]]].erase(parh[u]);
        }
        else{
            allr[part[parh[u]]].erase(parh[u]);
        }
    }
    seg.add(stf[u].first+kaf,(int)allr[u].size(),(int)allb[u].size(),val[u]);
    av=seg.pors(1,0,kaf-1,stf[parh[u]].first,stf[akh[parh[u]]].first);
    z=min(min(av.res[0][0],av.res[0][1]),min(av.res[1][0],av.res[1][1]));    
    mainres+=z;
    if(parh[u]!=1){
        if((av.res[0][0]==z||av.res[0][1]==z)&&(av.res[1][0]==z||av.res[1][1]==z)){
            //hehe
        }
        else if(av.res[0][0]==z||av.res[0][1]==z){
            allb[part[parh[u]]].insert(parh[u]);
        }
        else{
            allr[part[parh[u]]].insert(parh[u]);
        }
        return upd(part[parh[u]]);
    }
}

void initialize(int N, std::vector<int> A, std::vector<int> B) {
    n=N;
    for(int i=0;i<n-1;i++){
        adj[A[i]].push_back(B[i]);
        adj[B[i]].push_back(A[i]);
    }
    pre(1);
    makehld();
}
 
int cat(int v) {
    val[v]=1;
    upd(v);
   if(mainres<0||mainres>=inf){
        assert(0);
    }
    return mainres;
}
 
int dog(int v){
    val[v]=2;
    upd(v);
    if(mainres<0||mainres>=inf){
        assert(0);
    }
    return mainres;
}
 
int neighbor(int v) {
    val[v]=0;
    upd(v);
    if(mainres<0||mainres>=inf){
        assert(0);
    }
    return mainres;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 19864 KB Output is correct
2 Correct 7 ms 19852 KB Output is correct
3 Correct 6 ms 20056 KB Output is correct
4 Correct 6 ms 19804 KB Output is correct
5 Correct 7 ms 19804 KB Output is correct
6 Correct 6 ms 20060 KB Output is correct
7 Correct 7 ms 19804 KB Output is correct
8 Correct 6 ms 19800 KB Output is correct
9 Correct 6 ms 20044 KB Output is correct
10 Correct 6 ms 20036 KB Output is correct
11 Correct 6 ms 19804 KB Output is correct
12 Correct 6 ms 20060 KB Output is correct
13 Correct 7 ms 20052 KB Output is correct
14 Correct 7 ms 20048 KB Output is correct
15 Correct 6 ms 19804 KB Output is correct
16 Correct 6 ms 20060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 19864 KB Output is correct
2 Correct 7 ms 19852 KB Output is correct
3 Correct 6 ms 20056 KB Output is correct
4 Correct 6 ms 19804 KB Output is correct
5 Correct 7 ms 19804 KB Output is correct
6 Correct 6 ms 20060 KB Output is correct
7 Correct 7 ms 19804 KB Output is correct
8 Correct 6 ms 19800 KB Output is correct
9 Correct 6 ms 20044 KB Output is correct
10 Correct 6 ms 20036 KB Output is correct
11 Correct 6 ms 19804 KB Output is correct
12 Correct 6 ms 20060 KB Output is correct
13 Correct 7 ms 20052 KB Output is correct
14 Correct 7 ms 20048 KB Output is correct
15 Correct 6 ms 19804 KB Output is correct
16 Correct 6 ms 20060 KB Output is correct
17 Correct 9 ms 20060 KB Output is correct
18 Correct 9 ms 20060 KB Output is correct
19 Correct 8 ms 20060 KB Output is correct
20 Correct 7 ms 19884 KB Output is correct
21 Correct 7 ms 20060 KB Output is correct
22 Correct 7 ms 20060 KB Output is correct
23 Correct 10 ms 20028 KB Output is correct
24 Correct 11 ms 20312 KB Output is correct
25 Correct 8 ms 20064 KB Output is correct
26 Correct 7 ms 20064 KB Output is correct
27 Correct 8 ms 20064 KB Output is correct
28 Correct 6 ms 20064 KB Output is correct
29 Correct 8 ms 20056 KB Output is correct
30 Correct 8 ms 20080 KB Output is correct
31 Correct 6 ms 20064 KB Output is correct
32 Correct 8 ms 20036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 19864 KB Output is correct
2 Correct 7 ms 19852 KB Output is correct
3 Correct 6 ms 20056 KB Output is correct
4 Correct 6 ms 19804 KB Output is correct
5 Correct 7 ms 19804 KB Output is correct
6 Correct 6 ms 20060 KB Output is correct
7 Correct 7 ms 19804 KB Output is correct
8 Correct 6 ms 19800 KB Output is correct
9 Correct 6 ms 20044 KB Output is correct
10 Correct 6 ms 20036 KB Output is correct
11 Correct 6 ms 19804 KB Output is correct
12 Correct 6 ms 20060 KB Output is correct
13 Correct 7 ms 20052 KB Output is correct
14 Correct 7 ms 20048 KB Output is correct
15 Correct 6 ms 19804 KB Output is correct
16 Correct 6 ms 20060 KB Output is correct
17 Correct 9 ms 20060 KB Output is correct
18 Correct 9 ms 20060 KB Output is correct
19 Correct 8 ms 20060 KB Output is correct
20 Correct 7 ms 19884 KB Output is correct
21 Correct 7 ms 20060 KB Output is correct
22 Correct 7 ms 20060 KB Output is correct
23 Correct 10 ms 20028 KB Output is correct
24 Correct 11 ms 20312 KB Output is correct
25 Correct 8 ms 20064 KB Output is correct
26 Correct 7 ms 20064 KB Output is correct
27 Correct 8 ms 20064 KB Output is correct
28 Correct 6 ms 20064 KB Output is correct
29 Correct 8 ms 20056 KB Output is correct
30 Correct 8 ms 20080 KB Output is correct
31 Correct 6 ms 20064 KB Output is correct
32 Correct 8 ms 20036 KB Output is correct
33 Correct 408 ms 24836 KB Output is correct
34 Correct 121 ms 24144 KB Output is correct
35 Correct 375 ms 23956 KB Output is correct
36 Correct 582 ms 27940 KB Output is correct
37 Correct 17 ms 21792 KB Output is correct
38 Correct 591 ms 28560 KB Output is correct
39 Correct 586 ms 28808 KB Output is correct
40 Correct 575 ms 28992 KB Output is correct
41 Correct 575 ms 28692 KB Output is correct
42 Correct 541 ms 28628 KB Output is correct
43 Correct 588 ms 28732 KB Output is correct
44 Correct 544 ms 28732 KB Output is correct
45 Correct 559 ms 28948 KB Output is correct
46 Correct 572 ms 28612 KB Output is correct
47 Correct 611 ms 28720 KB Output is correct
48 Correct 179 ms 27032 KB Output is correct
49 Correct 178 ms 28284 KB Output is correct
50 Correct 78 ms 22096 KB Output is correct
51 Correct 80 ms 23232 KB Output is correct
52 Correct 37 ms 21588 KB Output is correct
53 Correct 234 ms 26312 KB Output is correct
54 Correct 198 ms 23048 KB Output is correct
55 Correct 518 ms 26188 KB Output is correct
56 Correct 328 ms 23800 KB Output is correct
57 Correct 401 ms 26452 KB Output is correct
58 Correct 31 ms 22740 KB Output is correct
59 Correct 77 ms 23376 KB Output is correct
60 Correct 162 ms 27728 KB Output is correct
61 Correct 174 ms 28536 KB Output is correct
62 Correct 100 ms 26024 KB Output is correct
63 Correct 61 ms 25936 KB Output is correct
64 Correct 67 ms 27384 KB Output is correct
65 Correct 64 ms 31304 KB Output is correct
66 Correct 122 ms 23128 KB Output is correct
67 Correct 92 ms 28244 KB Output is correct
68 Correct 198 ms 31828 KB Output is correct
69 Correct 77 ms 21340 KB Output is correct
70 Correct 22 ms 20060 KB Output is correct
71 Correct 102 ms 25848 KB Output is correct
72 Correct 119 ms 30548 KB Output is correct
73 Correct 358 ms 35044 KB Output is correct
74 Correct 370 ms 31444 KB Output is correct
75 Correct 233 ms 34900 KB Output is correct
76 Correct 266 ms 33620 KB Output is correct
77 Correct 355 ms 31824 KB Output is correct