Submission #3387789


Source Code Expand

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <limits.h>
#include <math.h>
#include <functional>
#include <bitset>

#define repeat(i,n) for (long long i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i] << '\n'
#define debugArrayP(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i].first<< " " << x[i].second << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef vector<int> vint;
typedef vector<ll> vll;
const ll INF = INT_MAX;
const ll MOD = 1e9+7;

struct HLDecomposition{
public:
    int V;
    vector<vint> g;
    vint dep,par,head,size,inv;
    vint in,out;
    HLDecomposition(int size_)
    :V(size_),g(V),dep(V,0),par(V,-1),head(V),size(V),inv(V),in(V),out(V),t(0){}

    void add_edge(int u,int v){
        g[u].push_back(v);
        g[v].push_back(u);
    }
    void build(int root=0){
        par[root]=0;
        dfs_size(root);
        par[root]=-1;
        dfs_hld(root);
    }

    int lca(int u,int v){
        while(1){
            if(in[u]>in[v])swap(u,v);
            if(head[u]==head[v])return u;
            v=par[head[v]];
        }
    }

    int distance(int u,int v){
        return dep[u]+dep[v]-2*dep[lca(u,v)];
    }

    int operator[](const int &k){
      return in[k];
    }
private:
    int t;
    void dfs_size(int v=0){
        size[v]=1;
        for(int& u:g[v]){
            if(par[u]>=0)continue;
            par[u]=v;
            dfs_size(u);
            size[v]+=size[u];
            if(size[u]>size[g[v][0]]){
                swap(u,g[v][0]);
            }
        }
    }
    void dfs_hld(int v=0){
        in[v] = t++;
        inv[in[v]]=v;
        for(int& u:g[v]){
            if(par[u]!=v)continue;
            head[u]=(u==g[v][0]?head[v]:u);
            dfs_hld(u);
        }
        out[v]=t;
    }
};




template<typename T>
struct segtree {
    int N;
    vector<T> node;
    //-------------書くとこ-------------------
    //単位元 ex)INF,0
    T default_value = 1;
    static inline T merge(const T& l, const T& r) {
        return l&r;
    }
    //----------------------------------------
    segtree(int n) {
        for(N=1;N<n;N*=2);
        node.resize(2 * N, default_value);
    }
    segtree(vector<int> v) {
        int sz = v.size();
        for(N=1;N<sz;N*=2);
        node.resize(2 * N, default_value);
        for (int i = 0; i < sz; i++)
            node[i + N - 1] = v[i];
        for (int i = N - 2; i >= 0; i--)
            node[i] = merge(node[2 * i + 1], node[2 * i + 2]);
    }
    void init(int k, T val) {
        k += N - 1; // leaf
        node[k] = val;
        while (k > 0) {
            k = (k - 1) / 2;
            node[k] = merge(node[k * 2 + 1], node[k * 2 + 2]);
        }
    }
    // update k th element
    void update(int k) {
        k += N - 1; // leaf
        node[k] ^= 1;
        while (k > 0) {
            k = (k - 1) / 2;
            node[k] = merge(node[k * 2 + 1], node[k * 2 + 2]);
        }
    }
    // [a, b)
    T query(int a, int b, int k=0, int l=0, int r=-1) {
      if(r<0)r=N;
        if (r <= a or b <= l)
            return default_value;
        if (a <= l and r <= b)
            return node[k];
        int m = (l + r) / 2;
        T vl = query(a, b, k * 2 + 1, l, m);
        T vr = query(a, b, k * 2 + 2, m, r);
        return merge(vl, vr);
    }

    T operator[](const int &k) {
        return node[k + N - 1];
    }
};


