- Time: 2016-03-15-16:19:15 Tuesday
- Title Number: [2016-03-15][hdu][1213][how many Tables]
- Ask a friend to eat, each friend does not like and do not know the person in a table, give the understanding of the relationship, asked at least how many tables
- Input:
- Number of T groups
- Each set of data
- Nm
- M line U V for u and v recognition
- Output:
- Analysis: And look up sets, find the number of sets
#ifdef _WORK_
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
-
#define CLR ( Span class= "PLN" >x y memset x y Span class= "pun"), sizeof x Span class= "pun")))
-
#define for ( Span class= "PLN" >x y z for ( int ( x y x Span class= "pun") < ( z x
-
#define FORD ( Span class= "PLN" >x y z for ( int ( x y x Span class= "pun") >= ( z x
const int maxn = 1000 + 100;
int fa[maxn],mrank[maxn];
void ini(int n){
memset(mrank,0,sizeof(mrank));
for(int i = 0; i <= n;++i){
fa[i] = i;
}
}
int fnd(int x){
return x ==fa[x]?x:fa[x] = fnd(fa[x]);
}
void uni(int x,int y){
x = fnd(x);y = fnd(y);
if(x == y) return ;
if(mrank[x] < mrank[y]){
fa[x] = y;
}else {
fa[y] = x;
if(mrank[x] == mrank[y]) ++mrank[x];
}
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d%d",&n,&m);
ini(n);
FOR(i,0,m){
int u,v;
scanf("%d%d",&u,&v);
uni(u,v);
}
int ans = 0;
FOR(i,1,n + 1){ if(fa[i] == i)
++ans;
}
printf("%d\n",ans);
}
return 0;
}
#endif
From for notes (Wiz)
[2016-03-15] [HDU] [1213] [How many Tables]