Submission #2966789


Source Code Expand

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// RMQ (to find LCA)
template<class VAL> struct RMQ {
    vector<pair<VAL, int> > dat;
    int SIZE_R;
    VAL INF = 1<<29; // to be set
    
    RMQ(int n = 110000) { init(n); }
    void init(int n) {
        SIZE_R = 1;
        while (SIZE_R < n) SIZE_R *= 2;
        dat.resize(SIZE_R * 2 - 1);
        for (int i = 0; i < (int)dat.size(); ++i) dat[i] = make_pair(INF, -1);
    }

    inline void set(int a, VAL v) {
        int k = a + SIZE_R - 1;
        dat[k] = make_pair(v, a);
        while (k > 0) {
            k = (k-1)/2;
            dat[k] = min(dat[k*2+1], dat[k*2+2]);
        }
    }
    
    inline pair<VAL,int> get(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return make_pair(INF, -1);
        if (a <= l && r <= b) return dat[k];
        else {
            pair<VAL,int> vl = get(a, b, k*2+1, l, (l+r)/2);
            pair<VAL,int> vr = get(a, b, k*2+2, (l+r)/2, r);
            return min(vl, vr);
        }
    }
    inline pair<VAL,int> get(int a, int b) { return get(a, b, 0, 0, SIZE_R); }
    
    void print() {
        for (int i = 0; i < SIZE_R; ++i) {
            VAL val = (*this)[i];
            if (val < INF) cout << val;
            else cout << "INF";
            if (i != SIZE_R-1) cout << ",";
        }
        cout << endl;
    }
};

// BIT (to find cost of path in the tree)
template<class Abel> struct BIT {
    vector<Abel> dat;
    Abel UNITY_SUM = 0;						// to be set
    
    /* [1, n] */
    BIT(int n = 110000) { init(n); }
    void init(int n) {
        dat.resize(n + 1);
        for (int i = 0; i < (int)dat.size(); ++i) dat[i] = UNITY_SUM;
    }
    
    /* a is 1-indexed */
    inline void add(int a, Abel x) {
        for (int i = a; i < (int)dat.size(); i += i & -i)
            dat[i] = dat[i] + x;
    }
    
    /* [1, a], a is 1-indexed */
    inline Abel sum(int a) {
        Abel res = UNITY_SUM;
        for (int i = a; i > 0; i -= i & -i)
            res = res + dat[i];
        return res;
    }
    
    /* [a, b), a and b are 1-indexed */
    inline Abel sum(int a, int b) {
        return sum(b - 1) - sum(a - 1);
    }
    
    /* a is 1-indexed */
    inline Abel operator [] (int a) {
        return sum(a, a+1);
    }
    
    void print() {
        for (int i = 1; i < (int)dat.size(); ++i) cout << sum(i, i + 1) << ",";
        cout << endl;
    }
};

// Graph
template<class VAL> struct Edge {
    int idx, from, to;
    VAL cost;
    Edge(int idx_, int from_, int to_, VAL cost_) : idx(idx_), from(from_), to(to_), cost(cost_) {}
    friend ostream& operator << (ostream& s, const Edge<VAL>& e) { return s << e.from << "->" << e.to << '(' << e.cost << ')'; }
};

template<class VAL> struct Graph {
    int iter = 0;
    vector<vector<Edge<VAL> > > list;
    Graph(int n = 110000) : iter(0) { init(n); }
    void init(int n = 110000) { iter = 0; list.clear(); list.resize(n); }
    inline vector<Edge<VAL> > operator [] (int i) {return list[i];}
    size_t size() const { return list.size(); }
    
    void connect(int f, int t, VAL c) {
        list[f].push_back(Edge<VAL>(iter, f, t, c));
        list[t].push_back(Edge<VAL>(iter, t, f, c));
        ++iter;
    }
    
    friend ostream& operator << (ostream& s, const Graph& G) {
        s << endl; for (int i = 0; i < (int)G.size(); ++i) {s << i << " : " << G.list[i] << endl;}
        return s;
    }
};

