Submission #2966778


Source Code Expand

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



#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }




// 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); }
    const inline vector<Edge<VAL> > operator [] (int i) {return list[i];}
    const 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 (int i = 0; i < (int)tree[v].size(); ++i) {
            Edge<VAL> e = tree[v][i];
            if (e.to != par) {
                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;
            }
        }
        /*
        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);
    //et.print();
    
    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);
            
            /*
            COUT(et.vf[v]);
            COUT(cur);
            COUT(add);
            et.bit.print();
            */
        }
        else {
            int u, v; scanf("%d %d", &u, &v); --u, --v;
            int res = et.sum(u, v);
            
            /*
            COUT(res);
            int lca = et.LCA(u, v);
            COUT(lca);
            COUT(et.vf[u]);
            COUT(et.vf[v]);
            COUT(et.bit.sum(et.vf[u]));
            COUT(et.bit.sum(et.vf[v]));
            COUT(et.bit.sum(et.vf[lca]));
            et.bit.print();
            */
            
            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 0
Code Size 8595 Byte
Status TLE
Exec Time 5257 ms
Memory 31052 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:226: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:227: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:240: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:255: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 0 / 1000
Status
AC × 5
AC × 67
TLE × 27
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 59 ms 11904 KB
01_graph_random_color_random_query_random_01 AC 63 ms 11904 KB
01_graph_random_color_random_query_random_02 AC 56 ms 9472 KB
01_graph_random_color_random_query_random_03 AC 24 ms 9984 KB
01_graph_random_color_random_query_random_04 AC 39 ms 9600 KB
01_graph_random_color_random_query_random_05 AC 48 ms 9216 KB
01_graph_random_color_random_query_random_06 AC 51 ms 10624 KB
01_graph_random_color_random_query_random_07 AC 625 ms 25340 KB
01_graph_random_color_random_query_random_08 AC 2371 ms 15992 KB
01_graph_random_color_random_query_random_09 AC 93 ms 21632 KB
02_graph_random_color_same_query_random_00 AC 50 ms 10496 KB
02_graph_random_color_same_query_random_01 AC 233 ms 25088 KB
02_graph_random_color_same_query_random_02 AC 37 ms 12160 KB
02_graph_random_color_same_query_random_03 AC 198 ms 9984 KB
02_graph_random_color_same_query_random_04 AC 93 ms 12928 KB
02_graph_random_color_same_query_random_05 AC 77 ms 16896 KB
02_graph_random_color_same_query_random_06 AC 30 ms 9472 KB
02_graph_random_color_same_query_random_07 AC 107 ms 12160 KB
02_graph_random_color_same_query_random_08 AC 34 ms 11264 KB
02_graph_random_color_same_query_random_09 AC 28 ms 9600 KB
03_graph_random_color_alternate_query_random_00 AC 22 ms 10112 KB
03_graph_random_color_alternate_query_random_01 TLE 5257 ms 24820 KB
03_graph_random_color_alternate_query_random_02 AC 27 ms 11648 KB
03_graph_random_color_alternate_query_random_03 AC 77 ms 13184 KB
03_graph_random_color_alternate_query_random_04 AC 190 ms 14080 KB
03_graph_random_color_alternate_query_random_05 AC 2194 ms 23156 KB
03_graph_random_color_alternate_query_random_06 AC 99 ms 27852 KB
03_graph_random_color_alternate_query_random_07 AC 59 ms 11776 KB
03_graph_random_color_alternate_query_random_08 AC 108 ms 24192 KB
03_graph_random_color_alternate_query_random_09 AC 89 ms 17280 KB
10_graph_path_color_alternate_query_all2_00 AC 372 ms 31052 KB
10_graph_path_color_random_query_random_00 AC 321 ms 30796 KB
10_graph_path_color_random_query_random_01 AC 124 ms 30668 KB
10_graph_path_color_random_query_random_02 AC 115 ms 30668 KB
10_graph_path_color_random_query_random_03 AC 114 ms 30668 KB
10_graph_path_color_random_query_random_04 AC 115 ms 30668 KB
10_graph_path_color_random_query_random_05 AC 114 ms 30668 KB
10_graph_path_color_same_query_all2_00 AC 358 ms 30924 KB
10_graph_path_color_sametoAlternate_query_11...12_00 AC 109 ms 30668 KB
20_graph_star_color_alternate_query_all2_00 TLE 5257 ms 25580 KB
20_graph_star_color_random_query_1Center2random_00 TLE 5256 ms 27628 KB
20_graph_star_color_random_query_1Center2random_01 TLE 5257 ms 25580 KB
20_graph_star_color_random_query_1Center2random_02 TLE 5257 ms 25580 KB
20_graph_star_color_random_query_1Center2random_03 TLE 5257 ms 25580 KB
20_graph_star_color_random_query_1Center2random_04 TLE 5256 ms 27628 KB
20_graph_star_color_random_query_1Center2random_05 TLE 5257 ms 25580 KB
20_graph_star_color_random_query_1Center2random_06 TLE 5257 ms 25580 KB
20_graph_star_color_random_query_1Center2random_07 TLE 5256 ms 27700 KB
20_graph_star_color_random_query_1Center2random_08 TLE 5256 ms 25580 KB
20_graph_star_color_random_query_1Center2random_09 TLE 5257 ms 25580 KB
20_graph_star_color_random_query_1Center2random_10 TLE 5256 ms 25580 KB
20_graph_star_color_random_query_random_00 TLE 5256 ms 27628 KB
20_graph_star_color_random_query_random_01 TLE 5257 ms 25580 KB
20_graph_star_color_random_query_random_02 TLE 5257 ms 25580 KB
20_graph_star_color_random_query_random_03 TLE 5256 ms 27628 KB
20_graph_star_color_random_query_random_04 TLE 5257 ms 25580 KB
20_graph_star_color_random_query_random_05 TLE 5257 ms 25580 KB
20_graph_star_color_same_query_all2_00 TLE 5257 ms 25580 KB
20_graph_star_color_sametoAlternate_query_11...12_00 TLE 5256 ms 27628 KB
30_graph_hitode_color_random_query_random_00 AC 365 ms 26700 KB
30_graph_hitode_color_random_query_random_01 AC 147 ms 26444 KB
30_graph_hitode_color_random_query_random_02 AC 139 ms 26444 KB
30_graph_hitode_color_random_query_random_03 AC 142 ms 26444 KB
30_graph_hitode_color_random_query_random_04 AC 139 ms 26444 KB
30_graph_hitode_color_random_query_random_05 AC 150 ms 26444 KB
40_graph_caterpillar_color_random_query_random_00 TLE 5256 ms 25896 KB
40_graph_caterpillar_color_random_query_random_01 TLE 5257 ms 25308 KB
40_graph_caterpillar_color_random_query_random_02 TLE 5257 ms 25308 KB
40_graph_caterpillar_color_random_query_random_03 TLE 5257 ms 25308 KB
40_graph_caterpillar_color_random_query_random_04 TLE 5256 ms 25944 KB
40_graph_caterpillar_color_random_query_random_05 TLE 5257 ms 25308 KB
50_graph_binaryTree_color_random_query_random_00 AC 312 ms 26240 KB
50_graph_binaryTree_color_random_query_random_01 AC 157 ms 26240 KB
50_graph_binaryTree_color_random_query_random_02 AC 121 ms 26240 KB
50_graph_binaryTree_color_random_query_random_03 AC 124 ms 26240 KB
50_graph_binaryTree_color_random_query_random_04 AC 122 ms 26240 KB
50_graph_binaryTree_color_random_query_random_05 AC 121 ms 26240 KB
60_graph_ternaryTree_color_random_query_random_00 AC 153 ms 25472 KB
60_graph_ternaryTree_color_random_query_random_01 AC 133 ms 25472 KB
60_graph_ternaryTree_color_random_query_random_02 AC 123 ms 25472 KB
60_graph_ternaryTree_color_random_query_random_03 AC 121 ms 25472 KB
60_graph_ternaryTree_color_random_query_random_04 AC 127 ms 25472 KB
60_graph_ternaryTree_color_random_query_random_05 AC 123 ms 25472 KB
70_graph_triangle_color_random_query_random_00 AC 338 ms 25728 KB
70_graph_triangle_color_random_query_random_01 AC 130 ms 25728 KB
70_graph_triangle_color_random_query_random_02 AC 130 ms 25728 KB
70_graph_triangle_color_random_query_random_03 AC 127 ms 25728 KB
70_graph_triangle_color_random_query_random_04 AC 126 ms 25728 KB
70_graph_triangle_color_random_query_random_05 AC 125 ms 25728 KB