Submission #2968462


Source Code Expand

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
int n,k,q,sz[100005],hv[100005],vs[100005],hd[100005],dp[100005],cn[100005],id[1000005],bit[100005],pa[100005],c[100005];
vector<int>g[100005];
void dfs(int v,int p){
    sz[v]=1;
    hv[v]=-1;
    for(int i=0;i<g[v].size();i++){
        if(g[v][i]!=p){
            dfs(g[v][i],v);
            sz[v]+=sz[g[v][i]];
            if(hv[v]==-1||sz[hv[v]]<sz[g[v][i]])hv[v]=g[v][i];
        }
    }
}
void dfs1(int v,int d,int p,int h){
    hd[k]=h;
    cn[k]=cn[h];
    id[v]=k;
    vs[k]=v;
    dp[k++]=d;
    if(hv[v]!=-1)dfs1(hv[v],d+1,v,h);
    for(int i=0;i<g[v].size();i++){
        if(g[v][i]!=p&&g[v][i]!=hv[v]){
            cn[k]=id[v];
            dfs1(g[v][i],d+1,v,k);
        }
    }
}
void add(int a,int b){
    a++;
    while(a<=n){
        bit[a]+=b;
        a+=(a&-a);
    }
}
int su(int a){
    a++;
    int res=0;
    while(a>0){
        res+=bit[a];
        a-=(a&-a);
    }
    return res;
}
int sum(int a,int b){
    return su(b)-su(a-1);
}
int lca(int u,int v){
    int res=0;
    u=id[u];v=id[v];
    while(hd[u]!=hd[v]){
        if(dp[hd[u]]>dp[hd[v]]){
            res+=sum(hd[u],u);
            u=cn[u];
        }else{
            res+=sum(hd[v],v);
            v=cn[v];
        }
    }
    return res+sum(min(u,v)+1,max(u,v));
}
int main(void){
    cin>>n;
    for(int i=1;i<n;i++){
        cin>>pa[i];
        g[--pa[i]].push_back(i);
    }
    dfs(0,-1);
    dfs1(0,0,-1,0);
    for(int i=0;i<n;i++){
        cin>>c[i];
    }
    for(int i=1;i<n;i++){
        if(c[i]==c[pa[i]]){
            pa[i]=1;
            add(id[i],1);
        }else pa[i]=0;
    }
    cin>>q;
    for(int i=0;i<q;i++){
        int t;
        cin>>t;
        if(t==1){
            int v;
            cin>>v;
            v--;
            if(v==0)continue;
            if(pa[v]==1){
                add(id[v],-1);
                pa[v]=0;
            }else{
                add(id[v],1);
                pa[v]=1;
            }
        }else{
            int u,v;
            cin>>u>>v;
            u--;
            v--;
            if(lca(u,v)==0)cout<<"YES\n";
            else cout<<"NO\n";
        }
    }
}

Submission Info

