HDU - 1213 How Many Tables解题报告(并查集)
2017-04-25 20:06
465 查看
题目大意:
并查集魔板题。代码:
#include<iostream> #include<math.h> #include<stdio.h> #include<string.h> #define maxn 1500 using namespace std; int test; int n,m; int boss[maxn]; void init() { for(int i=0;i<maxn;i++) { boss[i]=i; } } void add(int a,int b) { if(boss[a]==a&&boss[b]==b) { boss[a]=b; return; } boss[a]=boss[boss[a]]; boss[b]=boss[boss[b]]; add(boss[a],boss[b]); } int main() { cin>>test; while(test--) { init(); cin>>n>>m; for(int i=1;i<=m;i++) { int a,b; cin>>a>>b; add(a,b); } int s=0; for(int i=1;i<=n;i++) { if(boss[i]==i) { s++; } } cout<<s<<endl; } }
相关文章推荐
- HDU - 1213 How Many Tables 解题报告
- hdu 1213 How Many Tables 解题报告
- HDU1213 How Many Tables 解题报告
- hdu 1213 How Many Tables 解题报告
- HDU - 1213 How Many Tables 解题报告
- HDU--1213 -- How Many Tables [水水的并查集]
- HDU 1213 How Many Tables,并查集
- hdu 1213 How Many Tables(并查集学习)
- hdu 1213 How Many Tables(并查集练习)
- HDU 1213 How Many Tables(并查集)
- HDU 1213 How Many Tables 并查集 水~
- hdu 1213 How Many Tables(并查集,简单题)
- HDU 1213 How Many Tables 并查集
- HDU--1213--How Many Tables--并查集
- HDU - 1213 How Many Tables (简单并查集)
- HDU 1213 How Many Tables 并查集 水~
- HDU-#1213 How Many Tables (并查集)
- HDU1213 How Many Tables(简单的并查集)
- HDU 1213 How Many Tables 并查集入门
- 杭电hdu 1213 How Many Tables并查集