Codeforces 278C Learning Languages(并查集)
2013-10-15 00:37
381 查看
题意抽象出来就是求联通块的个数吧,然后添加最少边使图联通。
注意所有人都不会任何语言的时候,答案是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 292D Connected Components(并查集 前后缀)
- 二维并查集——CodeForces - 505B Mr. Kitayuta's Colorful Graph
- 【Codeforces 741 B. Arpa's weak amphitheater and Mehrdad's 】+ 并查集 + 01背包
- CodeForces - 893C Rumor(并查集)
- CodeForces 466E Information Graph --树形转线性+并查集
- CodeForces 622C Not Equal on a Segment(并查集)
- codeforces 466E Information Graph 并查集LCA离线一通乱搞
- codeforces 593D(并查集 + LCA)
- CodeForces - 744A (并查集)
- Codeforces 25 D.Roads not only in Berland(并查集)
- 【codeforces】Misha and Changing Handles(映射+并查集)
- Codeforces 437D The Child and Zoo(贪心+并查集)
- Codeforces 445B DZY Loves Chemistry(并查集)
- 【 CodeForces 209C】 【欧拉回路推结论+并查集计算联通分量】 【给定n点m边无向图,可能有自环和重边。 问最少添加多少条边后,使得图存在从点1出发发又回到点1的欧拉回路】
- CodeForces - 731C Socks(并查集)(贪心)
- CodeForces - 698B Fix a Tree(并查集)
- 【并查集+贪心】 CodeForces - 731C - Socks