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 (is_parent(x, y)) {
                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

*/
Copy
Escape from TarkZoo Baraa_Armoush
GNU G++17
31 ms
10.5 MB
Wrong Answer
Test Case #1
3 ms
5.1 MB
Accepted
Input
6 4
1 2 2 3 3
1 2
5 1
6 1
6 5
Output
yes
yes
yes
no
Judge Output
yes
yes
yes
no
Checker Message
4 tokens
Test Case #2
3 ms
5.2 MB
Accepted
Input
4 4
1 2 3
1 3
2 4
1 4
2 3
Output
yes
no
no
yes
Judge Output
yes
no
no
yes
Checker Message
4 tokens
Test Case #3
31 ms
10.5 MB
Wrong Answer
Input
43011 41896
1 2 3 4 3 4 3 6 9 7 6 12 12 4 13 8 10 18 14 20 15 15 23 16 24 17 24 5 12 13 19 18 30 23 19 25 35 29 37 24 15 29 25 28 29 24 40 42 47 32 18 44 42 46 53 54 54 15 50 53 48 62 49 39 26 34 59 57 30 65 33 40 58 65 48 35 23 69 35 33 75 60 25 62...
Output
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
y...
Judge Output
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
y...
Checker Message
17522nd words differ - expected: 'yes', found: 'no'