답안 #778809

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
778809 2023-07-10T17:52:15 Z Ahmed57 Cats or Dogs (JOI18_catdog) C++17
100 / 100
364 ms 26828 KB
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")

#include <bits/stdc++.h>
#include "catdog.h"
using namespace std;
vector<int> adj[200001];
int n;
int val[200001];
int sz[200001];
int P[200001];
int arr[200001];
int root[200001];
int down[200001];
void co(int i,int pr){
    sz[i] = 1;
    for(auto j:adj[i]){
        if(j==pr)continue;
        co(j,i);
        sz[i]+=sz[j];
    }
}
int no = 1;
void dfs(int i,int pr,int ro){
    val[i] = no++;
    root[i] = ro;
    down[ro] = i;
    P[i] = pr;
    int ma = -1 , su = 0;
    for(auto j:adj[i]){
        if(j==pr)continue;
        if(ma<sz[j]){
            ma = sz[j];
            su = j;
        }
    }
    if(ma!=-1)dfs(su,i,ro);
    for(auto j:adj[i]){
        if(j==pr||j==su)continue;
        dfs(j,i,j);
    }
}
struct node{
  int dp[2][2];
  node(){
    for(int i = 0;i<2;i++){
      for(int j = 0;j<2;j++){
        dp[i][j] = 1e5;
      }
    }
  }
}seg[400001];
node combine(node a,node b){
    node ans;
    for(int i = 0;i<2;i++){
      for(int j = 0;j<2;j++){
        for(int k = 0;k<2;k++){
          for(int l = 0;l<2;l++){
            ans.dp[i][j] = min(ans.dp[i][j],a.dp[i][k]+b.dp[l][j]+(k != l));
          }
        }
      }
    }
    return ans;
}
void build(int l,int r,int p){
    //cout<<l<<" "<<r<<" "<<p<<endl;
    if(l==r){
        seg[p].dp[0][0] = 0;
        seg[p].dp[1][1] = 0;
        seg[p].dp[0][1] = 1e5;
        seg[p].dp[1][0] = 1e5;
        return ;
    }
    int md = (l+r)/2;
    build(l,md,p*2);
    build(md+1,r,p*2+1);
    seg[p] = combine(seg[p*2],seg[p*2+1]);
}
void update(int l,int r,int p,int idx,pair<int,int> val){
    if(l==r){
        seg[p].dp[0][0] = val.first;
        seg[p].dp[1][1] = val.second;
        seg[p].dp[0][1] = 1e5;
        seg[p].dp[1][0] = 1e5;
        return ;
    }
    int md = (l+r)/2;
    if(idx<=md)update(l,md,p*2,idx,val);
    else update(md+1,r,p*2+1,idx,val);
    seg[p] = combine(seg[p*2],seg[p*2+1]);
}
node query(int l,int r,int p,int lq,int rq){
    //cout<<p<<" "<<l<<" "<<r<<endl;
    if(l>=lq&&r<=rq)return seg[p];
    int md = (l+r)/2;
    if(lq<=md&&rq>=md+1){
        return combine(query(l,md,p*2,lq,rq),query(md+1,r,p*2+1,lq,rq));
    }
    if(lq<=md){
        return query(l,md,p*2,lq,rq);
    }else return query(md+1,r,p*2+1,lq,rq);
}
int coll[100001];
int sum[100001][2];
int prv[100001][2];
node upd(int x){
    if(coll[x]==0)update(1, n,1, val[x], {sum[x][0], 1e5});
    if(coll[x]==1)update(1, n,1, val[x], {1e5, sum[x][1]});
    if(coll[x]==2)update(1, n,1, val[x], {sum[x][0], sum[x][1]});
    x = root[x];
    //cout<<x<<" "<<val[x]<<" "<<val[down[x]]<<"rng"<<endl;
    node ans = query(1,n,1,val[x],val[down[x]]);
    if(x==1){
        return ans;
    }
    for(int i = 0;i<2;i++){
        sum[P[x]][i] -= prv[x][i];
        int best = 1e5;
        for(int j = 0;j<2;j++){
            best = min(best,ans.dp[i][j]);
            best = min(best,ans.dp[i ^ 1][j] + 1);
        }
        prv[x][i] = best;
        sum[P[x]][i]+=prv[x][i];
    }
    return upd(P[x]);
}
void initialize(int N,vector<int>A,vector<int>B){
    n = N;
    coll[0] = 2;coll[N] = 2;
    for(int i = 0;i<N-1;i++){
        coll[i+1] = 2;
        adj[A[i]].push_back(B[i]);
        adj[B[i]].push_back(A[i]);
    }
    //cout<<"ff"<<endl;
    co(1,1);
    //cout<<"ff"<<endl;
    dfs(1,1,1);
    //cout<<"ff"<<endl;
    build(1,n,1);
    //cout<<"ff"<<endl;
}
int cat(int v){
    coll[v] = 0;
    //cout<<"ff"<<endl;
    node ans = upd(v);
    int best = 1e5;
    for(int i = 0;i<2;i++){
        for(int j = 0;j<2;j++){
            best = min(best,ans.dp[i][j]);
        }
    }
    return best;
}int dog(int v){
    coll[v] = 1;
    node ans = upd(v);
    int best = 1e5;
    for(int i = 0;i<2;i++){
        for(int j = 0;j<2;j++){
            best = min(best,ans.dp[i][j]);
        }
    }
    return best;
}int neighbor(int v){
    coll[v] = 2;
    node ans = upd(v);
    int best = 1e5;
    for(int i = 0;i<2;i++){
        for(int j = 0;j<2;j++){
            best = min(best,ans.dp[i][j]);
        }
    }
    return best;
}/*
int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int N = 5;
    vector<int> A = {1,2,2,4};
    vector<int> B = {2,3,4,5};
    initialize(N,A,B);
    cout<<"hh"<<endl;
    cout<<cat(3)<<"jjjjjjjjjjjj"<<endl;
    cout<<dog(5)<<"jjjjjjjjjjjj"<<endl;
    cout<<dog(1)<<"jjjjjjjjjjjj"<<endl;
    cout<<cat(2)<<"jjjjjjjjjjjj"<<endl;
    cout<<neighbor(2)<<"jjjjjjjjjjjj"<<endl;
}*/
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Correct 6 ms 11272 KB Output is correct
3 Correct 6 ms 11280 KB Output is correct
4 Correct 6 ms 11220 KB Output is correct
5 Correct 6 ms 11224 KB Output is correct
6 Correct 7 ms 11348 KB Output is correct
7 Correct 6 ms 11220 KB Output is correct
8 Correct 6 ms 11220 KB Output is correct
9 Correct 6 ms 11220 KB Output is correct
10 Correct 6 ms 11212 KB Output is correct
11 Correct 7 ms 11220 KB Output is correct
12 Correct 7 ms 11276 KB Output is correct
13 Correct 6 ms 11268 KB Output is correct
14 Correct 6 ms 11276 KB Output is correct
15 Correct 6 ms 11220 KB Output is correct
16 Correct 6 ms 11220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Correct 6 ms 11272 KB Output is correct
3 Correct 6 ms 11280 KB Output is correct
4 Correct 6 ms 11220 KB Output is correct
5 Correct 6 ms 11224 KB Output is correct
6 Correct 7 ms 11348 KB Output is correct
7 Correct 6 ms 11220 KB Output is correct
8 Correct 6 ms 11220 KB Output is correct
9 Correct 6 ms 11220 KB Output is correct
10 Correct 6 ms 11212 KB Output is correct
11 Correct 7 ms 11220 KB Output is correct
12 Correct 7 ms 11276 KB Output is correct
13 Correct 6 ms 11268 KB Output is correct
14 Correct 6 ms 11276 KB Output is correct
15 Correct 6 ms 11220 KB Output is correct
16 Correct 6 ms 11220 KB Output is correct
17 Correct 7 ms 11348 KB Output is correct
18 Correct 7 ms 11348 KB Output is correct
19 Correct 7 ms 11280 KB Output is correct
20 Correct 6 ms 11220 KB Output is correct
21 Correct 6 ms 11220 KB Output is correct
22 Correct 7 ms 11276 KB Output is correct
23 Correct 7 ms 11288 KB Output is correct
24 Correct 7 ms 11348 KB Output is correct
25 Correct 7 ms 11292 KB Output is correct
26 Correct 6 ms 11348 KB Output is correct
27 Correct 6 ms 11220 KB Output is correct
28 Correct 6 ms 11332 KB Output is correct
29 Correct 7 ms 11312 KB Output is correct
30 Correct 6 ms 11220 KB Output is correct
31 Correct 6 ms 11280 KB Output is correct
32 Correct 7 ms 11276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Correct 6 ms 11272 KB Output is correct
3 Correct 6 ms 11280 KB Output is correct
4 Correct 6 ms 11220 KB Output is correct
5 Correct 6 ms 11224 KB Output is correct
6 Correct 7 ms 11348 KB Output is correct
7 Correct 6 ms 11220 KB Output is correct
8 Correct 6 ms 11220 KB Output is correct
9 Correct 6 ms 11220 KB Output is correct
10 Correct 6 ms 11212 KB Output is correct
11 Correct 7 ms 11220 KB Output is correct
12 Correct 7 ms 11276 KB Output is correct
13 Correct 6 ms 11268 KB Output is correct
14 Correct 6 ms 11276 KB Output is correct
15 Correct 6 ms 11220 KB Output is correct
16 Correct 6 ms 11220 KB Output is correct
17 Correct 7 ms 11348 KB Output is correct
18 Correct 7 ms 11348 KB Output is correct
19 Correct 7 ms 11280 KB Output is correct
20 Correct 6 ms 11220 KB Output is correct
21 Correct 6 ms 11220 KB Output is correct
22 Correct 7 ms 11276 KB Output is correct
23 Correct 7 ms 11288 KB Output is correct
24 Correct 7 ms 11348 KB Output is correct
25 Correct 7 ms 11292 KB Output is correct
26 Correct 6 ms 11348 KB Output is correct
27 Correct 6 ms 11220 KB Output is correct
28 Correct 6 ms 11332 KB Output is correct
29 Correct 7 ms 11312 KB Output is correct
30 Correct 6 ms 11220 KB Output is correct
31 Correct 6 ms 11280 KB Output is correct
32 Correct 7 ms 11276 KB Output is correct
33 Correct 220 ms 17600 KB Output is correct
34 Correct 74 ms 17464 KB Output is correct
35 Correct 205 ms 16244 KB Output is correct
36 Correct 322 ms 21724 KB Output is correct
37 Correct 18 ms 14152 KB Output is correct
38 Correct 364 ms 22772 KB Output is correct
39 Correct 357 ms 22816 KB Output is correct
40 Correct 362 ms 22840 KB Output is correct
41 Correct 349 ms 22780 KB Output is correct
42 Correct 330 ms 22904 KB Output is correct
43 Correct 340 ms 22780 KB Output is correct
44 Correct 324 ms 22788 KB Output is correct
45 Correct 328 ms 22716 KB Output is correct
46 Correct 331 ms 22716 KB Output is correct
47 Correct 343 ms 22720 KB Output is correct
48 Correct 108 ms 19016 KB Output is correct
49 Correct 117 ms 20724 KB Output is correct
50 Correct 47 ms 13580 KB Output is correct
51 Correct 52 ms 14964 KB Output is correct
52 Correct 24 ms 13208 KB Output is correct
53 Correct 149 ms 21060 KB Output is correct
54 Correct 113 ms 15876 KB Output is correct
55 Correct 290 ms 19604 KB Output is correct
56 Correct 180 ms 16752 KB Output is correct
57 Correct 242 ms 20932 KB Output is correct
58 Correct 27 ms 14984 KB Output is correct
59 Correct 48 ms 14820 KB Output is correct
60 Correct 105 ms 19856 KB Output is correct
61 Correct 111 ms 20300 KB Output is correct
62 Correct 68 ms 18184 KB Output is correct
63 Correct 41 ms 17784 KB Output is correct
64 Correct 46 ms 19232 KB Output is correct
65 Correct 51 ms 23560 KB Output is correct
66 Correct 65 ms 14716 KB Output is correct
67 Correct 56 ms 20044 KB Output is correct
68 Correct 113 ms 24192 KB Output is correct
69 Correct 32 ms 12700 KB Output is correct
70 Correct 10 ms 11548 KB Output is correct
71 Correct 55 ms 17236 KB Output is correct
72 Correct 70 ms 22092 KB Output is correct
73 Correct 184 ms 26828 KB Output is correct
74 Correct 212 ms 23988 KB Output is correct
75 Correct 141 ms 26792 KB Output is correct
76 Correct 127 ms 25752 KB Output is correct
77 Correct 212 ms 24312 KB Output is correct