Problem-solving ideas: Give the N-point relationship, how many rings, if for point X and Point y, they have a wall, that is, they are connected to themselves, if find (x) =find (y), indicating that their root node is the same, they must be directly or indirectly connected, that is, forming a ring
Examples of
A total of 3 rings
Ice_cream ' s World I
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 642 Accepted S Ubmission (s): 371
Problem Descriptionice_cream ' s world is a rich country, it had many fertile lands. Today, the Queen of Ice_cream wants award land to diligent acmers. So there is some watchtowers is set up, and wall between watchtowers is build, in order to partition the Ice_cream ' s WOR Ld. But what many acmers at most can be awarded by the Queen is a big problem. One wall-surrounded land must is given to only one acmer and no walls is crossed if you can help the Queen solve this PR Oblem, you'll be was get a land. inputin the case, first and integers N, M (n<=1000, m<=10000) is represent the N Umber of Watchtower and the number of wall. The watchtower numbered from 0 to N-1. Next following M lines, every line contain, integers a, b mean between A and b have A wall (A and B are distinct). Terminate by end of file. outputoutput the maximum number of acmers who would be awarded. One answer one line. sample Input8 0 1 1 2 1 3 2 4 3 4 0 5 5 6 6 7 3 6 4 7 sample output3
#include <stdio.h> #include <string.h> int pre[10005],a[10005]; int find (int root) {if (Root!=pre[root]) Pre[root]=find (Pre[root]); return pre[root];} void unionroot (int root1,int root2) {int x,y;x=find (ROOT1); Y=find (ROOT2); if (x!=y) pre[x]=y;} int main () {int root1,root2,x,y,i,n,m,tmp;while (scanf ("%d%d", &n,&m)!=eof) {tmp=0;for (i=0;i<=10005;i++) Pre[i]=i;while (m--) {scanf ("%d%d", &root1,&root2), X=find (ROOT1); Y=find (ROOT2); if (x==y) tmp++;unionroot (x, y);} printf ("%d\n", tmp);}}
HDU 2120 Ice_cream ' s World I "and check Set"