sample Input 1
4 1
1 2
2 3
2 4
1 2
Sample Output 1
Road
Sample Input 2
5 2
1 2
2 3
3 4
4 5
1 3
1 5
Sample Output 2
Town
Town
Sample Input 3
9 9
2 3
5 6
4 8
8 9
4 5
3 4
1 9
3 7
7 9
2 5
2 6
4 6
2 4
5 8
7 8
3 6
5 6
Sample Output 3
Town
Road
Town
Town
Town
Town
Road
Road
Road
#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define ll long long int
#define endl '\n'
typedef pair<int, int> PII;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 7;
int Head[N], Edge[N], Next[N], idx;
void add(int a, int b) {
idx++;
Edge[idx] = b;
Next[idx] = Head[a];
Head[a] = idx;
}
int dis[N];
int vis[N];
void bfs(int s) {
mem(vis, 0);
dis[s] = 0;
queue<int> q;
q.push(s);
vis[s] = 1;
while (!q.empty()) {
int t = q.front();
q.pop();
for (int i = Head[t]; ~i; i = Next[i]) {
int e = Edge[i];
if (!vis[e]) {
q.push(e);
vis[e] = 1;
dis[e] = dis[t] + 1; //距离累加 前缀和
}
}
}
}
signed main() {
mem(Head, -1);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
add(u, v);
add(v, u);
}
bfs(1); //预处理1号节点到其他节点间的最短距离,是一个累加的过程,类似于前缀和
while (m--) {
int u, v;
cin >> u >> v;
if (abs(dis[u] - dis[v]) % 2 == 0) //两点间的最短距离为偶数,说明两点间有奇数个顶点
cout << "Town" << endl;
else
cout << "Road" << endl;
}
return 0;
}