/*
LCA Tarjan --- POJ 1330.
[调用方法]树存G(有向or无向).询问存进Q(无向).然后对树根root调用LCA(root)即可得出所有询问的答案.第i的询问对应第2i-1和2i-2条询问边.
[注意]这里我为了方便把G和Q的结构放一块儿了.有时图的结果可能需要复杂一些,则要把G和Q分开(或者不分开,把两个人需要的属性都一起放起来,可以减少代码量,但会增加空间)
*/
const int MAXV = 10005;
const int MAXE = 10005;
struct Gragh{
struct Gragh_Node{
int u, v;
int opp;
int next;
int res; //存Query的结果
}arc[MAXE];
int cnt, head[MAXV];
void init(){
cnt = 0;
memset(head, -1, sizeof(head));
}
void d_insert(int u, int v){ //有向边
arc[cnt].u = u;
arc[cnt].v = v;
arc[cnt].res = 0;
arc[cnt].next = head[u];
head[u] = cnt ++;
}
void u_insert(int u, int v){ //无向边
arc[cnt].u = u;
arc[cnt].v = v;
arc[cnt].res = 0;
arc[cnt].next = head[u];
arc[cnt].opp = cnt + 1;
head[u] = cnt ++;
arc[cnt].u = v;
arc[cnt].v = u;
arc[cnt].res = 0;
arc[cnt].next = head[v];
arc[cnt].opp = cnt - 1;
head[v] = cnt ++;
}
};
struct Disjoint_Sets{
struct Sets{
int father, ranks;
}S[MAXV];
void Init(int n){
for (int i = 0; i <= n; i ++)
S[i].father = i, S[i].ranks = 0;
}
int Father(int x){
if (S[x].father == x){
return x;
}
else{
S[x].father = Father(S[x].father); //Path compression
return S[x].father;
}
}
bool Union(int x, int y){
int fx = Father(x), fy = Father(y);
if (fx == fy){
return false;
}
else{ //Rank merge
if (S[fx].ranks > S[fy].ranks){
S[fy].father = fx;
}
else{
S[fx].father = fy;
if (S[fx].ranks == S[fy].ranks){
++ S[fy].ranks;
}
}
return true;
}
}
};
struct LCA{
Gragh G, Q; //G存图(树); Q存查询,可以把每个询问当成一个无向边存储
Disjoint_Sets DS;
int ancestor[MAXV];
int indegree[MAXV];
bool vis[MAXV];
void init(int n){
memset(ancestor,0,sizeof(ancestor));
memset(vis,0,sizeof(vis));
G.init();
Q.init();
DS.Init(n);
}
void insert_gragh(int u, int v){
G.d_insert(u, v);
}
void insert_query(int u, int v){
Q.u_insert(u, v);
}
void lca(int u){
ancestor[u] = u;
for (int i = G.head[u]; i != -1; i = G.arc[i].next){
int v = G.arc[i].v;
lca(v);
DS.Union(u, v);
ancestor[DS.Father(u)] = u;
}
vis[u] = 1;
for (int i = Q.head[u]; i != -1; i = Q.arc[i].next){
int v = Q.arc[i].v;
if (vis[v])
Q.arc[i].res = ancestor[DS.Father(v)];
Q.arc[Q.arc[i].opp].res = ancestor[DS.Father(v)];
}
}
void solve(int n){
memset(vis, 0, sizeof(vis));
memset(indegree, 0, sizeof(indegree));
for (int i = 0; i < G.cnt; i ++){
indegree[G.arc[i].v] ++;
}
for (int i = 1; i <= n; i ++){
if (indegree[i] == 0)
lca(i);
}
}
}L;
int main(){
//freopen("data.txt","r+",stdin);
int t, n;
scanf("%d",&t);
while(t --){
scanf("%d",&n);
L.init(n);
for (int i = 1; i < n; i ++){
int u,v;
scanf("%d %d",&u,&v);
L.insert_gragh(u, v);
}
int u,v;
scanf("%d %d",&u,&v);
L.insert_query(u, v);
L.solve(n);
printf("%d\n", L.Q.arc[1].res); //第i个询问对应第2i-1条Query边
}
return 0;
}
LyoKCUxDQSBUYXJqYW4gLS0tIFBPSiAxMzMwLgoJW+iwg+eUqOaWueazlV3moJHlrZhHKOacieWQkW9y5peg5ZCRKS7or6Lpl67lrZjov5tRKOaXoOWQkSku54S25ZCO5a+55qCR5qC5cm9vdOiwg+eUqExDQShyb290KeWNs+WPr+W+l+WHuuaJgOacieivoumXrueahOetlOahiC7nrKxp55qE6K+i6Zeu5a+55bqU56ysMmktMeWSjDJpLTLmnaHor6Lpl67ovrkuCglb5rOo5oSPXei/memHjOaIkeS4uuS6huaWueS+v+aKikflkoxR55qE57uT5p6E5pS+5LiA5Z2X5YS/5LqGLuacieaXtuWbvueahOe7k+aenOWPr+iDvemcgOimgeWkjeadguS4gOS6myzliJnopoHmiopH5ZKMUeWIhuW8gCjmiJbogIXkuI3liIblvIAs5oqK5Lik5Liq5Lq66ZyA6KaB55qE5bGe5oCn6YO95LiA6LW35pS+6LW35p2lLOWPr+S7peWHj+WwkeS7o+eggemHjyzkvYbkvJrlop7liqDnqbrpl7QpCiovCmNvbnN0IGludCBNQVhWID0gMTAwMDU7CmNvbnN0IGludCBNQVhFID0gMTAwMDU7CnN0cnVjdCBHcmFnaHsKICAgIHN0cnVjdCBHcmFnaF9Ob2RlewogICAgICAgIGludCB1LCB2OwogICAgICAgIGludCBvcHA7CiAgICAgICAgaW50IG5leHQ7CiAgICAgICAgaW50IHJlczsgICAgICAgIC8v5a2YUXVlcnnnmoTnu5PmnpwKICAgIH1hcmNbTUFYRV07CiAgICBpbnQgY250LCBoZWFkW01BWFZdOwogICAgdm9pZCBpbml0KCl7CiAgICAgICAgY250ID0gMDsKICAgICAgICBtZW1zZXQoaGVhZCwgLTEsIHNpemVvZihoZWFkKSk7CiAgICB9CiAgICB2b2lkIGRfaW5zZXJ0KGludCB1LCBpbnQgdil7CQkvL+acieWQkei+uQogICAgICAgIGFyY1tjbnRdLnUgPSB1OwogICAgICAgIGFyY1tjbnRdLnYgPSB2OwogICAgICAgIGFyY1tjbnRdLnJlcyA9IDA7CiAgICAgICAgYXJjW2NudF0ubmV4dCA9IGhlYWRbdV07CiAgICAgICAgaGVhZFt1XSA9IGNudCArKzsKICAgIH0KICAgIHZvaWQgdV9pbnNlcnQoaW50IHUsIGludCB2KXsJCS8v5peg5ZCR6L65CiAgICAgICAgYXJjW2NudF0udSA9IHU7CiAgICAgICAgYXJjW2NudF0udiA9IHY7CiAgICAgICAgYXJjW2NudF0ucmVzID0gMDsKICAgICAgICBhcmNbY250XS5uZXh0ID0gaGVhZFt1XTsKICAgICAgICBhcmNbY250XS5vcHAgPSBjbnQgKyAxOwogICAgICAgIGhlYWRbdV0gPSBjbnQgKys7CiAgICAgICAgYXJjW2NudF0udSA9IHY7CiAgICAgICAgYXJjW2NudF0udiA9IHU7CiAgICAgICAgYXJjW2NudF0ucmVzID0gMDsKICAgICAgICBhcmNbY250XS5uZXh0ID0gaGVhZFt2XTsKICAgICAgICBhcmNbY250XS5vcHAgPSBjbnQgLSAxOwogICAgICAgIGhlYWRbdl0gPSBjbnQgKys7CiAgICB9Cn07CnN0cnVjdCBEaXNqb2ludF9TZXRzewogICAgc3RydWN0IFNldHN7CiAgICAgICAgaW50IGZhdGhlciwgcmFua3M7CiAgICB9U1tNQVhWXTsKICAgIHZvaWQgSW5pdChpbnQgbil7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gbjsgaSArKykKICAgICAgICAgICAgU1tpXS5mYXRoZXIgPSBpLCBTW2ldLnJhbmtzID0gMDsKICAgIH0KICAgIGludCBGYXRoZXIoaW50IHgpewogICAgICAgIGlmIChTW3hdLmZhdGhlciA9PSB4KXsKICAgICAgICAgICAgcmV0dXJuIHg7CiAgICAgICAgfQogICAgICAgIGVsc2V7CiAgICAgICAgICAgIFNbeF0uZmF0aGVyID0gRmF0aGVyKFNbeF0uZmF0aGVyKTsgICAgIAkJLy9QYXRoIGNvbXByZXNzaW9uCiAgICAgICAgICAgIHJldHVybiBTW3hdLmZhdGhlcjsKICAgICAgICB9CiAgICB9CiAgICBib29sIFVuaW9uKGludCB4LCBpbnQgeSl7CiAgICAgICAgaW50IGZ4ID0gRmF0aGVyKHgpLCBmeSA9IEZhdGhlcih5KTsKICAgICAgICBpZiAoZnggPT0gZnkpewogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgICAgIGVsc2V7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vUmFuayBtZXJnZQogICAgICAgICAgICBpZiAoU1tmeF0ucmFua3MgPiBTW2Z5XS5yYW5rcyl7CiAgICAgICAgICAgICAgICBTW2Z5XS5mYXRoZXIgPSBmeDsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgU1tmeF0uZmF0aGVyID0gZnk7CiAgICAgICAgICAgICAgICBpZiAoU1tmeF0ucmFua3MgPT0gU1tmeV0ucmFua3MpewogICAgICAgICAgICAgICAgICAgICsrIFNbZnldLnJhbmtzOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIH0KICAgIH0KfTsKc3RydWN0IExDQXsKICAgIEdyYWdoIEcsIFE7ICAgICAgICAgLy9H5a2Y5Zu+KOagkSk7IFHlrZjmn6Xor6Is5Y+v5Lul5oqK5q+P5Liq6K+i6Zeu5b2T5oiQ5LiA5Liq5peg5ZCR6L655a2Y5YKoCiAgICBEaXNqb2ludF9TZXRzIERTOwogICAgaW50IGFuY2VzdG9yW01BWFZdOwogICAgaW50IGluZGVncmVlW01BWFZdOwogICAgYm9vbCB2aXNbTUFYVl07CiAgICB2b2lkIGluaXQoaW50IG4pewogICAgICAgIG1lbXNldChhbmNlc3RvciwwLHNpemVvZihhbmNlc3RvcikpOwogICAgICAgIG1lbXNldCh2aXMsMCxzaXplb2YodmlzKSk7CiAgICAgICAgRy5pbml0KCk7CiAgICAgICAgUS5pbml0KCk7CiAgICAgICAgRFMuSW5pdChuKTsKICAgIH0KICAgIHZvaWQgaW5zZXJ0X2dyYWdoKGludCB1LCBpbnQgdil7CiAgICAgICAgRy5kX2luc2VydCh1LCB2KTsKICAgIH0KICAgIHZvaWQgaW5zZXJ0X3F1ZXJ5KGludCB1LCBpbnQgdil7CiAgICAgICAgUS51X2luc2VydCh1LCB2KTsKICAgIH0KICAgIHZvaWQgbGNhKGludCB1KXsKICAgICAgICBhbmNlc3Rvclt1XSA9IHU7CiAgICAgICAgZm9yIChpbnQgaSA9IEcuaGVhZFt1XTsgaSAhPSAtMTsgaSA9IEcuYXJjW2ldLm5leHQpewogICAgICAgICAgICBpbnQgdiA9IEcuYXJjW2ldLnY7CiAgICAgICAgICAgIGxjYSh2KTsKICAgICAgICAgICAgRFMuVW5pb24odSwgdik7CiAgICAgICAgICAgIGFuY2VzdG9yW0RTLkZhdGhlcih1KV0gPSB1OwogICAgICAgIH0KICAgICAgICB2aXNbdV0gPSAxOwogICAgICAgIGZvciAoaW50IGkgPSBRLmhlYWRbdV07IGkgIT0gLTE7IGkgPSBRLmFyY1tpXS5uZXh0KXsKICAgICAgICAgICAgaW50IHYgPSBRLmFyY1tpXS52OwogICAgICAgICAgICBpZiAodmlzW3ZdKQogICAgICAgICAgICAgICAgUS5hcmNbaV0ucmVzID0gYW5jZXN0b3JbRFMuRmF0aGVyKHYpXTsKICAgICAgICAgICAgICAgIFEuYXJjW1EuYXJjW2ldLm9wcF0ucmVzID0gYW5jZXN0b3JbRFMuRmF0aGVyKHYpXTsKICAgICAgICB9CiAgICB9CiAgICB2b2lkIHNvbHZlKGludCBuKXsKICAgICAgICBtZW1zZXQodmlzLCAwLCBzaXplb2YodmlzKSk7CiAgICAgICAgbWVtc2V0KGluZGVncmVlLCAwLCBzaXplb2YoaW5kZWdyZWUpKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IEcuY250OyBpICsrKXsKICAgICAgICAgICAgaW5kZWdyZWVbRy5hcmNbaV0udl0gKys7CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkgKyspewogICAgICAgICAgICBpZiAoaW5kZWdyZWVbaV0gPT0gMCkKICAgICAgICAgICAgICAgIGxjYShpKTsKICAgICAgICB9CiAgICB9Cn1MOwoKaW50IG1haW4oKXsKICAgIC8vZnJlb3BlbigiZGF0YS50eHQiLCJyKyIsc3RkaW4pOwogICAgaW50IHQsIG47CiAgICBzY2FuZigiJWQiLCZ0KTsKICAgIHdoaWxlKHQgLS0pewogICAgICAgIHNjYW5mKCIlZCIsJm4pOwogICAgICAgIEwuaW5pdChuKTsKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8IG47IGkgKyspewogICAgICAgICAgICBpbnQgdSx2OwogICAgICAgICAgICBzY2FuZigiJWQgJWQiLCZ1LCZ2KTsKICAgICAgICAgICAgTC5pbnNlcnRfZ3JhZ2godSwgdik7CiAgICAgICAgfQogICAgICAgIGludCB1LHY7CiAgICAgICAgc2NhbmYoIiVkICVkIiwmdSwmdik7CiAgICAgICAgTC5pbnNlcnRfcXVlcnkodSwgdik7CiAgICAgICAgTC5zb2x2ZShuKTsKICAgICAgICBwcmludGYoIiVkXG4iLCBMLlEuYXJjWzFdLnJlcyk7ICAgICAvL+esrGnkuKror6Lpl67lr7nlupTnrKwyaS0x5p2hUXVlcnnovrkKICAgIH0KICAgIHJldHVybiAwOwp9Cg==