1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include<bits/stdc++.h> using namespace std; #define endl "\n" #define N 500005 int head[N], to[N<<1], nex[N<<1], cnt ; int visit[N], fa[N]; int qhead[N<<1], qnex[N<<1], qcnt = 1, qid[N<<1], ans[N], qto[N<<1]; void add_edge(int x, int y){ to[cnt] = y; nex[cnt] = head[x]; head[x] = cnt++; } void add_query(int x, int y, int w){ qto[qcnt] = y; qnex[qcnt] = qhead[x]; qid[qcnt] = w; qhead[x] = qcnt++; } int findx(int x){ if(fa[x] == x){ return x; } return fa[x] = findx(fa[x]); } void tarjan(int rt, int f){ for(int i = head[rt]; i; i = nex[i]){ if(to[i] == f)continue; tarjan(to[i], rt); fa[to[i]] = rt; } visit[rt] = 1; for(int i = qhead[rt]; i; i = qnex[i]){ if(visit[qto[i]] == 0)continue; ans[qid[i]] = findx(qto[i]); } } int main(){ ios::sync_with_stdio(false); int n, m, s; while(cin >> n >> m >> s){ int x, y; memset(head, 0, sizeof(head)); cnt = 1, qcnt = 1; memset(qhead, 0, sizeof(qhead)); for(int i = 1; i < n; i++){ cin >> x >> y; add_edge(x, y); add_edge(y, x); fa[i] = i; } fa[n] = n; for(int i = 1; i <= m; i++){ cin >> x >> y; add_query(x, y, i); add_query(y, x, i); } tarjan(s, 0); for(int i = 1; i <= m; i++){ cout << ans[i] << endl; } } return 0; }
|