Tên đăng nhập Điểm Bài tập Giới thiệu
20401 0 0
20402 101 1
20402 101 1
20404 201 1
20405 0 0
20406 801 1
20406 801 1
20408 0 0
20408 0 0
20410
daon5880
Call Me Nguyên
21027 370

Toán,Lí,Hóa... em cthe chiến thắng tất cả, chỉ thua cuộc trước Anh.

20411 0 0

<><>

20411 0 0
20413 1173 3
20414 2001 1
20415 412 4
20416 392 2
20417 0 0

Ư3GER

20417 0 0
20419
neko_chan
Nguyễn Đặng Quỳnh Như
2330 25
while "i"<3*"u": 
 print("je t'aime tame")

01110100 01100001 01101101 01100101 00101011 01101110 01100101 01101011 01101111 00111101 01110100 01100001 01101011 01101111

20420 0 0
20421
Tranngocson2006
Trần Ngọc Sơn
8017 70
20422 1601 1
20423 822 2
20424 13751 53

manhmit

20425 1157 5
20426 0 0
20426 0 0
20428
longdegea11
No brain user
16551 262

Welcome to my profile. Feel free to take a look at it.

20429 0 0
20429 0 0
20431 2722 21
20432 2839 5
20433 801 1
20434 6624 103
20435 535 6
20436
datp3877
Phan Thanh Đạt
7309 13
20437 4654 4

include<bits/stdc++.h>

using namespace std;

define endl "\n"

define pb push_back

define ll long long

const int maxn=1e6+1;

vector<int>adj[maxn],queries[maxn];
int n,q,par[maxn],rnk[maxn],ancestor[maxn];
bool visited[maxn];
map<pair\<int,int>,int>mp;
vector<pair\<int,int>>ans;

int find_set(int u){
while(par[u]!=u)u=par[u];
return u;
}

void union_set(int x,int y){
int xroot=find_set(x);
int yroot=find_set(y);
if(xroot==yroot)return;
if(rnk[xroot]<rnk[yroot])par[xroot]=yroot; else if(rnk[xroot]>rnk[yroot])par[yroot] = xroot;
else{
par[xroot]=yroot;
rnk[yroot]++;
}
}

void dfs(int w){
visited[w]=true;
ancestor[w]=w;
for(int u:adj[w]){
if(!visited[u]){
dfs(u);
union_set(w,u);
ancestor[find_set(u)] = w;
}
}
for(int u:queries[w]){
if(visited[u]){
mp[{min(w,u),max(u,w)}]=ancestor[find_set(u)];
}
}
}

int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>q;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1; i <= n; i++)
par[i] = i;
for (int i=1;i<=q;i++) {
int u, v;
cin >> u >> v;if (u>v)swap(u,v);ans.pb({u,v});
queries[u].push_back(v);
queries[v].push_back(u);
}
dfs(1);
for (auto u:ans)cout<<mp[u]<<endl;
}

20438 981 2
20439 1043 2
20440 0 0
20441 301 1
20442
DangAC
Lê Minh Đăng
1005 3
20443 3026 12
20444
JackTor_Guy
Phạm Minh Quang, Long An
5813 18
20445 0 0
20446 28968 439
20447 1729 14
20448 7576 38
20449 798 10
20450 101 1
20451 256 1
20452 30 1

<= Youtube

20453 1268 9
20454 0 0
20454 0 0
20456 456 5
20457 0 0
20458 665 7

Người đẹp trai nhất thế giới không ai sánh bằng:)

20459 3457 7
20460 783 3
20461 0 0
20462 492 2
20463 733 1
20464 8649 22
20465 10 1
20466 101 1
20467 0 0
20467 0 0
20469 698 6
20470 0 0
20471 13154 29
20472 101 1

?

20473 727 1
20474 0 0
20475
Tydo_d
Two in one
18762 268
20476 0 0
20477 842 6
20478 1361 7
20479 101 1

idk

20480 0 0
20480 0 0
20480 0 0
20480 0 0
20480 0 0
20485 1237 4
20486 0 0
20486 0 0

rooster

20486 0 0
20489 5306 67
20490 4896 6
20491 11609 84
20492 0 0
20493 101 1
20494 1436 7
20495 0 0
20495 0 0
20497
Eviel
Đặng Nguyễn Hải Sơn
457 5

nub coder :<

20498 0 0
20499 2848 28
20500 288 3