下午CSP了,我到现在连个并查集都错,枯了
查看原帖
下午CSP了,我到现在连个并查集都错,枯了
55227
魂逝_秦月歌楼主2020/11/7 11:52
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <cctype>
#include <queue>
#include <deque>
#include <ctime>
#include <cstring>
#include <string>
#define _e putchar (' ')
#define _v putchar ('\n')
#define ll long long
#define INF 999999999999999999ll
#define INE -999999999999999999ll
#define File(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
using namespace std;
inline int lowbit (int x) {
	return x&(-x);
}
inline int mx(int x,int y) {
	return x>y?x:y;
}
inline int mn(int x,int y) {
	return x<y?x:y;
}
inline void r(int &x) {
	int s=0,w=1;
	char ch=getchar ();
	while(ch<'0'||ch>'9') {if(ch=='-') w=-1; ch=getchar ();}
	while(ch>='0'&&ch<='9') {s=(s<<1)+(s<<3)+(ch^48); ch=getchar ();}
	x=s*w;
}
inline void wr(int x) {
	if(x<0) x=-x,putchar (45);
	if(x<10) {
		putchar (x+48);
		return ;
	}
	wr(x/10);
	putchar (x%10+48);
}
inline int ab (int x) {
	if(x<0) return -x;
	return x;
}
inline void swap (int &x,int &y) {
	x^=y^=x^=y;
}
const int N=1000005;
int n,m,fa[N],sum;
struct node {
	int x,y,t;
}a[N];
inline bool cmp (node X,node Y) {
	return X.t<Y.t;
}
inline int find (int x) {
	if(fa[x]==x) return x;
	return fa[x]=find (fa[x]);
}
inline void uni (int x,int y) {
	fa[find(x)]=find(y);
}
int main () {
	r(n),r(m);
	for(int i=1;i<=m;i++) 
		r(a[i].x),r(a[i].y),r(a[i].t);
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++)
	    fa[i]=i;
	for(int i=1;i<=m;i++) {
		if(find(a[i].x)!=find(a[i].y)) uni(a[i].x,a[i].y),sum++;
		if(sum==n-1) {
			wr(a[i].t),_v;
			return 0;
		}
	}
	wr(-1),_v;
	return 0;
}


麻烦各位大佬康康
2020/11/7 11:52
加载中...