首页 > 代码库 > BZOJ1370 [Baltic2003]Gang团伙
BZOJ1370 [Baltic2003]Gang团伙
20多天没写题啊。。。连键盘长什么样都忘了额。。。
用这道并查集水题练手2333
1 /************************************************************** 2 Problem: 1370 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:36 ms 7 Memory:816 kb 8 ****************************************************************/ 9 10 #include <cstdio>11 #include <algorithm>12 13 using namespace std;14 const int N = 1005;15 16 int n, m, ans;17 int fa[N << 1], a[N];18 19 int read() {20 int x = 0;21 char ch = getchar();22 while (ch < ‘0‘ || ‘9‘ < ch)23 ch = getchar();24 while (‘0‘ <= ch && ch <= ‘9‘)25 (x *= 10) += ch - ‘0‘, ch = getchar();26 return x;27 }28 29 int find_fa(int x) {30 return x == fa[x] ? x : fa[x] = find_fa(fa[x]);31 }32 33 int main() {34 int i, x, y;35 char st[5];36 n = read(), m = read();37 for (i = 1; i <= n * 2; ++i) fa[i] = i;38 for (i = 1; i <= m; ++i) {39 scanf("%s", st);40 x = read(), y = read();41 if (st[0] == ‘F‘)42 fa[find_fa(x)] = find_fa(y);43 else fa[find_fa(x)] = find_fa(y + n), fa[find_fa(y)] = find_fa(x + n);44 }45 for (i = 1; i <= n; ++i)46 a[i] = find_fa(i);47 sort(a + 1, a + n + 1);48 for (i = 1; i <= n; ++i)49 if (a[i] == 1 || a[i] != a[i - 1])50 ++ans;51 printf("%d\n", ans);52 return 0;53 }
BZOJ1370 [Baltic2003]Gang团伙
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。