hdu1213 How Many Tables
来源:互联网 发布:雅思7分有多难 知乎 编辑:程序博客网 时间:2024/05/20 20:47
#include <stdio.h>
#include <cstdio>
#include <string.h>
#include <cstring>
using namespace std;
const int N = 1000;
int pre[N];
int findd(int x)
{
int r = x;
while(r != pre[r])
r = pre[r];
int i = x, j;
if(i != r)
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;
}
int main()
{
// freopen("in.txt", "r", stdin);
int i, N, M, p1, p2, f1, f2, total, num;
scanf("%d", &num);
while(num --)
{
scanf("%d%d", &N, &M);
total = N;
for(i = 1; i <= N; i ++)
pre[i] = i;
while(M --)
{
scanf("%d%d", &p1, &p2);
f1 = findd(p1);
f2 = findd(p2);
if(f1 != f2)
{
pre[f2] = f1;
total --;
}
}
printf("%d\n", total);
}
return 0;
#include <cstdio>
#include <string.h>
#include <cstring>
using namespace std;
const int N = 1000;
int pre[N];
int findd(int x)
{
int r = x;
while(r != pre[r])
r = pre[r];
int i = x, j;
if(i != r)
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;
}
int main()
{
// freopen("in.txt", "r", stdin);
int i, N, M, p1, p2, f1, f2, total, num;
scanf("%d", &num);
while(num --)
{
scanf("%d%d", &N, &M);
total = N;
for(i = 1; i <= N; i ++)
pre[i] = i;
while(M --)
{
scanf("%d%d", &p1, &p2);
f1 = findd(p1);
f2 = findd(p2);
if(f1 != f2)
{
pre[f2] = f1;
total --;
}
}
printf("%d\n", total);
}
return 0;
}
心得:并查集水
0 0
- HDU1213:How Many Tables
- HDU1213--How Many Tables
- hdu1213-How Many Tables
- hdu1213 How Many Tables
- hdu1213 How Many Tables
- HDU1213 How Many Tables
- hdu1213 How Many Tables
- How Many Tables hdu1213
- HDU1213---How Many Tables
- hdu1213 How Many Tables
- hdu1213 How Many Tables
- HDU1213 How Many Tables
- hdu1213 How Many Tables
- [hdu1213] How Many Tables
- HDU1213-How Many Tables
- hdu1213 How Many Tables
- HDU1213 How Many Tables
- HDU1213 How Many Tables
- USACO-Section 3.1 Contact(枚举)
- 9. Swift 中的函数
- 【校内互测】陈老师搬书
- Python注释
- Java读取properties文件中文乱码问题解决
- hdu1213 How Many Tables
- HDU1754线段树
- 排版题 3
- mysql根据select查询结果 循环更改
- ionic 开发小问题集锦
- android-Building a Notification,Preserving Navigation when Starting an Activity
- AlertDialog编辑框无法调出输入法、锁定Activity方向、自定义进度条的WebView基础使用
- 一个对象怎么调用另一个对象, 打jar包,到底调用哪一个方法, 匿名对象, 空对象
- POJ2503字典树