zoukankan      html  css  js  c++  java
  • [HDOJ3367]Pseudoforest(并查集,贪心)

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

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

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

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

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rll(a) scanf("%lld", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f7f, sizeof(a))
     53 #define lrt rt << 1
     54 #define rrt rt << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onenum(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long ULL;
     62 typedef pair<int, int> pii;
     63 typedef pair<string, int> psi;
     64 typedef map<string, int> msi;
     65 typedef vector<int> vi;
     66 typedef vector<LL> vl;
     67 typedef vector<vl> vvl;
     68 typedef vector<bool> vb;
     69 
     70 typedef struct Edge {
     71     int u, v, w;
     72     Edge() {}
     73     Edge(int uu, int vv, int ww) : u(uu), v(vv), w(ww) {}
     74 }Edge;
     75 const int maxn = 10010;
     76 const int maxm = 100010;
     77 bool circle[maxn];
     78 int n, m;
     79 int pre[maxn];
     80 Edge edge[maxm];
     81 
     82 bool cmp(Edge a, Edge b) {
     83     RT a.w > b.w;
     84 }
     85 
     86 int find(int x) {
     87     return x == pre[x] ? x : pre[x] = find(pre[x]);
     88 }
     89 
     90 int unite(int x, int y) {
     91     int fx = find(x);
     92     int fy = find(y);
     93     if(fx != fy) {
     94         if(circle[fx] && circle[fy]) return 0;
     95         if(circle[fx]) pre[fy] = fx;
     96         else pre[fx] = fy;
     97         return 1;
     98     }
     99     if(fx == fy) {
    100         if(circle[fx]) return 0;
    101         circle[fx] = 1;
    102         return 1;
    103     }
    104 }
    105 
    106 int main() {
    107     // FRead();
    108     int u, v, c;
    109     while(~Rint(n) && ~Rint(m) && n + m) {
    110         Cls(circle);
    111         Rep(i, n+5) pre[i] = i;
    112         Rep(i, m) {
    113             Rint(u); Rint(v); Rint(c);
    114             edge[i] = Edge(u, v, c);
    115         }
    116         sort(edge, edge+m, cmp);
    117         int ret = 0;
    118         Rep(i, m) {
    119             if(unite(edge[i].u, edge[i].v)) {
    120                 ret += edge[i].w;
    121             }
    122         }
    123         printf("%d
    ", ret);
    124     }
    125     RT 0;
    126 }
  • 相关阅读:
    聊聊赚钱
    Java面试官最爱问的volatile关键字
    你适合副业挣钱吗?
    SpringBoot自定义starter及自动配置
    mybatis进阶--输入映射和输出映射
    mybatis入门--初识mybatis
    mybatis入门--#{}和${}的区别
    mybatis入门--mybatis和hibernate比较
    mybatis入门--单表的增删改操作
    mybatis进阶--mapper输入映射和输出映射
  • 原文地址:https://www.cnblogs.com/kirai/p/5545167.html
Copyright © 2011-2022 走看看