Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code

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

vector<int> prefix_xor;

void dfs(vector<vector<int>> &adj, int src, int par, int xor_till_now) {
    prefix_xor[src] = xor_till_now;

    for (int child : adj[src]) {
        if (child != par) {
            dfs(adj, child, src, 1 ^ xor_till_now);
        }
    }
}

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

    prefix_xor.clear();
    prefix_xor.resize(n);

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

    dfs(adj, 0, -1, 0);

    for (int i = 0; i < q; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;

        int parity = prefix_xor[x] ^ prefix_xor[y];
        if (parity == 1) {
            cout << "Road"
                 << "\n";
        } else {
            cout << "Town"
                 << "\n";
        }
    }
}

int main() {
    solve();
    return 0;
}