和TLE有仇!!kruskal为什么TLE??(雾)
查看原帖
和TLE有仇!!kruskal为什么TLE??(雾)
116015
bellmanford楼主2020/6/6 23:07

TLE 3个点,zbl

#pragma GCC optimize("O3")
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;

const int M=1e5+5;

void swap(int &x,int &y){ return (void)(x^=y^=x^=y); }
int min(int x,int y){ return x<y?x:y; }
int max(int x,int y){ return x>y?x:y; }

int n,m,fa[M],hep[M],minn[M],lazy[M],ch[M][2];

int read(){
	int x=0,y=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') y=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*y;
}

int get(int u){
	return (ch[fa[u]][0]==u||ch[fa[u]][1]==u);
}

void pushup(int u){
	minn[u]=min(minn[ch[u][0]],minn[ch[u][1]]);
}

void flip(int u){
	swap(ch[u][0],ch[u][1]);
	lazy[u]^=1;
}

void pushdown(int u){
	if(!lazy[u]) return ;
	lazy[u]=0;
	if(ch[u][0]) flip(ch[u][0]);
	if(ch[u][1]) flip(ch[u][1]);
}

void rotate(int u){
	int a=fa[u],b=fa[a],d1=(ch[a][1]==u),d2=(ch[b][1]==a);
	int v=ch[u][d1^1];
	if(get(a)) ch[b][d2]=u;
	ch[u][d1^1]=a,ch[a][d1]=v;
	if(v) fa[v]=a;
	fa[a]=u,fa[u]=b;
	pushup(a),pushup(u);
}

void Splay(int u){
	int v=u,top=0;
	hep[++top]=v;
	while(get(v)) hep[++top]=v=fa[v];
	while(top) pushdown(hep[top--]);
	while(get(u)){
		v=fa[u],top=fa[v];
		if(get(v)) rotate((ch[v][0]==u)^(ch[top][0]==v)?u:v);
		rotate(u);
	}
	pushup(u);
}

void Access(int u){
	for(int v=0;u;u=fa[v=u]){
		Splay(u);
		ch[u][1]=v;
		pushup(u);
	}
}

void makeroot(int u){
	Access(u);
	Splay(u);
	flip(u);
}

void Split(int u,int v){
	makeroot(u);
	Access(v);
	Splay(v);
}

int findroot(int u){
	Access(u);
	Splay(u);
	while(ch[u][0]){
		pushdown(u);
		u=ch[u][0];
	}
	return u;
}

void Link(int u,int v){
	makeroot(u);
	if(findroot(v)!=u) fa[u]=v;
}

void Cut(int u,int v){
	Split(u,v);
	if(findroot(v)==u&&fa[u]==v&&!ch[u][1]){
		fa[u]=ch[v][0]=0;
		pushup(v);
	}
}

struct CuCun{
	int x,y,z;
}c[M];
bool cmp(CuCun x,CuCun y){
	return x.z<y.z;
}

void solve(){
	n=read(),m=read();int Ans=0,bian=0;
	for(int i=1;i<=m;i++) c[i].x=read(),c[i].y=read(),c[i].z=read();
	sort(c+1,c+m+1,cmp);
	for(int i=1;i<=m;i++){
		int x=c[i].x,y=c[i].y,z=c[i].z;
		makeroot(x);
		if(findroot(y)==x) continue ;
		fa[x]=y;
		Ans+=z;
		bian++;
	}
	if(bian<n-1) return (void)(printf("orz\n"));
	return (void)(printf("%d\n",Ans));
}

int main(){
	solve();
}
2020/6/6 23:07
加载中...