Untitled

Anonymous
C++
10/30/2025 4:38 AM
5.3 KB
16
Indexable
Never
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#ifdef TOWRIST
    #define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__);
#else
    #define debug(...);
#endif

template <typename T> std::ostream &operator<<(std::ostream &stream, const vector<T> &vec) {for (size_t i = 0; i < vec.size(); i++) { stream << vec[i]; if (i != vec.size() - 1) stream << ' '; }; return stream; } template <typename T> std::istream &operator>>(std::istream &stream, vector<T> &vec) {for (T &x : vec) stream >> x; return stream; } template <typename T, typename U> std::ostream &operator<<(std::ostream &stream, const pair<T, U> &pr) {stream << pr.first << ' ' << pr.second; return stream; } template <typename T, typename U> std::istream &operator>>(std::istream &stream, pair<T, U> &pr) {stream >> pr.first >> pr.second; return stream; } template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(char c) {string s; s += c; return s; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "1" : "0"); } string to_string(vector<bool> v) {bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) {string res = ""; for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) {if (!first) {res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cout << " " << to_string(H); debug_out(T...); }

#define int             long long
#define all(x)          x.begin(), x.end()
#define double          long double
#define endl            '\n'
#define ff              first
#define ss              second
#define v               vector
#define pb              push_back
#define bit(num, i)     (num & (1ll << i))

using pii = pair<int, int>;
const bool multipleTestCases = false;

const int N = 2e5 + 10, K = 30;
int depth[N], ancestor[N][K];

v<int> adj[N];

void pre(int node = 1, int parent = 0)
{   
    ancestor[node][0] = parent;
    
    for(int i = 1; i < K; ++i)
    {
        int mid_node = ancestor[node][i - 1];
        ancestor[node][i] = ancestor[mid_node][i - 1];
    }

    for(int nb : adj[node])
    {
        if(nb == parent) continue;
        depth[nb] = 1 + depth[node];
        pre(nb, node);
    }
}

int get_ancestor_in_the_original_tree(int node, int k)
{
    for(int i = K - 1; i >= 0; --i)
    {
        if(k & (1 << i))
        {
            node = ancestor[node][i];
        }
    }

    return node;
}

int get_lca_in_the_oringal_tree(int x, int y)
{
    if(depth[x] > depth[y])
    {
        swap(x, y);
    }

    y = get_ancestor_in_the_original_tree(y, depth[y] - depth[x]);

    if(x == y)
    {
        return x;
    }

    for(int i = K - 1; i >= 0; --i)
    {
        if(ancestor[x][i] != ancestor[y][i])
        {
            x = ancestor[x][i];
            y = ancestor[y][i];
        }
    }

    return ancestor[y][0];
}

int get_depth(int x, int r)
{
    int lca = get_lca_in_the_oringal_tree(x, r);
    return depth[x] + depth[r] - 2 * depth[lca];
}   

int get_lca(int x, int y, int r) 
{
    int a = get_lca_in_the_oringal_tree(x, y);
    int b = get_lca_in_the_oringal_tree(x, r);
    int c = get_lca_in_the_oringal_tree(y, r);

    return a ^ b ^ c;
}

void solve()
{
    int n, q; cin >> n >> q;

    for(int i = 1; i < n; ++i)
    {
        int u, v; cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    pre();

    while(q--)
    {
        int r, x, y; cin >> r >> x >> y;
        
        int depth_x = get_depth(x, r);
        int depth_y = get_depth(y, r);

        if(depth_x > depth_y)
        {
            swap(x, y);
            swap(depth_x, depth_y);
        }

        int lca = get_lca(x, y, r);
        int depth_lca = get_depth(lca, r);

        cout << ((lca == x or depth_lca == depth_x - 1) ? "YES" : "NO") << endl;
    }
}     

signed main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int t = 1;
    multipleTestCases and cin >> t;
    for (int i = 1; i <= t; i++)
    {
        // cout << "Case #" << i << ": ";
        solve();
    }
}
Editor is loading...
Leave a Comment