int main(){
  int N;cin>>N;
  HLDecomposition hld(N);
  vint p(N);
  p[0]=-1;
  repeat(i,N-1){
    cin>>p[i+1];p[i+1]--;
    hld.add_edge(i+1,p[i+1]);
  }
  hld.build();
  //debugArray(hld,N);
  segtree<ll> seg(N);
  vint C(N);
  repeat(i,N){
    cin>>C[i];
  }
  repeat(i,N-1){
    seg.init(hld[i+1],C[p[i+1]]!=C[i+1]);
  }
  debugArray(seg,N);
  int Q;cin>>Q;
  repeat(q,Q){
    int op,u;cin>>op>>u;u--;
    if(op==1){
      seg.update(hld[u]);
      //debugArray(seg,N);
    }else{
      int v;cin>>v;v--;
      bool isok=true;
      while(1){
        if(hld[u]>hld[v])swap(u,v);
        if(hld.head[u]!=hld.head[v]){
          isok &= seg.query(hld[hld.head[v]],hld[v]+1);
          v = hld.par[hld.head[v]];
        }else{
          if(u!=v)isok &= seg.query(hld[u]+1,hld[v]+1);
          break;
        }
      }
      cout << (isok? "YES":"NO") << endl;
    }
  }
  return 0;
}

Submission Info

