本文主要是介绍【并查集】 HDU 1213 How Many Tables,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
HDU 1213 How Many Tables
纯粹的并查集
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;int father[1111];int get_fa(int x)
{if(x != father[x]) father[x] = get_fa(father[x]);return father[x];
}int main()
{int t;while(scanf("%d", &t) != EOF){while(t--){int n, m;scanf("%d%d", &n, &m);for(int i = 0; i <= n; i++)father[i] = i;while(m--){int a, b;scanf("%d%d", &a, &b);a = get_fa(a), b = get_fa(b);father[b] = a;}int cnt = 0;for(int i = 1; i <= n; i++)if(father[i] == i)cnt++;printf("%d\n", cnt);}}return 0;
}
这篇关于【并查集】 HDU 1213 How Many Tables的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!