답안 #916673

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
916673 2024-01-26T09:10:10 Z manizare Cats or Dogs (JOI18_catdog) C++14
100 / 100
213 ms 73016 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")


#define pb push_back
#define F first
#define S second 
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define ll long long
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= (b);i++)
#define per(i , a , b) for(int i=a;i >= (b);i--)
#define deb(x) cerr<<#x << " : " << x << "\n"; 

using namespace std ;   
const int maxn = 4e5 + 10 ,  mod = 1e9 + 7 , inf =1e9  ;
int n , c[maxn] , par[maxn] , d[maxn] ,cnt = 2 , s[maxn][2] , mark[maxn];
vector <int> G[maxn] ;

struct node{
    int a[2][2] ;
};

node operator+(node a, node b){
    node res ;
    rep(i,0,1)rep(j,0,1)res.a[i][j] = inf ;
    rep(i,0,1)
    rep(j,0,1)
    rep(k,0,1)
    rep(z,0,1)
    res.a[i][j] = min(res.a[i][j] , a.a[i][k] + b.a[z][j] + (k!=z)) ;
    return res; 
}

struct segtree{
    int id ; 
    vector <int> az ; 
    vector <node> seg;
    void UPD(int x ,int p ,int l  , int r ){
        int mid = (l+r)/2, pl=p<<1 , pr =p<<1|1 ;
        if(x > r || x < l)return ;
        if(l==r){
            seg[p].a[0][1] = seg[p].a[1][0] = seg[p].a[0][0] = seg[p].a[1][1] = inf ;
            if(mark[az[x]] == 0 || mark[az[x]] == 1)seg[p].a[0][0] = s[az[x]][0] ;
            if(mark[az[x]] == 0 || mark[az[x]] == 2)seg[p].a[1][1] = s[az[x]][1] ;
            return ; 
        }
        UPD(x,pl,l,mid);
        UPD(x,pr,mid+1,r); 
        seg[p] = seg[pl] + seg[pr] ;
    }
    void upd(int x){
        UPD(x,1 ,0 ,sz(az)-1) ;
    }
    void bui(){
        rep(i , 0 , 4*sz(az)+2){
            node res ;
            seg.pb(res) ;
        }
        rep(i , 0 , sz(az)-1){
            upd(i);
        }
    }

};
segtree h[maxn] ;int   sb[maxn] ; 

void d1(int v, int p=0){
    sb[v] = 1; 
    for(int u : G[v]){
        if(u == p)continue ;
        d1(u , v); 
        sb[v] += sb[u] ;
    }
}
void d2(int v , int p =0){
    vector <pii> vec ;
    for(int u : G[v]){
        if(u == p)continue ;
        vec.pb({sb[u] , u}) ;
    }
    sort(all(vec)) ;
    per(i , sz(vec)-1 , 0){
        int u = vec[i].S ;
        if(i == sz(vec)-1){
            par[u] = par[v] ;
            c[u]=c[v] ;
            h[c[u]].az.pb(u) ;
            d[u] = d[v] + 1 ;
            d2(u , v);
        }else{
            par[u] = v ; 
            c[u] = cnt ;
            h[cnt].az.pb(u) ;
            cnt++;
            d[u] =0 ;
            d2(u , v) ;
        }
    }
    if(sz(vec)==0){
        h[c[v]].bui() ;
    }
}

void ch(int v){
    if(c[v]==1){
        h[c[v]].upd(d[v]);
        return ;
    }
    rep(i , 0 , 1)
s[par[v]][i]-=min(min(h[c[v]].seg[1].a[i][0],h[c[v]].seg[1].a[i][1]),min(h[c[v]].seg[1].a[!i][0],h[c[v]].seg[1].a[!i][1])+1) ;
    h[c[v]].upd(d[v]); 
    rep(i , 0 , 1)
s[par[v]][i]+=min(min(h[c[v]].seg[1].a[i][0],h[c[v]].seg[1].a[i][1]),min(h[c[v]].seg[1].a[!i][0],h[c[v]].seg[1].a[!i][1])+1) ;
    ch(par[v]) ;
}

