[HDOJ3367]Pseudoforest(并查集,贪心)

时间:2021-08-30 09:06:15

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3367

求一个无向图上权值最大的伪森林。

伪森林:一个图的连通子图,当且仅当这个子图有且仅有一个环。

既然是一个图的连通子图,那这个图本身就是连通的就没有疑问了,我们就可以贪心地找尽可能大的边,把他们并在一起,在并的时候,用pre来维护他们的祖先,额外开一个circle维护一条边是否在一个环内。好像生成树啊…是不是可以求最大生成树再加上一条最大边呢?

 /*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lrt rt << 1
#define rrt rt << 1 | 1
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; typedef struct Edge {
int u, v, w;
Edge() {}
Edge(int uu, int vv, int ww) : u(uu), v(vv), w(ww) {}
}Edge;
const int maxn = ;
const int maxm = ;
bool circle[maxn];
int n, m;
int pre[maxn];
Edge edge[maxm]; bool cmp(Edge a, Edge b) {
RT a.w > b.w;
} int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
} int unite(int x, int y) {
int fx = find(x);
int fy = find(y);
if(fx != fy) {
if(circle[fx] && circle[fy]) return ;
if(circle[fx]) pre[fy] = fx;
else pre[fx] = fy;
return ;
}
if(fx == fy) {
if(circle[fx]) return ;
circle[fx] = ;
return ;
}
} int main() {
// FRead();
int u, v, c;
while(~Rint(n) && ~Rint(m) && n + m) {
Cls(circle);
Rep(i, n+) pre[i] = i;
Rep(i, m) {
Rint(u); Rint(v); Rint(c);
edge[i] = Edge(u, v, c);
}
sort(edge, edge+m, cmp);
int ret = ;
Rep(i, m) {
if(unite(edge[i].u, edge[i].v)) {
ret += edge[i].w;
}
}
printf("%d\n", ret);
}
RT ;
}