// Euler Tour
template<class VAL> struct EulerTour {
    // main results
    Graph<VAL> tree;
    vector<int> depth;
    vector<int> node; // the node-number of i-th element of Euler-tour
    vector<int> vf, ve; // the index of Euler-tour of node v
    vector<int> eid; // the index of edge e (i*2 + (0: dir to leaf, 1: dir to root))
    
    // sub results
    RMQ<int> rmq; // depth (to find LCA)
    BIT<VAL> bit; // to calc sum of edges
    
    EulerTour(const Graph<VAL> &tree_) { init(tree_); }
    void init(const Graph<VAL> &tree_) {
        tree = tree_;
        int V = (int)tree.size();
        depth.resize(V*2-1); node.resize(V*2-1); vf.resize(V); ve.resize(V); eid.resize((V-1)*2);
        rmq.init(V*2-1); bit.init((V-1)*2);
        int k = 0;
        dfs(0, -1, 0, k);
        for (int i = 0; i < V*2-1; ++i) rmq.set(i, depth[i]);
    }
    
    void dfs(int v, int par, int dep, int &ord) {
        node[ord] = v;
        depth[ord] = dep;
        vf[v] = ve[v] = ord;
        ++ord;
        for (auto e : tree[v]) {
            if (e.to == par) continue;
            bit.add(ord, e.cost);
            eid[e.idx*2] = ord;
            dfs(e.to, v, dep+1, ord);
            node[ord] = v;
            depth[ord] = dep;
            ve[v] = ord;
            eid[e.idx*2+1] = ord;
            bit.add(ord, -e.cost); // minus cost for opposite direction
            ++ord;
        }
    }

    inline int LCA(int u, int v) {
        int a = vf[u], b = vf[v];
        if (a > b) swap(a, b);
        return node[rmq.get(a, b+1).second];
    }
    
    inline void add(int index_edge, VAL v) {
        bit.add(eid[index_edge*2], v);
        bit.add(eid[index_edge*2+1], -v);
    }
    
    inline VAL sum(int u, int v) {
        int lca = LCA(u, v);
        return bit.sum(vf[u]) + bit.sum(vf[v]) - bit.sum(vf[lca])*2;
    }
    
    //void print() { COUT(node); COUT(vf); COUT(eid); bit.print(); }
};

int N, Q;
vector<int> P, C;
Graph<int> tree;

int main() {
    cin >> N;
    tree.init(N);
    P.resize(N-1); C.resize(N);
    for (int i = 0; i < N-1; ++i) scanf("%d", &P[i]), --P[i];
    for (int i = 0; i < N; ++i) scanf("%d", &C[i]);
    for (int i = 0; i < N-1; ++i) {
        int weights = 0;
        if (C[i+1] == C[P[i]]) weights = 1;
        tree.connect(i+1, P[i], weights);
    }
    EulerTour<int> et(tree);
    
    cin >> Q;
    for (int q = 0; q < Q; ++q) {
        int type; cin >> type;
        if (type == 1) {
            int v; scanf("%d", &v); --v;
            if (v == 0) continue;
            int cur = et.bit[et.vf[v]];
            int add; if (cur == 0) add = 1; else add = -1;
            et.bit.add(et.vf[v], add);
            et.bit.add(et.ve[v]+1, -add);
        }
        else {
            int u, v; scanf("%d %d", &u, &v); --u, --v;
            int res = et.sum(u, v);
            if (res == 0) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
}

Submission Info

Submission Time
Task H - 白黒ツリー
User drken
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 6576 Byte
Status AC
Exec Time 362 ms
Memory 35404 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:191:61: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 0; i < N-1; ++i) scanf("%d", &P[i]), --P[i];
                                                             ^
./Main.cpp:192:51: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 0; i < N; ++i) scanf("%d", &C[i]);
                                                   ^