void initialize(int N , vector <int> A , vector <int> B){
    n = N ;
    rep(i , 0 , n-2){
        G[A[i]].pb(B[i]);
        G[B[i]].pb(A[i]);
    }
    d1(1) ;
    par[1] = 1;
    h[1].az.pb(1) ;
    c[1] = 1;
    d2(1); 
}
int ans(){
    int f = inf ;
    rep(i , 0 , 1){
        rep(j , 0 ,1){
            f =min(f , h[1].seg[1].a[i][j]) ;
        }
    }
    return f ;
}
int neighbor(int v){
    mark[v] = 0 ;
    ch(v) ;
    return ans() ;
}
int cat(int v){
    mark[v] =1 ;
    ch(v) ;
    return ans() ;
}
int dog(int v){
    mark[v]= 2 ;
    ch(v);
    return ans() ;
}
/*
signed main(){
    ios::sync_with_stdio(0) ;cin.tie(0);
    int n , q;
    cin >> n >> q;
    vector <int> a , b ;
    rep(i , 1, n-1){
        int v,  u;
        cin >> v >> u ;
        a.pb(v);
        b.pb(u) ;
    }
    initialize(n , a, b);
    while(q--){
        int t  , v ;
        cin >> t >> v;
        if(t==0){
            cout << neighbor(v)  << "\n" ;
        }else if(t==1){
            cout << cat(v) << "\n" ;
        }else{
            cout << dog(v) << "\n" ;
        }
    }
}
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 37212 KB Output is correct
2 Correct 9 ms 37212 KB Output is correct
3 Correct 10 ms 37212 KB Output is correct
4 Correct 9 ms 37212 KB Output is correct
5 Correct 11 ms 37212 KB Output is correct
6 Correct 10 ms 37380 KB Output is correct
7 Correct 10 ms 37208 KB Output is correct
8 Correct 10 ms 37212 KB Output is correct
9 Correct 9 ms 37212 KB Output is correct
10 Correct 10 ms 37212 KB Output is correct
11 Correct 10 ms 37308 KB Output is correct
12 Correct 11 ms 37212 KB Output is correct
13 Correct 10 ms 37204 KB Output is correct
14 Correct 10 ms 37212 KB Output is correct
15 Correct 11 ms 37212 KB Output is correct
16 Correct 10 ms 37312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 37212 KB Output is correct
2 Correct 9 ms 37212 KB Output is correct
3 Correct 10 ms 37212 KB Output is correct
4 Correct 9 ms 37212 KB Output is correct
5 Correct 11 ms 37212 KB Output is correct
6 Correct 10 ms 37380 KB Output is correct
7 Correct 10 ms 37208 KB Output is correct
8 Correct 10 ms 37212 KB Output is correct
9 Correct 9 ms 37212 KB Output is correct
10 Correct 10 ms 37212 KB Output is correct
11 Correct 10 ms 37308 KB Output is correct
12 Correct 11 ms 37212 KB Output is correct
13 Correct 10 ms 37204 KB Output is correct
14 Correct 10 ms 37212 KB Output is correct
15 Correct 11 ms 37212 KB Output is correct
16 Correct 10 ms 37312 KB Output is correct
17 Correct 11 ms 37464 KB Output is correct
18 Correct 10 ms 37464 KB Output is correct
19 Correct 10 ms 37720 KB Output is correct
20 Correct 10 ms 37212 KB Output is correct
21 Correct 10 ms 37320 KB Output is correct
22 Correct 11 ms 37212 KB Output is correct
23 Correct 11 ms 37340 KB Output is correct
24 Correct 11 ms 37468 KB Output is correct
25 Correct 11 ms 37480 KB Output is correct
26 Correct 10 ms 37468 KB Output is correct
27 Correct 10 ms 37468 KB Output is correct
28 Correct 10 ms 37468 KB Output is correct
29 Correct 13 ms 37464 KB Output is correct
30 Correct 10 ms 37296 KB Output is correct
31 Correct 11 ms 37464 KB Output is correct
32 Correct 10 ms 37352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 37212 KB Output is correct
2 Correct 9 ms 37212 KB Output is correct
3 Correct 10 ms 37212 KB Output is correct
4 Correct 9 ms 37212 KB Output is correct
5 Correct 11 ms 37212 KB Output is correct
6 Correct 10 ms 37380 KB Output is correct
7 Correct 10 ms 37208 KB Output is correct
8 Correct 10 ms 37212 KB Output is correct
9 Correct 9 ms 37212 KB Output is correct
10 Correct 10 ms 37212 KB Output is correct
11 Correct 10 ms 37308 KB Output is correct
12 Correct 11 ms 37212 KB Output is correct
13 Correct 10 ms 37204 KB Output is correct
14 Correct 10 ms 37212 KB Output is correct
15 Correct 11 ms 37212 KB Output is correct
16 Correct 10 ms 37312 KB Output is correct
17 Correct 11 ms 37464 KB Output is correct
18 Correct 10 ms 37464 KB Output is correct
19 Correct 10 ms 37720 KB Output is correct
20 Correct 10 ms 37212 KB Output is correct
21 Correct 10 ms 37320 KB Output is correct
22 Correct 11 ms 37212 KB Output is correct
23 Correct 11 ms 37340 KB Output is correct
24 Correct 11 ms 37468 KB Output is correct
25 Correct 11 ms 37480 KB Output is correct
26 Correct 10 ms 37468 KB Output is correct
27 Correct 10 ms 37468 KB Output is correct
28 Correct 10 ms 37468 KB Output is correct
29 Correct 13 ms 37464 KB Output is correct
30 Correct 10 ms 37296 KB Output is correct
31 Correct 11 ms 37464 KB Output is correct
32 Correct 10 ms 37352 KB Output is correct
33 Correct 105 ms 48708 KB Output is correct
34 Correct 73 ms 50004 KB Output is correct
35 Correct 94 ms 45640 KB Output is correct
36 Correct 187 ms 56840 KB Output is correct
37 Correct 31 ms 43356 KB Output is correct
38 Correct 194 ms 59196 KB Output is correct
39 Correct 180 ms 58856 KB Output is correct
40 Correct 179 ms 58988 KB Output is correct
41 Correct 204 ms 59172 KB Output is correct
42 Correct 183 ms 58944 KB Output is correct
43 Correct 177 ms 59004 KB Output is correct
44 Correct 188 ms 59000 KB Output is correct
45 Correct 179 ms 58860 KB Output is correct
46 Correct 184 ms 58948 KB Output is correct
47 Correct 187 ms 59020 KB Output is correct
48 Correct 85 ms 55752 KB Output is correct
49 Correct 99 ms 60596 KB Output is correct
50 Correct 30 ms 42096 KB Output is correct
51 Correct 41 ms 46548 KB Output is correct
52 Correct 27 ms 41940 KB Output is correct
53 Correct 121 ms 57216 KB Output is correct
54 Correct 73 ms 46092 KB Output is correct
55 Correct 141 ms 52556 KB Output is correct
56 Correct 99 ms 47380 KB Output is correct
57 Correct 136 ms 55908 KB Output is correct
58 Correct 39 ms 47536 KB Output is correct
59 Correct 40 ms 45528 KB Output is correct
60 Correct 95 ms 58184 KB Output is correct
61 Correct 97 ms 59064 KB Output is correct
62 Correct 74 ms 54756 KB Output is correct
63 Correct 72 ms 52420 KB Output is correct
64 Correct 79 ms 55480 KB Output is correct
65 Correct 127 ms 66244 KB Output is correct
66 Correct 62 ms 43972 KB Output is correct
67 Correct 94 ms 57020 KB Output is correct
68 Correct 162 ms 67840 KB Output is correct
69 Correct 32 ms 39632 KB Output is correct
70 Correct 14 ms 37720 KB Output is correct
71 Correct 75 ms 53452 KB Output is correct
72 Correct 114 ms 64316 KB Output is correct
73 Correct 209 ms 73016 KB Output is correct
74 Correct 213 ms 65520 KB Output is correct
75 Correct 176 ms 72512 KB Output is correct
76 Correct 173 ms 69428 KB Output is correct
77 Correct 208 ms 66156 KB Output is correct