码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2120 Ice_cream's world I

时间:2015-09-08 21:58:27      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2120  

Ice_cream‘s world I

Description

ice_cream‘s world is a rich country, it has many fertile lands. Today, the queen of ice_cream wants award land to diligent ACMers. So there are some watchtowers are set up, and wall between watchtowers be build, in order to partition the ice_cream’s world. But how many ACMers at most can be awarded by the queen is a big problem. One wall-surrounded land must be given to only one ACMer and no walls are crossed, if you can help the queen solve this problem, you will be get a land.

Input

In the case, first two integers N, M (N<=1000, M<=10000) is represent the number of watchtower and the number of wall. The watchtower numbered from 0 to N-1. Next following M lines, every line contain two integers A, B mean between A and B has a wall(A and B are distinct). Terminate by end of file.

Output

Output the maximum number of ACMers who will be awarded.
One answer one line.

Sample Input

8 10
0 1
1 2
1 3
2 4
3 4
0 5
5 6
6 7
3 6
4 7

Sample Output

3

用并查集统计环的个数。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::map;
using std::min;
using std::find;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1010;
const int INF = 0x3f3f3f3f;
struct UinonFind {
    int ans, par[N], rank[N];
    inline void init(int n) {
        ans = 0;
        rep(i, n + 1) {
            par[i] = i;
            rank[i] = 0;
        }
    }
    inline int find(int x) {
        while(x != par[x]) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
    inline void unite(int x, int y) {
        x = find(x), y = find(y);
        if(x == y) { ans++; return; }
        if(rank[x] < rank[y]) {
            par[x] = y;
        } else {
            par[y] = x;
            rank[x] += rank[x] == rank[y];
        }
    }
    inline void solve(int n, int m) {
        init(n);
        int x, y;
        while(m--) {
            scanf("%d %d", &x, &y);
            unite(x, y);
        }
        printf("%d\n", ans);
    }
}go;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    int n, m;
    while(~scanf("%d %d", &n, &m)) {
        go.solve(n, m);
    }
    return 0;
}

hdu 2120 Ice_cream's world I

标签:

原文地址:http://www.cnblogs.com/GadyPu/p/4792795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!