HDU 2120 Ice_cream's world I
题意:给出n个塔和m堵墙,求出被围墙包围的区域有多少个。
分析:并查集判断环的个数。
View Code
#include <cstdio> #include <cstring> int f[1001]; int find(int x) { return f[x] == x?x:(f[x]=find(f[x])); } int main() { int n, m; int i, j; int res = 0; while (scanf("%d %d",&n,&m)!=EOF) { res = 0; for (i=0; i<n; i++) f[i] = i; while (m--) { int a,b; scanf("%d %d",&a,&b); int fx = find(a); int fy = find(b); if (fx==fy) res++; else f[fx] = fy; } printf("%d\n",res); } return 0; }