HDU 1213 How Many Tables
2012-08-22 10:57
381 查看
题意:一个人不会坐在一个人也不认识的桌子上;
思路:并查集求解,初始桌子数为n ,如果有认识的人归并在一起,桌子数-1。
思路:并查集求解,初始桌子数为n ,如果有认识的人归并在一起,桌子数-1。
#include<iostream> using namespace std; int father[1005]; int n; void Init() { for(int i=0;i<1001;i++) father[i]=i; } int find(int x) { if(x!=father[x]) father[x]=find(father[x]); return father[x]; } void merge_set(int x,int y) { int fx=find(x); int fy=find(y); if(fx!=fy) { n--; father[fy]=fx; } } int main() { int t,b,m,i,x,y; cin>>t; b=t; while(t--) { if(t==(b-1)) getchar(); cin>>n>>m; Init(); for(i=1;i<=m;i++) { cin>>x>>y; merge_set(x,y); } cout<<n<<endl; } return 0; }
相关文章推荐
- HDU1213_How Many Tables
- hdu 1213 How Many Tables
- HDU-1213-How Many Tables
- How Many Tables HDU - 1213
- hdu 1213 How Many Tables
- hdu1213--How Many Tables
- HDU 1213 How many tables
- HDU-1213-How Many Tables
- How Many Tables HDU - 1213 并查集
- 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