Codeforces 278C Learning Languages(并查集)
2013-10-15 18:41
447 查看
题意抽象出来就是求联通块的个数吧,然后添加最少边使图联通。
注意所有人都不会任何语言的时候,答案是n而不是n-1。
注意所有人都不会任何语言的时候,答案是n而不是n-1。
#include<algorithm> #include<iostream> #include<cstring> #include<fstream> #include<sstream> #include<vector> #include<string> #include<cstdio> #include<bitset> #include<queue> #include<stack> #include<cmath> #include<map> #include<set> #define FF(i, a, b) for(int i=a; i<b; i++) #define FD(i, a, b) for(int i=a; i>=b; i--) #define REP(i, n) for(int i=0; i<n; i++) #define CLR(a, b) memset(a, b, sizeof(a)) #define debug puts("**debug**") #define LL long long #define PB push_back #define MP make_pair #define eps 1e-8 using namespace std; const int maxn = 111; int n, m, k, x, fa[maxn]; set<int> lg[maxn]; set<int> :: iterator it; int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); } bool check(int i, int j) { for(it=lg[i].begin(); it!=lg[i].end(); it++) if(lg[j].find(*it) != lg[j].end()) return true; return false; } int main() { scanf("%d%d", &n, &m); int cnt = 0; FF(i, 1, n+1) { fa[i] = i; scanf("%d", &k); if(k == 0) cnt++; while(k--) { scanf("%d", &x); lg[i].insert(x); } } if(cnt == n) { printf("%d\n", n); return 0; } FF(i, 1, n+1) FF(j, i+1, n+1) if(check(i, j)) { int x = findset(i), y = findset(j); if(x != y) fa[x] = y; } int ans = 0; FF(i, 1, n+1) if(fa[i] == i) ans++; printf("%d\n", ans-1); return 0; }
相关文章推荐
- Codeforces 278C Learning Languages【并查集】水题
- Codeforces 278C Learning Languages(并查集)
- codeforces 278C. Learning Languages(并查集)
- CodeForces - 731C Socks(并查集)(贪心)
- CodeForces - 698B Fix a Tree(并查集)
- codeforces 468B Two Sets 并查集
- Codeforces 437D The Child and Zoo(贪心+并查集)
- Codeforces 469D Two Sets【思维+并查集】
- CodeForces 827D Best Edge Weight (倍增 启发式合并 链剖 并查集)
- [LCT维护最小生成树 || CDQ分治 || 线段树 并查集 dfs树] Codeforces 603E #334 (Div. 1) E. Pastoral Oddities
- CodeForces 505B(并查集)
- Codeforces 506D Mr. Kitayuta's Colorful Graph 并查集+水水的分类讨论+水水的离线预处理
- codeforces 691D 并查集
- CodeForces 691D Swaps in Permutation(并查集)
- Codeforces 455E Civilization【思维+树的直径+并查集】
- CodeForces 217A Ice Skating(并查集思路)
- CodeForces - 731C Socks (并查集+贪心)
- Codeforces 734E Anton and Tree【并查集缩点||DFS缩点+树的直径】
- codeforces 687D Dividing Kingdom II 带权并查集(dsu)
- CodeForces-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合