题目大意:邀请n个人,要几张桌子,桌子特别大,多少人都坐的过。同一张桌子必须都是朋友,朋友的朋友也是朋友。
解题思路:与HDU1232几乎是一样的。
ac代码:
#include <iostream>
#include <set>
using namespace std;
int t, n, m, pre[1005], t1, t2;
set <int>se;
int find(int x)
{int r=x, temp;while (r != pre[r])r = pre[r];while (r != x){temp = pre[x];pre[x] = r;x = temp;}
return r;
}
void join(int x, int y)
{int fx=find(x), fy=find(y);if (fx != fy)pre[fx] = fy;
}
int main()
{scanf("%d", &t);while (t--){scanf("%d%d", &n, &m);for (int i=1; i<=n; i++)pre[i] = i;for (int i=0; i<m; i++){scanf("%d%d", &t1, &t2); join(t1, t2);}for (int i=1; i<=n; i++)se.insert(find(i));printf("%d\n", se.size());se.clear();}
return 0;
}