求助,不知道为什么会炸空间
查看原帖
求助,不知道为什么会炸空间
366108
tombs楼主2022/7/31 18:19
#include<iostream>
#include<algorithm>
#include<cstring>
#define re register
using namespace std;
const int mod=1e9+7;
int ans1,ans2=1;
int fa[40100],cnt;
int n,m,xx[4],yy[4],cntx,cnty;
struct edge{
	int next,to,dis;
}e[100100];
void add(int u,int v,int w){
	e[++cnt].to=v;
	e[cnt].dis=w;
	e[cnt].next=u;
}
bool cmp(edge a,edge b){
	return a.dis<b.dis;
}
int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
void kruskal(){
	for(re int i=1;i<=n;++i) fa[i]=i;
	int x,y;
	for(re int i=1;i<=cnt;++i){
		//cout<<cntx<<" "<<cnty<<endl;
		x=find(e[i].next);
		y=find(e[i].to);
		if(x!=y){
			xx[++cntx]=x;
			yy[++cnty]=y;	
	    }
		if(e[i].dis!=e[i+1].dis){
			if(cntx==1){
				fa[xx[1]]=yy[1];
				ans1+=e[i].dis;
			}else if(cntx==2){
				if(xx[1]==xx[2]&&yy[1]==yy[2]){
					fa[xx[1]]=yy[1];
					ans2*=2;
					ans1+=e[i].dis;
				}else{
					fa[xx[1]]=yy[1];
					fa[xx[2]]=yy[2];
					ans1+=e[i].dis*2;
				}
			}else if(cntx==3){
				if(xx[1]==xx[2]&&xx[2]==xx[3]&&yy[1]==yy[2]&&yy[2]==yy[3]){
					fa[xx[1]]=yy[1];
					ans2*=3;
					ans1+=e[i].dis;
				}else if(xx[1]==xx[2]&&yy[1]==yy[2]){
					fa[xx[1]]=yy[1];
					fa[xx[3]]=yy[3];
					ans2*=2;
					ans1+=e[i].dis*2;
				}else if(xx[1]==xx[3]&&yy[1]==yy[3]){
					fa[xx[1]]=yy[1];
					fa[xx[2]]=yy[2];
					ans2*=2;
					ans1+=e[i].dis*2;
				}else if(xx[2]==xx[3]&&yy[2]==yy[3]){
					fa[xx[2]]=yy[2];
					fa[xx[1]]=yy[1];
					ans2*=2;
					ans1+=e[i].dis*2;
				}else{
					fa[xx[1]]=yy[1];
					fa[xx[2]]=yy[2];
					x=find(fa[xx[3]]);
					y=find(fa[yy[3]]);
					if(x==y){
						ans2*=3;
						ans1+=e[i].dis*2;
					}else{
						fa[x]=y;
						ans1+=e[i].dis*3;
					}
				}
			}
			cntx=0;
			cnty=0;
			ans1%=mod;
			ans2%=mod;
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(re int i=1;i<=m;++i){
		int u,v,w;
		cin>>u>>v>>w;
		add(u,v,w);
	}
	sort(e+1,e+m+1,cmp);
	//cout<<e[1].dis<<endl;
	kruskal();
	cout<<ans1%mod<<" "<<ans2%mod<<endl;
}
2022/7/31 18:19
加载中...