24分求调(悬关)
查看原帖
24分求调(悬关)
917683
rnf5114楼主2024/1/23 01:00

题目数据里是有重边吗

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,m,fa[40010],Max,lu[40010],s,vis1[1000010],vis2[1000010],ans1,ans,rans=1,fat[40010];
const int mod=1e9+7;
struct node{
	int x,y,zhi;
}p[200010];
int Find(int x){
	int root=x;
	while(root!=fa[root]){
		root=fa[root];
	}
	while(x!=root){
		int tmp=fa[x];
		fa[x]=root;
		x=tmp;
	}
	return root;
}
bool cmp(node x,node y){
	return x.zhi<y.zhi;
}
void mei(int l,int r,int zhi){
	if(zhi==1){
		ans1=(r-l+1);
	}
	if(zhi==2){
		if(r-l+1==2){
			ans1=1;
		}
		else{
			ans1=3;
		}
	}
	if(zhi==3){
		ans1=1;
	}
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++)
		cin>>p[i].x>>p[i].y>>p[i].zhi;
	for(int i=1;i<=n;i++)
		fa[i]=i;
	sort(p+1,p+m+1,cmp);
	int k=0;
	for(int i=1;i<=m;i++){
		int fx=Find(p[i].x),fy=Find(p[i].y);
		if(fx!=fy){
			ans+=p[i].zhi;
			vis2[p[i].zhi]++;
			fa[fx]=fy;
			k++;
		}
		if(k==n-1){
			Max=p[i].zhi;
			cout<<ans<<" ";
			break;
		}
	}
	
	for(int i=1;i<=m;i++){
		if(vis1[p[i].zhi]){
			continue;
		}
		int wei=i;
		ans1=1;
		for(int j=i;j<=m;j++){
			if(p[i].zhi==p[j].zhi){
				wei=j;
			}
			else{
				break;
			}
		}
		vis1[p[i].zhi]=1;
		mei(i,wei,vis2[p[i].zhi]);
		rans*=ans1;
		rans%=mod;
	}
	cout<<rans;
    return 0;
}


/*
       ┏┓   ┏┓
     ┏┛┻━━━┛┻┓
     ┃       ┃
     ┃   ━   ┃
     ┃ ┳┛ ┗┳ ┃
     ┃       ┃
     ┃   ┻   ┃
     ┃       ┃
     ┗━┓   ┏━┛Codes are far away from bugs with the animal protecting
         ┃   ┃    神兽保佑,代码无bug
         ┃   ┃
         ┃   ┗━━━┓
         ┃      ┣┓
         ┃     ┏┛
         ┗┓┓┏━┳┓┏┛
           ┃┫┫ ┃┫┫
           ┗┻┛ ┗┻┛     ○| ̄|_

*/
2024/1/23 01:00
加载中...