./Main.cpp:204:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
             int v; scanf("%d", &v); --v;
                                   ^
./Main.cpp:212:45: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
             int u, v; scanf("%d %d", &u, &v); --u, --v;
                                  ...

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 5 ms 7936 KB
00_sample_01 AC 5 ms 7936 KB
00_sample_02 AC 5 ms 7936 KB
00_sample_03 AC 5 ms 7936 KB
00_sample_04 AC 5 ms 7936 KB
01_graph_random_color_random_query_random_00 AC 56 ms 11904 KB
01_graph_random_color_random_query_random_01 AC 37 ms 11904 KB
01_graph_random_color_random_query_random_02 AC 24 ms 9472 KB
01_graph_random_color_random_query_random_03 AC 22 ms 10112 KB
01_graph_random_color_random_query_random_04 AC 38 ms 9856 KB
01_graph_random_color_random_query_random_05 AC 47 ms 9216 KB
01_graph_random_color_random_query_random_06 AC 49 ms 10624 KB
01_graph_random_color_random_query_random_07 AC 88 ms 25340 KB
01_graph_random_color_random_query_random_08 AC 71 ms 16200 KB
01_graph_random_color_random_query_random_09 AC 70 ms 21632 KB
02_graph_random_color_same_query_random_00 AC 47 ms 10496 KB
02_graph_random_color_same_query_random_01 AC 117 ms 25088 KB
02_graph_random_color_same_query_random_02 AC 33 ms 12288 KB
02_graph_random_color_same_query_random_03 AC 204 ms 10240 KB
02_graph_random_color_same_query_random_04 AC 89 ms 12928 KB
02_graph_random_color_same_query_random_05 AC 69 ms 16896 KB
02_graph_random_color_same_query_random_06 AC 25 ms 9472 KB
02_graph_random_color_same_query_random_07 AC 105 ms 12544 KB
02_graph_random_color_same_query_random_08 AC 32 ms 11264 KB
02_graph_random_color_same_query_random_09 AC 26 ms 9600 KB
03_graph_random_color_alternate_query_random_00 AC 20 ms 10112 KB
03_graph_random_color_alternate_query_random_01 AC 75 ms 24820 KB
03_graph_random_color_alternate_query_random_02 AC 24 ms 11648 KB
03_graph_random_color_alternate_query_random_03 AC 54 ms 13184 KB
03_graph_random_color_alternate_query_random_04 AC 40 ms 14080 KB
03_graph_random_color_alternate_query_random_05 AC 69 ms 23156 KB
03_graph_random_color_alternate_query_random_06 AC 95 ms 30796 KB
03_graph_random_color_alternate_query_random_07 AC 55 ms 11776 KB
03_graph_random_color_alternate_query_random_08 AC 102 ms 24192 KB
03_graph_random_color_alternate_query_random_09 AC 82 ms 17280 KB
10_graph_path_color_alternate_query_all2_00 AC 362 ms 35404 KB
10_graph_path_color_random_query_random_00 AC 319 ms 35276 KB
10_graph_path_color_random_query_random_01 AC 119 ms 35020 KB
10_graph_path_color_random_query_random_02 AC 112 ms 35020 KB
10_graph_path_color_random_query_random_03 AC 109 ms 35020 KB
10_graph_path_color_random_query_random_04 AC 108 ms 35020 KB
10_graph_path_color_random_query_random_05 AC 108 ms 35020 KB
10_graph_path_color_same_query_all2_00 AC 355 ms 35276 KB
10_graph_path_color_sametoAlternate_query_11...12_00 AC 103 ms 35020 KB
20_graph_star_color_alternate_query_all2_00 AC 346 ms 25964 KB
20_graph_star_color_random_query_1Center2random_00 AC 346 ms 25836 KB
20_graph_star_color_random_query_1Center2random_01 AC 322 ms 25836 KB
20_graph_star_color_random_query_1Center2random_02 AC 294 ms 25836 KB
20_graph_star_color_random_query_1Center2random_03 AC 274 ms 25836 KB
20_graph_star_color_random_query_1Center2random_04 AC 245 ms 25708 KB
20_graph_star_color_random_query_1Center2random_05 AC 221 ms 25708 KB
20_graph_star_color_random_query_1Center2random_06 AC 199 ms 25708 KB
20_graph_star_color_random_query_1Center2random_07 AC 175 ms 25708 KB
20_graph_star_color_random_query_1Center2random_08 AC 154 ms 25580 KB
20_graph_star_color_random_query_1Center2random_09 AC 121 ms 25580 KB
20_graph_star_color_random_query_1Center2random_10 AC 95 ms 25580 KB
20_graph_star_color_random_query_random_00 AC 248 ms 25708 KB
20_graph_star_color_random_query_random_01 AC 122 ms 25580 KB
20_graph_star_color_random_query_random_02 AC 104 ms 25580 KB
20_graph_star_color_random_query_random_03 AC 102 ms 25580 KB
20_graph_star_color_random_query_random_04 AC 100 ms 25580 KB
20_graph_star_color_random_query_random_05 AC 100 ms 25580 KB
20_graph_star_color_same_query_all2_00 AC 349 ms 25836 KB
20_graph_star_color_sametoAlternate_query_11...12_00 AC 97 ms 25580 KB
30_graph_hitode_color_random_query_random_00 AC 346 ms 28236 KB
30_graph_hitode_color_random_query_random_01 AC 136 ms 27980 KB
30_graph_hitode_color_random_query_random_02 AC 128 ms 27852 KB
30_graph_hitode_color_random_query_random_03 AC 125 ms 27852 KB
30_graph_hitode_color_random_query_random_04 AC 124 ms 27852 KB
30_graph_hitode_color_random_query_random_05 AC 124 ms 27852 KB
40_graph_caterpillar_color_random_query_random_00 AC 277 ms 25384 KB
40_graph_caterpillar_color_random_query_random_01 AC 121 ms 25308 KB
40_graph_caterpillar_color_random_query_random_02 AC 103 ms 25308 KB
40_graph_caterpillar_color_random_query_random_03 AC 103 ms 25308 KB
40_graph_caterpillar_color_random_query_random_04 AC 101 ms 25308 KB
40_graph_caterpillar_color_random_query_random_05 AC 101 ms 25308 KB
50_graph_binaryTree_color_random_query_random_00 AC 302 ms 26240 KB
50_graph_binaryTree_color_random_query_random_01 AC 142 ms 26240 KB
50_graph_binaryTree_color_random_query_random_02 AC 118 ms 26240 KB
50_graph_binaryTree_color_random_query_random_03 AC 116 ms 26240 KB
50_graph_binaryTree_color_random_query_random_04 AC 112 ms 26240 KB
50_graph_binaryTree_color_random_query_random_05 AC 113 ms 26240 KB
60_graph_ternaryTree_color_random_query_random_00 AC 140 ms 25472 KB
60_graph_ternaryTree_color_random_query_random_01 AC 121 ms 25472 KB
60_graph_ternaryTree_color_random_query_random_02 AC 112 ms 25472 KB
60_graph_ternaryTree_color_random_query_random_03 AC 111 ms 25472 KB
60_graph_ternaryTree_color_random_query_random_04 AC 112 ms 25472 KB
60_graph_ternaryTree_color_random_query_random_05 AC 111 ms 25472 KB
70_graph_triangle_color_random_query_random_00 AC 314 ms 25728 KB
70_graph_triangle_color_random_query_random_01 AC 119 ms 25728 KB
70_graph_triangle_color_random_query_random_02 AC 116 ms 25728 KB
70_graph_triangle_color_random_query_random_03 AC 116 ms 25728 KB
70_graph_triangle_color_random_query_random_04 AC 116 ms 25728 KB
70_graph_triangle_color_random_query_random_05 AC 115 ms 25728 KB