hdu1213
2015-09-25 20:12
204 查看
最简单的并查集。
2015.10.5:
hahaha
2015.10.5:
hahaha
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; #define N 1010 int fa ; int root(int x){ if(fa[x]==x){ return x; } else{ fa[x]=root(fa[x]); return fa[x]; } } int main(){ int t; int n,m; int a,b; int ans; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ fa[i]=i; } ans=n; for(int i=1;i<=m;i++){ scanf("%d%d",&a,&b); int ra=root(a); int rb=root(b); if(ra==rb){ continue; } else{ ans--; fa[ra]=rb; } } printf("%d\n",ans); } }
相关文章推荐
- Fragment 生命周期 全局变量的声明位置
- Android 图片缓冲的管理-内存缓存
- quick-find【连通图】
- 【转载】GridView自动排序
- 《深入理解Linux网络技术内幕》阅读笔记(三)
- 蓝懿iOS培训日志20 MKMap
- GridView
- HTTP Live Streaming协议草案各版本加入的新特性
- 4.2二叉排序树
- Codeforces Beta Round #3——B. Lorry
- Cocos2d-x 3.2 大富翁游戏项目开发-第八部分 角色的散步路径
- NYOJ 32 组合数
- 我自己的软弱和情绪
- 字符串中单词的个数
- 第一个位和一个真正的项目件
- GUI
- IO流详解
- android HAL接口的编码小结
- 集合框架List部分
- STM32F4——IIC总线与SPI总线