Submission Time
Task H - 白黒ツリー
User nxteru
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 2314 Byte
Status AC
Exec Time 330 ms
Memory 19328 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 5
AC × 94
Set Name Test Cases
Sample 00_sample_00, 00_sample_01, 00_sample_02, 00_sample_03, 00_sample_04
All 00_sample_00, 00_sample_01, 00_sample_02, 00_sample_03, 00_sample_04, 01_graph_random_color_random_query_random_00, 01_graph_random_color_random_query_random_01, 01_graph_random_color_random_query_random_02, 01_graph_random_color_random_query_random_03, 01_graph_random_color_random_query_random_04, 01_graph_random_color_random_query_random_05, 01_graph_random_color_random_query_random_06, 01_graph_random_color_random_query_random_07, 01_graph_random_color_random_query_random_08, 01_graph_random_color_random_query_random_09, 02_graph_random_color_same_query_random_00, 02_graph_random_color_same_query_random_01, 02_graph_random_color_same_query_random_02, 02_graph_random_color_same_query_random_03, 02_graph_random_color_same_query_random_04, 02_graph_random_color_same_query_random_05, 02_graph_random_color_same_query_random_06, 02_graph_random_color_same_query_random_07, 02_graph_random_color_same_query_random_08, 02_graph_random_color_same_query_random_09, 03_graph_random_color_alternate_query_random_00, 03_graph_random_color_alternate_query_random_01, 03_graph_random_color_alternate_query_random_02, 03_graph_random_color_alternate_query_random_03, 03_graph_random_color_alternate_query_random_04, 03_graph_random_color_alternate_query_random_05, 03_graph_random_color_alternate_query_random_06, 03_graph_random_color_alternate_query_random_07, 03_graph_random_color_alternate_query_random_08, 03_graph_random_color_alternate_query_random_09, 10_graph_path_color_alternate_query_all2_00, 10_graph_path_color_random_query_random_00, 10_graph_path_color_random_query_random_01, 10_graph_path_color_random_query_random_02, 10_graph_path_color_random_query_random_03, 10_graph_path_color_random_query_random_04, 10_graph_path_color_random_query_random_05, 10_graph_path_color_same_query_all2_00, 10_graph_path_color_sametoAlternate_query_11...12_00, 20_graph_star_color_alternate_query_all2_00, 20_graph_star_color_random_query_1Center2random_00, 20_graph_star_color_random_query_1Center2random_01, 20_graph_star_color_random_query_1Center2random_02, 20_graph_star_color_random_query_1Center2random_03, 20_graph_star_color_random_query_1Center2random_04, 20_graph_star_color_random_query_1Center2random_05, 20_graph_star_color_random_query_1Center2random_06, 20_graph_star_color_random_query_1Center2random_07, 20_graph_star_color_random_query_1Center2random_08, 20_graph_star_color_random_query_1Center2random_09, 20_graph_star_color_random_query_1Center2random_10, 20_graph_star_color_random_query_random_00, 20_graph_star_color_random_query_random_01, 20_graph_star_color_random_query_random_02, 20_graph_star_color_random_query_random_03, 20_graph_star_color_random_query_random_04, 20_graph_star_color_random_query_random_05, 20_graph_star_color_same_query_all2_00, 20_graph_star_color_sametoAlternate_query_11...12_00, 30_graph_hitode_color_random_query_random_00, 30_graph_hitode_color_random_query_random_01, 30_graph_hitode_color_random_query_random_02, 30_graph_hitode_color_random_query_random_03, 30_graph_hitode_color_random_query_random_04, 30_graph_hitode_color_random_query_random_05, 40_graph_caterpillar_color_random_query_random_00, 40_graph_caterpillar_color_random_query_random_01, 40_graph_caterpillar_color_random_query_random_02, 40_graph_caterpillar_color_random_query_random_03, 40_graph_caterpillar_color_random_query_random_04, 40_graph_caterpillar_color_random_query_random_05, 50_graph_binaryTree_color_random_query_random_00, 50_graph_binaryTree_color_random_query_random_01, 50_graph_binaryTree_color_random_query_random_02, 50_graph_binaryTree_color_random_query_random_03, 50_graph_binaryTree_color_random_query_random_04, 50_graph_binaryTree_color_random_query_random_05, 60_graph_ternaryTree_color_random_query_random_00, 60_graph_ternaryTree_color_random_query_random_01, 60_graph_ternaryTree_color_random_query_random_02, 60_graph_ternaryTree_color_random_query_random_03, 60_graph_ternaryTree_color_random_query_random_04, 60_graph_ternaryTree_color_random_query_random_05, 70_graph_triangle_color_random_query_random_00, 70_graph_triangle_color_random_query_random_01, 70_graph_triangle_color_random_query_random_02, 70_graph_triangle_color_random_query_random_03, 70_graph_triangle_color_random_query_random_04, 70_graph_triangle_color_random_query_random_05
Case Name Status Exec Time Memory
00_sample_00 AC 3 ms 6400 KB
00_sample_01 AC 3 ms 6400 KB
00_sample_02 AC 3 ms 6400 KB
00_sample_03 AC 3 ms 6400 KB
00_sample_04 AC 3 ms 6400 KB
01_graph_random_color_random_query_random_00 AC 74 ms 7424 KB
01_graph_random_color_random_query_random_01 AC 43 ms 7040 KB
01_graph_random_color_random_query_random_02 AC 29 ms 6656 KB
01_graph_random_color_random_query_random_03 AC 24 ms 7040 KB
01_graph_random_color_random_query_random_04 AC 50 ms 7040 KB
01_graph_random_color_random_query_random_05 AC 62 ms 6784 KB
01_graph_random_color_random_query_random_06 AC 63 ms 7040 KB
01_graph_random_color_random_query_random_07 AC 88 ms 8448 KB
01_graph_random_color_random_query_random_08 AC 82 ms 7680 KB
01_graph_random_color_random_query_random_09 AC 74 ms 7936 KB
02_graph_random_color_same_query_random_00 AC 62 ms 7040 KB
02_graph_random_color_same_query_random_01 AC 119 ms 8448 KB
02_graph_random_color_same_query_random_02 AC 35 ms 7680 KB
02_graph_random_color_same_query_random_03 AC 182 ms 7552 KB
02_graph_random_color_same_query_random_04 AC 87 ms 7808 KB
02_graph_random_color_same_query_random_05 AC 82 ms 8704 KB
02_graph_random_color_same_query_random_06 AC 30 ms 6656 KB
02_graph_random_color_same_query_random_07 AC 104 ms 8192 KB
02_graph_random_color_same_query_random_08 AC 35 ms 7296 KB
02_graph_random_color_same_query_random_09 AC 32 ms 6784 KB
03_graph_random_color_alternate_query_random_00 AC 19 ms 6912 KB
03_graph_random_color_alternate_query_random_01 AC 65 ms 8444 KB
03_graph_random_color_alternate_query_random_02 AC 21 ms 7424 KB
03_graph_random_color_alternate_query_random_03 AC 58 ms 7168 KB
03_graph_random_color_alternate_query_random_04 AC 41 ms 7296 KB
03_graph_random_color_alternate_query_random_05 AC 57 ms 8192 KB
03_graph_random_color_alternate_query_random_06 AC 86 ms 14976 KB
03_graph_random_color_alternate_query_random_07 AC 71 ms 7424 KB
03_graph_random_color_alternate_query_random_08 AC 102 ms 10368 KB
03_graph_random_color_alternate_query_random_09 AC 86 ms 8704 KB
10_graph_path_color_alternate_query_all2_00 AC 318 ms 19328 KB
10_graph_path_color_random_query_random_00 AC 294 ms 19072 KB
10_graph_path_color_random_query_random_01 AC 133 ms 18944 KB
10_graph_path_color_random_query_random_02 AC 128 ms 18944 KB
10_graph_path_color_random_query_random_03 AC 126 ms 18944 KB
10_graph_path_color_random_query_random_04 AC 126 ms 18944 KB
10_graph_path_color_random_query_random_05 AC 127 ms 18944 KB
10_graph_path_color_same_query_all2_00 AC 325 ms 19200 KB
10_graph_path_color_sametoAlternate_query_11...12_00 AC 126 ms 18944 KB
20_graph_star_color_alternate_query_all2_00 AC 310 ms 8824 KB
20_graph_star_color_random_query_1Center2random_00 AC 313 ms 8824 KB
20_graph_star_color_random_query_1Center2random_01 AC 296 ms 8824 KB
20_graph_star_color_random_query_1Center2random_02 AC 274 ms 8696 KB
20_graph_star_color_random_query_1Center2random_03 AC 257 ms 8696 KB
20_graph_star_color_random_query_1Center2random_04 AC 237 ms 8696 KB
20_graph_star_color_random_query_1Center2random_05 AC 217 ms 8696 KB
20_graph_star_color_random_query_1Center2random_06 AC 195 ms 8568 KB
20_graph_star_color_random_query_1Center2random_07 AC 177 ms 8568 KB
20_graph_star_color_random_query_1Center2random_08 AC 155 ms 8568 KB
20_graph_star_color_random_query_1Center2random_09 AC 137 ms 8568 KB
20_graph_star_color_random_query_1Center2random_10 AC 115 ms 8440 KB
20_graph_star_color_random_query_random_00 AC 235 ms 8696 KB
20_graph_star_color_random_query_random_01 AC 131 ms 8440 KB
20_graph_star_color_random_query_random_02 AC 119 ms 8440 KB
20_graph_star_color_random_query_random_03 AC 117 ms 8440 KB
20_graph_star_color_random_query_random_04 AC 117 ms 8440 KB
20_graph_star_color_random_query_random_05 AC 116 ms 8440 KB
20_graph_star_color_same_query_all2_00 AC 315 ms 8824 KB
20_graph_star_color_sametoAlternate_query_11...12_00 AC 113 ms 8440 KB
30_graph_hitode_color_random_query_random_00 AC 330 ms 13952 KB
30_graph_hitode_color_random_query_random_01 AC 146 ms 13696 KB
30_graph_hitode_color_random_query_random_02 AC 141 ms 13696 KB
30_graph_hitode_color_random_query_random_03 AC 136 ms 13696 KB
30_graph_hitode_color_random_query_random_04 AC 139 ms 13696 KB
30_graph_hitode_color_random_query_random_05 AC 139 ms 13696 KB
40_graph_caterpillar_color_random_query_random_00 AC 254 ms 8824 KB
40_graph_caterpillar_color_random_query_random_01 AC 125 ms 8576 KB
40_graph_caterpillar_color_random_query_random_02 AC 115 ms 8568 KB
40_graph_caterpillar_color_random_query_random_03 AC 113 ms 8576 KB
40_graph_caterpillar_color_random_query_random_04 AC 113 ms 8576 KB
40_graph_caterpillar_color_random_query_random_05 AC 113 ms 8544 KB
50_graph_binaryTree_color_random_query_random_00 AC 301 ms 9728 KB
50_graph_binaryTree_color_random_query_random_01 AC 154 ms 9600 KB
50_graph_binaryTree_color_random_query_random_02 AC 131 ms 9472 KB
50_graph_binaryTree_color_random_query_random_03 AC 130 ms 9472 KB
50_graph_binaryTree_color_random_query_random_04 AC 133 ms 9472 KB
50_graph_binaryTree_color_random_query_random_05 AC 134 ms 9472 KB
60_graph_ternaryTree_color_random_query_random_00 AC 159 ms 9088 KB
60_graph_ternaryTree_color_random_query_random_01 AC 140 ms 8960 KB
60_graph_ternaryTree_color_random_query_random_02 AC 131 ms 8960 KB
60_graph_ternaryTree_color_random_query_random_03 AC 129 ms 8960 KB
60_graph_ternaryTree_color_random_query_random_04 AC 126 ms 8960 KB
60_graph_ternaryTree_color_random_query_random_05 AC 128 ms 8960 KB
70_graph_triangle_color_random_query_random_00 AC 299 ms 11392 KB
70_graph_triangle_color_random_query_random_01 AC 140 ms 11136 KB
70_graph_triangle_color_random_query_random_02 AC 140 ms 11136 KB
70_graph_triangle_color_random_query_random_03 AC 137 ms 11136 KB
70_graph_triangle_color_random_query_random_04 AC 138 ms 11136 KB
70_graph_triangle_color_random_query_random_05 AC 137 ms 11136 KB