90pts , WA#1
查看原帖
90pts , WA#1
400783
Nephren_Sakura楼主2022/2/18 16:48

rt

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-48;ch=getchar();}
	return x*f;
}
int n=read(),m=read(),fa[1000005],cnt,depth[1000005],dp[1000005][21],big[1000005][21],big2[1000005][21],sum,mini=1e18;
bool vis[1000005];
struct edge{
	int x,y,w;
}a[1000005];
struct node{
	int y,w;
};
vector<node> nbr[1000005];
bool cmp(edge x,edge y){
	return x.w<y.w;
}
int find(int x){
	if(x==fa[x])
		return x;
	return fa[x]=find(fa[x]);
}
void dfs(int cur,int fa,int w){
	depth[cur]=depth[fa]+1;
	dp[cur][0]=fa;
	big[cur][0]=w;
//	if(depth[cur]>2)
//		big2[cur][1]=max(big[cur][0],big[dp[cur][0]][0]);
	for(int i=1; (1<<i)<=depth[cur]; i++)
		dp[cur][i]=dp[dp[cur][i-1]][i-1];
	for(int i=0; i<nbr[cur].size(); i++){
		int nxt=nbr[cur][i].y;
		if(nxt!=fa)
			dfs(nxt,cur,nbr[cur][i].w);
	}
}
int LCA(int x,int y){
	if(depth[x]>depth[y])
		swap(x,y);
	for(int i=20; i>=0; i--)
		if(depth[x]<=depth[dp[y][i]])
			y=dp[y][i];
	if(x==y)
		return x;
	for(int i=20; i>=0; i--)
		if(dp[x][i]!=dp[y][i])
			x=dp[x][i],y=dp[y][i];
	return dp[x][0];
}
void help(int cur,int fa){
	for(int i=1; (1<<i)<=depth[cur]; i++)
		big[cur][i]=max(big[cur][i-1],big[dp[cur][i-1]][i-1]);
	for(int i=0; i<nbr[cur].size(); i++){
		int nxt=nbr[cur][i].y;
		if(nxt!=fa)
			help(nxt,cur);
	}
	return;
}
//void help2(int cur,int fa){
//	if(depth[cur]>2){
//		for(int i=2; (1<<i)<=depth[cur]; i++){
//			int x=big[cur][i-1],y=big[dp[cur][i-1]][i-1];
//			if(x>y)
//				x=big2[cur][i-1];
//			else
//				y=big2[dp[cur][i-1]][i-1];
//			big2[cur][i]=max(x,y);
//		} 
//	}
//	for(int i=0; i<nbr[cur].size(); i++){
//		int nxt=nbr[cur][i].y;
//		if(nxt!=fa)
//			help2(nxt,cur);
//	} 
//	return;
//}
int hp(int x,int y){
	if(depth[x]>depth[y])
		swap(x,y);
	int ans=-1e9;
	for(int i=20; i>=0; i--){
		if(depth[x]<=depth[dp[y][i]]){
			ans=max(ans,big[y][i]);
			y=dp[y][i];
		}
	}
	return ans;
}
int hp2(int x,int y){
	int z=hp(x,y),maxi=-1e9;
	for(int i=20; i>=0; i--)
		if(depth[x]<=depth[dp[y][i]])
			if(big[y][i]!=z)
				maxi=max(maxi,big[y][i]);
	return maxi;
}
signed main(){
	for(int i=1; i<=n; i++)
		for(int j=1; j<=20; j++)
			big[i][j]=1e18;
	for(int i=1; i<=n; i++)
		fa[i]=i;
	for(int i=1; i<=m; i++){
		a[i].x=read();
		a[i].y=read();
		a[i].w=read();
	}
	sort(a+1,a+m+1,cmp);
	for(int i=1; i<=m; i++){
		int fx=find(a[i].x),fy=find(a[i].y);
		if(fx!=fy){
			vis[i]=true;
			fa[fx]=fy;
			sum+=a[i].w;
			nbr[a[i].x].push_back(node{a[i].y,a[i].w});
			nbr[a[i].y].push_back(node{a[i].x,a[i].w});
		}
	}
	dfs(1,0,0);
	help(1,0);
//	help2(1,0);
	for(int i=1; i<=m; i++){
		if(vis[i]==true)
			continue;
		int x=a[i].x,y=a[i].y;
		int lxy=LCA(a[i].x,a[i].y);
		if(hp(x,lxy)<hp(y,lxy))
			swap(x,y);
		int ans=hp(x,lxy);
		if(a[i].w!=ans)
			mini=min(mini,a[i].w-ans);
		else
			mini=min(mini,a[i].w-max(hp(y,lxy),hp2(x,lxy)));
	}
	cout<<sum+mini;
    return 0;
}

2022/2/18 16:48
加载中...