HDU 1232
来源:互联网 发布:dock软件iphone6s 编辑:程序博客网 时间:2024/05/29 14:24
#include<stdio.h>int father[1005],depth[1005];void init(){ int i; for(i = 1; i < 1005;i ++) { father[i] = i; depth[i] = 0; }}int find(int x){ if(x==father[x]) return x; else return father[x] = find(father[x]); //优化树的高度;}void unit(int x,int y){ x = find(x); y = find(y); if(x==y) return ; if(depth[x]<depth[y]) //注意树的优化,防止高度过高而超时; { father[x] = father[y]; } else { if(depth[x]>depth[y]) father[y] = father[x]; else { father[x] = father[y]; depth[y]++; } }}int main(){ int n,m,a,b,j,gr; while(~scanf("%d%d",&n,&m)&&n!=0) { gr = 0; init(); while(m--) { scanf("%d%d",&a,&b); unit(a,b); } for(j = 1;j <= n;j ++) { if(j==father[j]) gr++; } printf("%d\n",gr-1); } return 0;}
0 0
- HDU 1232
- HDU 1232
- HDU 1232
- hdu 1232
- HDU 1232
- hdu 1232
- HDU 1232
- hdu 1232
- hdu 1232
- /* HDU 1232 */
- HDU 1232
- HDU 1232
- hdu 1232
- hdu 1232
- HDU 1232
- HDU 1232
- HDU 1232
- hdu 1232
- 学习zigbee入门-6
- android apk打包签名机制
- Windows 下 gpedit.msc services.msc 扩展选项卡空白
- [五一特别礼物] 最强的反编译 ida Pro 6.5 Demo Plus来了
- 学习zigbee入门-7
- HDU 1232
- HDU 1272
- HDU 1002
- hostapd--内部结构分析
- HDU 1013
- HDU 1004
- 学习zigbee入门-8
- HDU 1213
- 初学acm感想