Ice_cream’s world I
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 59 Accepted Submission(s) : 39
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
题目意思:
有8个塔 有10条线(10 组数据 每组数据有两个数,意思是这两个塔之间有墙连着 也就是有十条线),解决的问题就是这十条线把这个封闭的区域分成了几小块。每条线之间还有可能相接,那么你所要做的就是这十条线能构成几个环,就是所形成的区域数
#include<cstdio> #include<cstring> #define maxn 1000+10 int p[maxn],ans=0; int find(int x) { while(x!=p[x]) x=p[x]; return p[x]; } int he(int a,int b) { if(find(a)!=find(b)) p[find(a)]=find(b); else ans++; // 如果根节点相同那么就成环了,++ return ans; } int main() { int i,x,y; while(~scanf("%d%d",&x,&y)) { for(i=0;i<x;i++) p[i]=i; int x1,y1; while(y--) { scanf("%d%d",&x1,&y1); he(x1,y1); } printf("%d\n",ans); ans=0; } return 0; }