Tip1:二分查找确实是个有效手段(lower_bound yyds),与其绞尽脑汁想一个O(n)算法不如二分+其他O(nlogn)可行
1.信息传递(洛谷P2661)
并查集求最小环:
#include <bits/stdc++.h>
using namespace std;
int f[200005],d[200005],ans=0x3f3f3f3f;
int fa(int x){
if (f[x]!=x){
int last=f[x];
f[x]=fa(f[x]);
d[x]+=d[last];
}
}
void join(int i,int x){
int fi=fa(i),fx=fa(x);
if(fi!=fx) {
f[fi]=fx;
d[i]=d[x]+1;
}
else ans=min(ans,d[i]+d[x]+1);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for(int i=1;i<=n;i++){
f[i]=i;
}
int x;
for(int i=1;i<=n;i++){
cin >> x;
join(i,x);
}
return 0;
}
rnm今天没想明白明天接着想