Submission Time
Task H - 白黒ツリー
User hashiryo
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 4800 Byte
Status AC
Exec Time 690 ms
Memory 16384 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 1 ms 256 KB
00_sample_01 AC 1 ms 256 KB
00_sample_02 AC 1 ms 256 KB
00_sample_03 AC 1 ms 256 KB
00_sample_04 AC 1 ms 256 KB
01_graph_random_color_random_query_random_00 AC 135 ms 3456 KB
01_graph_random_color_random_query_random_01 AC 107 ms 3456 KB
01_graph_random_color_random_query_random_02 AC 52 ms 1536 KB
01_graph_random_color_random_query_random_03 AC 55 ms 1920 KB
01_graph_random_color_random_query_random_04 AC 73 ms 1664 KB
01_graph_random_color_random_query_random_05 AC 78 ms 1408 KB
01_graph_random_color_random_query_random_06 AC 104 ms 2560 KB
01_graph_random_color_random_query_random_07 AC 322 ms 13696 KB
01_graph_random_color_random_query_random_08 AC 211 ms 6656 KB
01_graph_random_color_random_query_random_09 AC 234 ms 8832 KB
02_graph_random_color_same_query_random_00 AC 100 ms 2560 KB
02_graph_random_color_same_query_random_01 AC 345 ms 11392 KB
02_graph_random_color_same_query_random_02 AC 103 ms 3584 KB
02_graph_random_color_same_query_random_03 AC 231 ms 1920 KB
02_graph_random_color_same_query_random_04 AC 205 ms 4608 KB
02_graph_random_color_same_query_random_05 AC 225 ms 6912 KB
02_graph_random_color_same_query_random_06 AC 53 ms 1536 KB
02_graph_random_color_same_query_random_07 AC 173 ms 3584 KB
02_graph_random_color_same_query_random_08 AC 91 ms 2944 KB
02_graph_random_color_same_query_random_09 AC 57 ms 1664 KB
03_graph_random_color_alternate_query_random_00 AC 55 ms 1920 KB
03_graph_random_color_alternate_query_random_01 AC 293 ms 11260 KB
03_graph_random_color_alternate_query_random_02 AC 85 ms 3200 KB
03_graph_random_color_alternate_query_random_03 AC 147 ms 4864 KB
03_graph_random_color_alternate_query_random_04 AC 153 ms 5504 KB
03_graph_random_color_alternate_query_random_05 AC 254 ms 10240 KB
03_graph_random_color_alternate_query_random_06 AC 316 ms 13952 KB
03_graph_random_color_alternate_query_random_07 AC 131 ms 3328 KB
03_graph_random_color_alternate_query_random_08 AC 318 ms 10880 KB
03_graph_random_color_alternate_query_random_09 AC 249 ms 7168 KB
10_graph_path_color_alternate_query_all2_00 AC 569 ms 16384 KB
10_graph_path_color_random_query_random_00 AC 537 ms 16256 KB
10_graph_path_color_random_query_random_01 AC 358 ms 16000 KB
10_graph_path_color_random_query_random_02 AC 351 ms 16000 KB
10_graph_path_color_random_query_random_03 AC 351 ms 16000 KB
10_graph_path_color_random_query_random_04 AC 349 ms 16000 KB
10_graph_path_color_random_query_random_05 AC 357 ms 16000 KB
10_graph_path_color_same_query_all2_00 AC 573 ms 16256 KB
10_graph_path_color_sametoAlternate_query_11...12_00 AC 345 ms 16000 KB
20_graph_star_color_alternate_query_all2_00 AC 573 ms 12024 KB
20_graph_star_color_random_query_1Center2random_00 AC 574 ms 12024 KB
20_graph_star_color_random_query_1Center2random_01 AC 554 ms 12024 KB
20_graph_star_color_random_query_1Center2random_02 AC 531 ms 11896 KB
20_graph_star_color_random_query_1Center2random_03 AC 505 ms 11896 KB
20_graph_star_color_random_query_1Center2random_04 AC 485 ms 11896 KB
20_graph_star_color_random_query_1Center2random_05 AC 461 ms 11896 KB
20_graph_star_color_random_query_1Center2random_06 AC 438 ms 11768 KB
20_graph_star_color_random_query_1Center2random_07 AC 421 ms 11768 KB
20_graph_star_color_random_query_1Center2random_08 AC 390 ms 11768 KB
20_graph_star_color_random_query_1Center2random_09 AC 366 ms 11768 KB
20_graph_star_color_random_query_1Center2random_10 AC 346 ms 11640 KB
20_graph_star_color_random_query_random_00 AC 487 ms 11896 KB
20_graph_star_color_random_query_random_01 AC 365 ms 11768 KB
20_graph_star_color_random_query_random_02 AC 347 ms 11640 KB
20_graph_star_color_random_query_random_03 AC 344 ms 11640 KB
20_graph_star_color_random_query_random_04 AC 345 ms 11640 KB
20_graph_star_color_random_query_random_05 AC 358 ms 11640 KB
20_graph_star_color_same_query_all2_00 AC 575 ms 12024 KB
20_graph_star_color_sametoAlternate_query_11...12_00 AC 340 ms 11640 KB
30_graph_hitode_color_random_query_random_00 AC 611 ms 13184 KB
30_graph_hitode_color_random_query_random_01 AC 381 ms 12928 KB
30_graph_hitode_color_random_query_random_02 AC 372 ms 12800 KB
30_graph_hitode_color_random_query_random_03 AC 369 ms 12800 KB
30_graph_hitode_color_random_query_random_04 AC 369 ms 12800 KB
30_graph_hitode_color_random_query_random_05 AC 368 ms 12928 KB
40_graph_caterpillar_color_random_query_random_00 AC 513 ms 11904 KB
40_graph_caterpillar_color_random_query_random_01 AC 359 ms 11776 KB
40_graph_caterpillar_color_random_query_random_02 AC 344 ms 11776 KB
40_graph_caterpillar_color_random_query_random_03 AC 342 ms 11776 KB
40_graph_caterpillar_color_random_query_random_04 AC 342 ms 13824 KB
40_graph_caterpillar_color_random_query_random_05 AC 343 ms 11776 KB
50_graph_binaryTree_color_random_query_random_00 AC 690 ms 11520 KB
50_graph_binaryTree_color_random_query_random_01 AC 410 ms 11264 KB
50_graph_binaryTree_color_random_query_random_02 AC 363 ms 11264 KB
50_graph_binaryTree_color_random_query_random_03 AC 374 ms 11264 KB
50_graph_binaryTree_color_random_query_random_04 AC 363 ms 11264 KB
50_graph_binaryTree_color_random_query_random_05 AC 364 ms 11264 KB
60_graph_ternaryTree_color_random_query_random_00 AC 417 ms 11264 KB
60_graph_ternaryTree_color_random_query_random_01 AC 379 ms 11264 KB
60_graph_ternaryTree_color_random_query_random_02 AC 363 ms 11264 KB
60_graph_ternaryTree_color_random_query_random_03 AC 360 ms 11264 KB
60_graph_ternaryTree_color_random_query_random_04 AC 356 ms 11264 KB
60_graph_ternaryTree_color_random_query_random_05 AC 358 ms 11264 KB
70_graph_triangle_color_random_query_random_00 AC 601 ms 11520 KB
70_graph_triangle_color_random_query_random_01 AC 371 ms 11264 KB
70_graph_triangle_color_random_query_random_02 AC 373 ms 11264 KB
70_graph_triangle_color_random_query_random_03 AC 371 ms 11264 KB
70_graph_triangle_color_random_query_random_04 AC 367 ms 11264 KB
70_graph_triangle_color_random_query_random_05 AC 367 ms 11264 KB