小白月赛13 F:小A的最短路(LCA + dfs)

2 篇文章 0 订阅
1 篇文章 0 订阅

链接: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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值