标签:out struct find turn sync end 一个 space memset
发现了一个很奇怪的问题,二分的上界设置错了,结果是运行错误。。。只有95分。后来改了上界就100了,百思不得其解
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define pb(x) push_back(x)
#define cls(x, val) memset(x, val, sizeof(x))
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define inc(i, l, r) for(int i=l; i<=r; i++)
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+10;
struct Node{
int u, v, w;
};
int n, m;
vector<Node> G;
int fa[maxn];
int find_fa(int x){
return x == fa[x]?x:fa[x] = find_fa(fa[x]);
}
void uni(int u, int v){
int fau = find_fa(u);
int fav = find_fa(v);
if(fau!=fav){
fa[fau] = fav;
}
}
bool dfs(int val){
for(int i=1; i<=n; i++) fa[i] = i;
int u, v;
for(int i=0; i<G.size(); i++){
if(G[i].w>val) continue;
u = G[i].u, v = G[i].v;
uni(u, v);
}
find_fa(1), find_fa(n);
return (fa[1] == fa[n]);
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
int u, v, w;
for(int i=1; i<=m; i++){
cin>>u>>v>>w;
G.push_back(Node{u, v, w});
}
//很迷的一个wa点,上界设成1e9就总是95分
int l=1, r=1000000;
int ans;
while(l<=r){
int mid = (l+r)/2;
//cout<<l<<" "<<r<<endl;
if(dfs(mid)){
r=mid-1;
ans=mid;
}
else l=mid+1;
}
cout<<ans<<endl;
return 0;
}
标签:out struct find turn sync end 一个 space memset
原文地址:https://www.cnblogs.com/babydragon/p/11757573.html