sss

· · 个人记录

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

const int MAXN = 1e5 + 5;
const int LOG = 20;

vector<int> tree[MAXN];
int depth[MAXN];
int up[MAXN][LOG];

void dfs(int u, int parent) {
    up[u][0] = parent;
    for (int i = 1; i < LOG; ++i) {
        up[u][i] = up[up[u][i-1]][i-1];
    }
    for (int v : tree[u]) {
        if (v != parent) {
            depth[v] = depth[u] + 1;
            dfs(v, u);
        }
    }
}

int lca(int u, int v) {
    if (depth[u] < depth[v]) swap(u, v);
    for (int i = LOG - 1; i >= 0; --i) {
        if (depth[u] - (1 << i) >= depth[v]) {
            u = up[u][i];
        }
    }
    if (u == v) return u;
    for (int i = LOG - 1; i >= 0; --i) {
        if (up[u][i] != up[v][i]) {
            u = up[u][i];
            v = up[v][i];
        }
    }
    return up[u][0];
}

int distance(int u, int v) {
    int ancestor = lca(u, v);
    return depth[u] + depth[v] - 2 * depth[ancestor];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n;
    cin >> n;
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        tree[u].push_back(v);
        tree[v].push_back(u);
    }

    dfs(1, 1);

    int q;
    cin >> q;
    while (q--) {
        int x, y;
        cin >> x >> y;
        int dist = distance(x, y);
        if (dist % 2 == 0) {
            cout << "Meet at the intersection!" << '\n';
        } else {
            cout << "Meeting on the road!" << '\n';
        }
    }

    return 0;
}