其实现在更新博客,写题解的兴致已经不高了,明天是网赛第一场,希望能够顺利。
写这篇文章是因为我打算把基本的算法都写一遍,以便笔试面试什么的。
从图论开始,对图论有一种说不出的喜欢。
HDU2988是Kruskal水题,为什么用Kruskal,因为点多200000,边少,稀疏图呢。
Kruska的算法是十分简单的,就是贪心,从最小权值开始遍历,通过并查集来确认加入这条边是否会形成回路,如果没有则加入到最小生成树的边集中,最后一定会形成最小生成树(MST)。
下面是code
/***********************************************************
> OS : Linux 3.13.0-24-generic (Mint-17)
> Author : yaolong
> Mail : dengyaolong@yeah.net
> Time : 2014年09月04日 星期四 07:52:04
**********************************************************/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 220000
int f[MAXN];
int find(int x)
{
if ( x == f[x] )
{
return x;
}
return f[x] = find(f[x]);
}
s