鏈接:https://ac.nowcoder.com/acm/contest/549/F
來源:牛客網
題目描述
小A這次來到一個景區去旅遊,景區裏面有N個景點,景點之間有N-1條路徑。小A從當前的一個景點移動到下一個景點需要消耗一點的體力值。但是景區裏面有兩個景點比較特殊,它們之間是可以直接坐觀光纜車通過,不需要消耗體力值。而小A不想走太多的路,所以他希望你能夠告訴它,從當前的位置出發到他想要去的那個地方,他最少要消耗的體力值是多少。
輸入描述:
第一行一個整數N代表景區的個數。 接下來N-1行每行兩個整數u,v代表從位置u到v之間有一條路徑可以互相到達。 接下來的一行兩個整數U,V表示這兩個城市之間可以直接坐纜車到達。 接下來一行一個整數Q,表示有Q次詢問。 接下來的Q行每行兩個整數x,y,代表小A的位置在x,而他想要去的地方是y。
輸出描述:
對於每個詢問下x,y輸出一個結果,代表x到y消耗的最少體力對於每個詢問下x,y輸出一個結果,代表x到y消耗的最少體力
示例1
輸入
4 1 2 1 3 2 4 3 4 2 1 3 3 4
輸出
1 0
備註:
1≤N≤3e5, 1≤Q≤1e6
題意很簡單 ,給出一顆邊權爲1的樹,然後還有一個邊權爲0的邊存在(先不考慮),那可以直接dfs+ LCA 求出來樹上任意兩點之間的距離 dis(u,v) = num[u] + num[v] - 2 * num[LCA(u,v)] (num 代表節點的deep)
然後我們考慮邊權爲0的邊是否在u和v之間僅有的一條路徑上(所以不管在不在直接取個min就可以) 注意cin會T。。。
代碼如下:
#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second
#define endll "\n"
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
///vector(len,val);
using namespace std;
typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int> P;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
inline int cmp(db a,db b){ return sign(a - b);}
void debug(int x){ cout << x << endl; }
ll mul(ll a,ll b,ll c) { ll res = 1; while(b) { if(b & 1) res *= a,res %= c; a *= a,a %= c,b >>= 1; } return res;}
ll phi(ll x) { ll res = x; for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1); while(x % i == 0) x /= i; } if(x > 1) res = res / x * (x - 1); return res;}
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline ll add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline ll mul1(A x, B y) {return 1ll * x * y % mod;} /// x * y % mod;
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} /// return x * y
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline ll sqr(A x){return 1ll * x * x;}
template <typename A> A inv(A x) {return mul(x, mod - 2);}
inline ll rd() { char c = getchar(); ll x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}while(c >= '0' && c <= '9') x = 1ll * x * 10 + c - '0', c = getchar();return x * f;}
int fa[maxn];
int Find(int x) { if(x != fa[x]) return fa[x] = Find(fa[x]); return fa[x];}
ll c,n,k;
ll a[maxn],s1[maxn],s2[maxn];
int top[maxn],son[maxn],num[maxn],siz[maxn];
vector<int>v[maxn];
void dfs(int x,int _fa){
fa[x] = _fa;siz[x] = 1;
num[x] = num[_fa] + 1;
for(auto d:v[x]){
if(d == _fa) continue;
dfs(d,x); siz[x] += siz[d];
if(siz[d] > siz[son[x]]) son[x] = d;
}
}
void dfs2(int x, int topf) {
top[x] = topf;
if(!son[x]) return ;
dfs2(son[x], topf);
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i];
if(!top[to]) dfs2(to, to);
}
}
int LCA(int x, int y) {
while(top[x] ^ top[y]) {
if(num[top[x]] < num[top[y]]) swap(x, y);
x = fa[top[x]];
}
return num[x] < num[y] ? x : y;
}
int getnum(int x,int y){
return num[x] + num[y] - 2 * num[LCA(x,y)];
}
int main() {
ios::sync_with_stdio(false);
while(cin >> n){
int x,y,x1,y1;
for(int i = 1;i < n;i++){
x = read(),y = read();
v[x].push_back(y),v[y].push_back(x);
}
cin >> x1 >> y1;
num[1] = 1;
dfs(1,0);
dfs2(1,1); int q; cin >> q;
while(q--){
x = read(),y = read();
printf("%d\n",min(min(getnum(x,y) , getnum(x,x1) + getnum(y,y1) ),getnum(x,y1) + getnum(y,x1)));
}
}
cerr << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}