Source Code
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define sim template <class c
#define ris return *this
#define dor > debug& operator<<
#define eni(x) sim > typename enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) {
    return rge<c>{i, j};
}
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
    ~debug() {
        cerr << endl;
    }
    eni(!=) cerr << boolalpha << i;
    ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
    ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
}
#else
    sim dor(const c&) {
        ris;
    }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int K = 20;
const int N = 200200;

int h[N];
int deg[N];
int anc[N][K];
int best1[N];
int best2[N];
int best3[N];
vector<int> adj[N];

void dfs(int x = 1, int p = 0) {
    h[x] = h[p] + 1;
    best1[x] = (deg[x] > 2 ? x : best1[p]);
    best2[x] = best2[p];
    if (!best2[x] && deg[x] > 2) {
        best2[x] = x;
    }
    for (int y : adj[x]) {
        dfs(y, x);
    }
    if (adj[x].size() == 1) {
        best3[x] = best3[adj[x][0]];
    } else if (adj[x].size() > 1) {
        best3[x] = x;
    }
}

int lca(int x, int y) {
    if (h[x] < h[y]) {
        swap(x, y);
    }

    for (int j = K - 1; ~j; --j) {
        if (h[x] - (1 << j) >= h[y]) {
            x = anc[x][j];
        }
    }

    for (int j = K - 1; ~j; --j) {
        if (anc[x][j] != anc[y][j]) {
            x = anc[x][j];
            y = anc[y][j];
        }
    }

    if (x != y) {
        x = anc[x][0];
        y = anc[y][0];
    }

    return assert(x == y), x;
}

int dist(int x, int y) {
    return h[x] + h[y] - 2 * h[lca(x, y)];
}

bool is_parent(int x, int p) {
    return lca(x, p) == p;
}

int main() {
    int n, q;
    cin >> n >> q;

    for (int x = 2; x <= n; x++) {
        scanf("%d", &anc[x][0]);
        deg[x]++;
        deg[anc[x][0]]++;
        adj[anc[x][0]].push_back(x);
    }

    dfs();

    for (int j = 1; j < K; j++) {
        for (int x = 1; x <= n; x++) {
            anc[x][j] = anc[anc[x][j - 1]][j - 1];
        }
    }

    while (q--) {
        int x, y;
        scanf("%d%d", &x, &y);

        if (x == y) {
            puts("yes");
            continue;
        }

        bool leaf = (adj[y].empty());

        if (!leaf) {
            if (y == 1) {
                int b = best3[y];
                if (b) {
                    puts(dist(y, b) < dist(x, b) ? "yes" : "no");
                } else {
                    puts("no");
                }
            } else {
                puts("yes");
            }
        } else {
            int b = best1[y];
            if (b) {
                puts(dist(y, b) < dist(x, b) ? "yes" : "no");
            } else {
                b = best2[x];
                if (b) {
                    puts(dist(y, b) < dist(x, b) ? "yes" : "no");
                } else {
                    puts("no");
                }
            }
        }
    }
}

/*
6 100
1 2 2 3 3

4 1
1 2 3
4 2

*/
Copy
Escape from TarkZoo Baraa_Armoush
GNU G++17
69 ms
22.7 MB
Accepted