#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define dbg(x) (cout<<#x<<" = "<<(x)<<'\n') #ifdef HOME const string inputFile = "input.txt"; const string outputFile = "output.txt"; #else const string problemName = ""; const string inputFile = problemName + ".in"; const string outputFile = problemName + ".out"; #endif typedef long long int lld; typedef pair PII; typedef pair PIL; typedef pair PLI; typedef pair PLL; const int INF = (1LL << 31) - 1; const lld LINF = (1LL << 62) - 1; const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1}; const int MOD = (int)(1e9) + 7; const int NMAX = 100000 + 5; const int MMAX = 100000 + 5; const int KMAX = 100000 + 5; const int PMAX = 100000 + 5; const int LMAX = 100000 + 5; const int VMAX = 100000 + 5; int N, M; vector V[NMAX]; int euler[2 * NMAX], cnt; int first[NMAX]; int lvl[NMAX]; PII RMQ[18][2 * NMAX]; bitset viz; void dfs(int x) { euler[++cnt] = x; first[x] = cnt; viz[x] = 1; for(auto y : V[x]) if(!viz[y]) { lvl[y] = lvl[x] + 1; dfs(y); euler[++cnt] = x; } } void build() { int i, j, p; for(i = 1; i <= cnt; i++) RMQ[0][i] = make_pair(lvl[euler[i]], euler[i]); for(i = 1, p = 2; p <= cnt; i++, p <<= 1) for(j = 1; j + p / 2 <= cnt; j++) RMQ[i][j] = min(RMQ[i - 1][j], RMQ[i - 1][j + p / 2]); } int lca(int x, int y) { x = first[x]; y = first[y]; if(x > y) swap(x, y); int d = (y - x + 1); int i = log2(d + 0.5); int p = (1 << i); return min(RMQ[i][x], RMQ[i][y - p + 1]).second; } int main() { int i, j, x, y, z, type; #ifndef ONLINE_JUDGE freopen(inputFile.c_str(), "r", stdin); freopen(outputFile.c_str(), "w", stdout); #endif scanf("%d%d", &N, &M); for(i = 1; i <= N - 1; i++) { scanf("%d%d", &x, &y); V[x].push_back(y); V[y].push_back(x); } /* doar brut :( */ dfs(1); build(); while(M--) { scanf("%d", &type); if(type == 1) { scanf("%d", &x); viz = 0; dfs(x); build(); } else { scanf("%d%d", &x, &y); z = lca(x, y); if(z == x || z == y) z = -1; printf("%d\n", z); } } return 0; }