牛客练习赛74 D. CCA的图

链接

https://ac.nowcoder.com/acm/contest/9700/D

题意

无向连通图,规定只能走权值在 [ L , R ] [L , R] [L,R] 内的边。

求在 L , R L,R LR 分别等于多少时,可以顺利从 s s s 到达 t t t,要求 L L L 尽可能大,在 L L L 最大的情况下 R R R 尽可能小。

思路

从大到小枚举边,用并查集维护点之间连通关系,当 s s s t t t 连通时,当前边权值即为 L L L

再枚举权值大于等于 L L L 的边,用同样的方法确定 R R R

代码

#include <bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
//head
const int N=1e6+6;
int n,m,s,t,l,r,fa[N];
struct E {
    int u,v,w;
    E() {}
    E(int u,int v,int w):u(u),v(v),w(w) {}
    bool operator < (const E &a) const {
        return w<a.w;
    }
};
vector<E> e;
int find(int x) {
    return x==fa[x]?x:fa[x]=find(fa[x]);
}
void merge(int x,int y) {
    int tx=find(x),ty=find(y);
    if(tx==ty) return;
    fa[tx]=ty;
}
int main() {
    //freopen("E:/OneDrive/IO/in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>m>>s>>t;
    for(int i=1;i<=m;i++) {
        int u,v,w;
        cin>>u>>v>>w;
        e.EB(u,v,w);
    }
    sort(ALL(e));
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=SZ(e)-1;i>=0;i--) {
        merge(e[i].u,e[i].v);
        if(find(s)==find(t)) {l=e[i].w;break;}
    }
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=0;i<SZ(e);i++) {
        if(e[i].w<l) continue;
        merge(e[i].u,e[i].v);
        if(find(s)==find(t)) {r=e[i].w;break;}
    }
    cout<<l<<' '<<r<<'\n';
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值