HDU 1233 還是暢通工程 最小生成樹 Prim模板的應用

Ice_cream's world I
Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1138    Accepted Submission(s): 684


Problem 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


Author
Wiskey


Source
HDU 2007-10 Programming Contest_WarmUp
//作圖後可以看出每有一條迴路,就會有一塊地被劃分出
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 100000*2
int Par[MAXN], Rank[MAXN];
void Init(int n)
{
    for (int i = 1;i <= n;i++)
    {
        Par[i] = i;
        Rank[i] = 0;
    }
}
int Find(int x)
{
    if (Par[x] == x) return x;
    else return Par[x] = Find(Par[x]); ///同時壓縮路徑
}
void Unite(int x, int y)
{
    x = Find(x);y = Find(y);
    if (x == y) return;
    if (Rank[x]<Rank[y]) Par[x] = y;
    else Par[y] = x;
    if (Rank[x] == Rank[y]) Rank[x]++;
}
int main(void)
{
    //freopen("F:\\test.txt","r",stdin);
    int N,M;
    while(~scanf("%d %d",&N,&M))
    {
        Init(N);int Count = 0;
        for(int i=1,a,b;i<=M&&scanf("%d %d",&a,&b);i++)
        {
            if(Find(a+1)!=Find(b+1)) Unite(a+1,b+1);
            else Count++;//利用並查集判斷是否有迴路,自圈也是迴路
        }
        printf("%d\n",Count);
    }
}

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章