ACM_HDU 1213 How Many Tables(基础并查集)
来源:互联网 发布:二战 日本 知乎 编辑:程序博客网 时间:2024/05/16 11:16
#include<iostream>#define NN 1000int ranka[NN];using namespace std;struct node { int pre; }p[NN]; void Init(int n) { for(int i=1;i<=n;i++) { p[i].pre=i; ranka[i]=1; } } int find(int x) { return x == p[x].pre ? x : find(p[x].pre); } void join(int x,int y) { x=find(x); y=find(y); if(x==y)return; if(ranka[x]>=ranka[y]) { p[y].pre=x; ranka[x]=ranka[x]+ranka[y]; }else { p[x].pre=y; ranka[y]=ranka[y]+ranka[x]; } } int main(void){int t,M,N,num,a,b,number;cin>>num;for(int i = 0;i < num;i++){cin>>N>>M;Init(N);for(int j = 0;j < M;j++){cin>>a>>b;join(a,b);}number = 0;for(int m = 1;m <= N;m++){if(p[m].pre==m) //数根节点的个数的办法。number++; }cout<<number<<endl;}return 0; }rank仿佛是标准库的一个自留词,不能用这个名字。
0 0
- ACM_HDU 1213 How Many Tables(基础并查集)
- HDOJ 1213 How Many Tables(基础并查集)
- 【hdu 1213 基础并查集】How Many Tables
- HDU 1213How Many Tables(基础并查集)
- HDU 1213 How Many Tables 基础并查集★
- How Many Tables(并查集)
- How Many Tables(并查集)
- How Many Tables(并查集)
- HDU 1213 How Many Tables(并查集)
- hdu 1213 How Many Tables(并查集练习)
- [ACM] hdu 1213 How Many Tables(并查集)
- [ACM] hdu 1213 How Many Tables(并查集)
- 杭电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 (并查集)
- Android Retrofit2+okhttp3详细配置,解决URL乱码的问题。
- c# 反射使用及详解
- 关于解决AndroidStudio自动下载第三方jar包速度慢的问题解决方法
- 堆的一些理解
- 23种设计模式-Adapter(适配器)设计模式
- ACM_HDU 1213 How Many Tables(基础并查集)
- Setter Semantics即@property中使用的修饰符
- 拷贝构造函数3.调用时机
- Java实现CRC16CCITT算法
- 嵌入式linux驱动-bus-driver-device模型笔记
- PHP 中使用 strtotime "+1 month" 时发现的坑
- make和make install的区别
- iOS客户端monkey测试
- Insert Interval区间插入题型求解 C实现