HDU 1213 How Many Tables(并查集模板)
生活随笔
收集整理的這篇文章主要介紹了
HDU 1213 How Many Tables(并查集模板)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://acm.hdu.edu.cn/showproblem.php?pid=1213
題意:
這個問題的一個重要規則是,如果我告訴你A知道B,B知道C,這意味著A,B,C知道對方,所以他們可以留在一個桌子。
例如:如果我告訴你A知道B,B知道C,D知道E,所以A,B,C可以留在一個桌子中,D,E必須留在另一個桌子中。所以Ignatius至少需要2個桌子。
?
思路:
并查集模板題。
#include<iostream> using namespace std;int p[1005];int find(int x) {return p[x] == x ? x : find(p[x]); }void Unions(int x, int y) {p[x] = y; }int main() {//freopen("D:\\txt.txt", "r", stdin);int T, a, b, n, m;cin >> T;while (T--){cin >> n >> m;for (int i = 1; i <= n; i++)p[i] = i;for (int i = 0; i < m; i++){cin >> a >> b;int x = find(a);int y = find(b);if (x != y)Unions(x, y);}int ans = 0;for (int i = 1; i <= n;i++)if (p[i] == i) ans++;cout << ans << endl;}return 0; }?
轉載于:https://www.cnblogs.com/zyb993963526/p/6386359.html
總結
以上是生活随笔為你收集整理的HDU 1213 How Many Tables(并查集模板)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到手机断了什么意思
- 下一篇: 第三章:多坐标系