hdu 1213 How Many Tables
来源:互联网 发布:剑网三dx11优化版 编辑:程序博客网 时间:2024/05/16 05:41
简单并查集,求出并查集的个数。一句话:尽信书,不如无书。凡事要自己思考。merge时都是把双方的根结点来merge。里面的函数均是正确的。
AC代码:
#include<stdio.h>#include<string.h>int n,m,res;#define NUM 1005int set[NUM];int find(int x){return set[x];}void merge(int a, int b){int i,j,k,temp;i=find(a);j=find(b);if(i>j){temp=i;i=j;j=temp;}for(k=1;k<=n;k++){if(set[k]==j)set[k]=i;}}int find1(int x){int r=x;while(set[r]!=r)r=set[r];return r;}void merge1(int a, int b){int x=find1(a);int y=find1(b);set[x]=y;}int main(){int t,i,a,b;freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin);scanf("%d",&t);while(t--){scanf(" %d%d",&n,&m);res=n;for(i=0;i<=n;i++)set[i]=i;for(i=0;i<m;i++){scanf("%d%d",&a,&b);if(find1(a)!=find1(b)){merge1(a,b);res--;}}printf("%d\n",res);}return 0;}
- 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 1213how 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
- 惯性是创新的天敌
- 爬楼梯问题 一次走一步或两步,输入台阶数,输出走法次数
- 85条人生感悟:每天读一遍,不久你就会变!
- Oracle 字符集切换
- 关于http接口防止非法调用的设计方案
- hdu 1213 How Many Tables
- poj 2387 最短路径Dijkstra
- java中httpclent以post方式提交请求时,http返回码是302重定向时,不自动跳转的处理
- 九度OJ 题目1141:Financial Management
- c++中利用标准库 将string转换为int
- plpython 9.2.2
- C++ 匿名namespace的作用以及它与static的区别
- hits算法
- MapReduce执行流程