bzoj1083

辣鸡题  裸最小生成树


#include 
#include 
using namespace std;
int n,m,head[310];

struct edge {
	int u,v,w;
	bool operator < (const edge &b)const {
		return w < b.w;
	}
}e[90010];

int read_int () {
	char c = getchar();
	int re = 0;
	for(;c > '9' || c < '0'; c = getchar());
	for(;c >= '0' && c <= '9';c = getchar())
		re = re * 10 + c - '0';
	return re;
}

int find (int u) {
	if(head[u] == u)
		return u;
	return head[u] = find(head[u]);
}

int main () {
	n = read_int();
	m = read_int();
	for(int i = 1;i <= n;++i)
		head[i] = i;
	for(int i = 1;i <= m;++i) {
		e[i].u = read_int();
		e[i].v = read_int();
		e[i].w = read_int();
	}
	sort(e + 1,e + 1 + m);
	int ans,cnt = 0;
	for(int i = 1;i <= m;++i) {
		if(find(e[i].u) != find(e[i].v)) {
			++cnt;
			head[find(e[i].u)] = find(e[i].v);
			ans = e[i].w;
		}
	}
	printf("%d %d\n",cnt,ans);
}


你可能感兴趣的:(最小生成树)