首页 > 代码库 > HDU2120【并查集判环】

HDU2120【并查集判环】

Ice_cream‘s world I

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 600    Accepted Submission(s): 344


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 100 11 21 32 43 40 55 66 73 64 7
 

 

Sample Output
3
 
大意:
题意很简单,刚开始我理解成两个塔之间有一个墙把他们隔开呢T_T
 
分析:并查集判环  只要在一个集合之中一定会有一块空地
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5  6 const int maxn = 1005; 7  8 int fa[maxn]; 9 10 int find(int x) {11     if(fa[x] == x) return x;12     return fa[x] = find(fa[x]);13 }14 15 int main() {16     int n, m;17     int a, b;18     while(EOF != scanf("%d %d",&n, &m) ) {19         for(int i = 0; i < n; i++) fa[i] = i;20         int cnt = 0;21         while(m--) {22             scanf("%d %d",&a, &b);23             if(find(a) == find(b) ) {24                 cnt++;25             } else {26                 fa[find(a)] = find(b);27             }28         }29         printf("%d\n", cnt);30     }31     return 0;32 }
View Code

 

 

HDU2120【并查集判环】