这集傻逼了。
原题解
经验。
首先最小生成树容易求出来。
显然无论是非严格次小还是严格都一定是到外面选择一个边过来替换掉原树上的一个边。
先讨论非严格,假设新加的边是 那么删掉的边一定是 树上路径里边权最大的边。
诶,最小生成树上,路径边权最大,这不是瓶颈路权值吗!
上 kruskal 重构树,容易发现这个权值就在这两个点的 上。
严格次小就是要求,删掉的这个边必须权值要小于当前边,如果 合法就选了。
否则,重构树上边权有单调性,路径上的边依然是重构树路径上的边,直接两个端点向上倍增即可。
时间复杂度 。
感谢 zhang_jinghan 的 hack,我这个做不了严格次小。不过非严格次小应该没问题。
注意到两个点在重构树上路径的边集其实是原树路径边集的超集,于是倍增找到的次大值不一定在原树路径上,遂倒闭。
数据两则:
输入 #1:
3 4
1 2 1
2 3 2
2 3 2
1 3 4输出 #1:
5输入 #2:
4 5
1 2 1
2 3 2
3 4 2
1 4 114
2 4 2输出 #2:
117所以有没有人教教这个到底咋做的 /fad
要到楼主的 code 了。
code
#include<iostream>
#include<vector>
#include<numeric>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
using pii=pair<int,int>;
inline unsigned lg(int n) {return 31-__builtin_clz(n);}
inline unsigned lg(unsigned n) {return 32-__builtin_clz(n);}
inline unsigned lg(long long n) {return 63-__builtin_clz(n);}
inline unsigned lg(unsigned long long n) {return 64-__builtin_clz(n);}
vector<int> dsu;
inline int find(int x) {
return x==dsu[x] ? x : dsu[x] = find(dsu[x]);
}
struct edge {
int u,v,w;
edge(int a=0,int b=0,int c=0)
:u(min(a,b)),v(max(a,b)),w(c){}
bool operator<(const edge& e) const {
if(w!=e.w) return w<e.w;
if(u!=e.u) return u<e.u;
if(v!=e.v) return v<e.v;
}
bool operator==(const edge&b){
return u==b.u&&v==b.v&&w==b.w;
}
};
struct node {
int l,r;
long long w;
explicit node(int l=0, int r=0, long long w=-1)
: l(l),r(r),w(w) {}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n,m;
cin>>n>>m;
dsu.resize(n<<1);
iota(dsu.begin(),dsu.end(),0);
vector<edge> e(m);
for(auto&i:e)cin>>i.u>>i.v>>i.w;
sort(e.begin(),e.end());
e.erase(unique(e.begin(),e.end()),e.end());
vector<node> tree(n+1);
vector<char> vis(e.size());
vector<vector<long long>> f(n<<1,vector<long long>(lg(n<<1)+1)),
sm(n<<1,vector<long long>(lg(n<<1)+1,-1));
long long mst=0;
tree.reserve(n<<1);
for (int j=0;j<e.size();j++){
const auto&i=e[j];
int fu=find(i.u),fv=find(i.v);
if (fu!=fv) {
tree.emplace_back(fu,fv,i.w);
f[fu][0]=f[fv][0]=tree.size()-1,dsu[fv]=dsu[fu]=tree.size()-1;
vis[j]=true,mst+=i.w;
}
}
queue<pii> q;
vector<int> dep(n<<1);
for (int i=tree.size()-1;i>0;i--) if (dep[i]==0){
q.emplace(i,0);
while (!q.empty()) {
int r=q.front().first,fa=q.front().second;
q.pop();
dep[r]=dep[fa]+1;
if (tree[r].l!=fa&&tree[r].l)
q.emplace(tree[r].l,r);
if (tree[r].r!=fa&&tree[r].r)
q.emplace(tree[r].r,r);
}
}
for (int j=1;(1<<j)<(n<<1);j++)
for (int i=1;i<(n<<1);i++) {
f[i][j]=f[f[i][j-1]][j-1];
long long cand[]={
tree[f[i][j-1]].w,tree[f[f[i][j-1]][j-1]].w,
sm[i][j-1],sm[f[i][j-1]][j-1]
};
sort(cand,cand+4);
int p=3;
while (p>=0&&cand[p]==cand[3])p--;
sm[i][j]=(p<0)?-1:cand[p];
}
auto lca=[&](int u,int v,const int w)->long long {
if(u==v)return 1e18;
long long ans=-1;
if(dep[u]<dep[v])swap(u,v);
for(int i=f.front().size()-1;i>=0;i--)
if(dep[f[u][i]]>=dep[v]) {
if (tree[f[u][i]].w<w)ans=max(ans,tree[f[u][i]].w);
if (sm[u][i]<w)ans=max(ans,sm[u][i]);
u=f[u][i];
}
if(u==v)return ans;
for(int i=f.front().size()-1;i>=0;i--)
if(f[u][i]!=f[v][i]) {
if (tree[f[u][i]].w<w)ans=max(ans,tree[f[u][i]].w);
if (sm[u][i]<w)ans=max(ans,sm[u][i]);
if (tree[f[v][i]].w<w)ans=max(ans,tree[f[v][i]].w);
if (sm[v][i]<w)ans=max(ans,sm[v][i]);
u=f[u][i],v=f[v][i];
}
if (tree[f[u][0]].w<w)ans=max(ans,tree[f[u][0]].w);
if (sm[u][0]<w)ans=max(ans,sm[u][0]);
if (tree[f[v][0]].w<w)ans=max(ans,tree[f[v][0]].w);
if (sm[v][0]<w)ans=max(ans,sm[v][0]);
return ans;
};
long long smst=1e18;
for (int i=0;i<e.size();i++)if (!vis[i]&&e[i].u!=e[i].v) {
long long t=lca(e[i].u,e[i].v,e[i].w);
if (t==1e18||t==-1)continue;
smst=min(smst,mst+e[i].w-t);
}
cout<<smst;
return 0;
}hacked.
输入:
4 4
1 2 1
1 3 2
1 4 3
2 4 3正确输出:
8错误输出:
7因为不明原因 zjh 精心手搓的两组 hack 没有叉掉,这组 hack 是大肥鱼干的。
这排序方法有力气,怎么有把 也放进去排序的啊 /咦
把 zjh 的第二个 hack 改造成如下形式:
4 5
1 2 1
2 3 2
3 4 2
1 4 114
4 2 2注意到只是调换了最后一个边的 ,但是 hack 成功了!
代码的错误输出为:
6