Crazy country
Difficulty: Hard

<p>
In some place, there is a country which is formed by N cities connected by N-1 roads.  There is just one way in going from one city to the other, it is by using the shortest path. The president is crazy, he continuously changed the country's capital. So the people are getting into trouble. Now the people want your help. They travel a lot, so
they are interested to pass by the closest city to the capital if they travel from one city to other. They always take the shortest path. Currently the capital's city is the number 1.
</p>
<br>Input format</br>
<br>
The first line of the input contains one integer N (1 ≤ N ≤ 100000)the number of cities. Next N−1 lines describe a road. Each of these lines contains a pair of integers ai, bi (1 ≤ ai,bi ≤ N) specifying the numbers of the cities connected by corresponding road, each road has the same length. The next line has an integer 
Q (1 ≤ Q ≤ 100000) representing the number of queries. The Q queries follow on a single line each. There are two type of query: type '1' and type '2' (quotes for clarity only). In the case of a '1' query, it will be in the form '1 X', where X is the new capital elected, (1≤X≤N). In the case of a '2' query, it will be in the form '2 X Y', (1 ≤ X, Y ≤ N), where X and Y are two cities.
</br>

<br>Output format</br>
<br>
For every '2' query output one line containing the closer city to the capital in the path between the two cities provided.
</br>

<br>Sample input</br>
<br>10
<br>1 8
<br>8 9
<br>9 4
<br>4 2
<br>9 5
<br>9 6
<br>6 7
<br>6 10
<br>10 3
<br>6
<br>1 5
<br>2 10 1
<br>1 4
<br>1 8
<br>1 10
<br>2 7 10
</br>

<br>Sample output</br>
<br>9
<br>10

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
<pre>
#include <cstdio>
#include <iostream>

#include <cstring>
#include <string>

#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <cctype>
#include <climits>
#include <complex>
#include <set>
#include <stack>
#include <map>
using namespace std;

struct node {
	int val;
	bool rev;
	node *l, *r, *p;

	node(){}

	node(int val) {
		this->val = val;
		l = r = p = NULL;
	}

	bool root() {
		return (!p || (p->l != this && p->r != this));
	}

	void propagate() {
		if(rev) {
			rev = 0;
			swap(l, r);
			if(l) l->rev ^= 1;
			if(r) r->rev ^= 1;
		}
	}
};

void zig(node *x) {
	node *p = x->p;
	node *q = p->p;
	if( (p->l = x->r) ) p->l->p = p;
	x->r = p, p->p = x;
	if( (x->p = q) ) {
		if(q->r == p) q->r = x;
		if(q->l == p) q->l = x;
	}
}

void zag(node *x) {
	node *p = x->p;
	node *q = p->p;
	if( (p->r = x->l) ) p->r->p = p;
	x->l = p, p->p = x;
	if( (x->p = q) ) {
		if(q->r == p) q->r = x;
		if(q->l == p) q->l = x;
	}
}

void push_down(node *x) {
	if(!x->root()) push_down(x->p);
	x->propagate();
}

void splay(node *x) {
	push_down(x);
	while(!x->root()) {
		node *p = x->p;
		if(p->root()) {
			if(p->l == x) zig(x); else
				zag(x);
		} else {
			node *q = p->p;
			if(p->l == x) {
				if(q->l == p) zig(p), zig(x); else
					zig(x), zag(x);
			} else {
				if(q->l == p) zag(x), zig(x); else
					zag(p), zag(x);
			}
		}
	}
}

void expose(node *x) {
	node *r = NULL;
	for(node *p=x;p;p=p->p) {
		splay(p);
		p->r = r;
		r = p;
	}
}

void evert(node *x) {
	expose(x), splay(x);
	x->rev ^= 1;
}

void link(node *x, node *y) {
	evert(x);
	x->p = y;
}

int lca(node *x, node *y) {
	expose(x);
	splay(x);

	node *p = y;
	for(;p;p=p->p) {
		splay(p);
		if(!p->p) break;
	}
	return p->val;
}

int op;
int n, q, a, b;
node *tree[100005];

int main(){
	scanf("%d", &n);
	for(int i = 1;i<=n;i++) {
		tree[i] = new node( i );
	}

	for(int i = 1;i<n;i++) {
		int u,v;
		scanf("%d%d", &u, &v);
		link(tree[u], tree[v]);
	}

	scanf("%d", &q);

	evert(tree[1]);
	while(q--) {
		scanf("%d", &op);
		if(op == 1) {
			scanf("%d", &a);
			evert(tree[a]);
		} else {
			scanf("%d%d", &a, &b);
			printf("%d\n", lca(tree[a], tree[b]));
		}
	}
	return 0;
}
</pre>