#include #include #include using namespace std; int h[10001]; int parent[10001]; int n, m; list adiac[10001]; list::iterator it; void reMake(int root) { queue q; int i; parent[root] = -1; for (i = 1; i <= n; ++i) { h[i] = 0; } q.push (root); { i = q.front(); q.pop(); for (it = adiac[i].begin(); it != adiac[i].end(); ++it) { if (!h[*it]) { h[*it] = h[i] + 1; parent[*it] = i; q.push(*it); } } } } int ask(int x, int y) { while (x != y) { if (h[x] > h[y]) { x = parent[x]; } else { y = parent[y]; } } } int main() { cin >> n >> m; int i, j, k; for (i = 0; i < n; ++i) { cin >> j >> k; adiac[j].push_front(k); adiac[k].push_front(j); } reMake(1); while (m--) { cin >> i >> j; if (i == 1) reMake(j); else { cin >> k; i = ask(j, k); if (i == j || i == k) { cout << parent[i] << '\n'; } else cout << i << '\n'; } } return 0; }