#include #include #include using namespace std; #define NM 100001 vector a[NM]; bitset visited; int tata[NM],dep[NM]; int N,M, root; void build_arb(int nod) { if (visited[nod]) { return; } visited[nod] = 1; for (vector::iterator it = a[nod].begin(); it != a[nod].end(); ++it) { if (visited[*it]) { continue; } tata[*it] = nod; dep[*it] = dep[nod] + 1; build_arb(*it); } } int get_lca(int x, int y) { //printf("dep: %d %d\n", dep[x], dep[y]); while (dep[x] < dep[y] && dep[y]) { y = tata[y]; } while (dep[y] < dep[x] && dep[x]) { x = tata[x]; } while (x != y) { //printf("tata: %d - %d %d - %d\n", x, tata[x], y, tata[y]); x = tata[x]; y = tata[y]; } //printf("rez: %d\n", x); return x; } int main() { //freopen("date.in","r",stdin); scanf("%d %d",&N,&M); int x,y,z; for (int i = 1; i < N; ++i) { scanf("%d %d",&x, &y); a[x].push_back(y); a[y].push_back(x); } root = 1; build_arb(root); while (M--) { scanf("%d",&z); if (z == 1) { scanf("%d", &root); tata[root] = 0; dep[root] = 0; visited.reset(); build_arb(root); } else { scanf("%d %d", &x, &y); int rez = get_lca(x,y); if (rez == x || rez == y) { rez = -1; } else if (rez == 0) { rez = root; } printf("%d\n", rez); } } return 0; }