zoukankan      html  css  js  c++  java
  • 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
    ", ans);
    52   return 0;
    53 }
    View Code
    By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
  • 相关阅读:
    Objective-C 调用C++,C
    ios项目不能再用UDID了
    xcode 4 制作静态库详解
    Icon specified in the Info.plist not found under the top level app wrapper: Icon.png
    吼吼 尬English
    Redis
    处理android 经典蓝牙发送文件时接收包的问题
    Md5加密的文件流中是否会包含其md5值
    Android gradle buid failed case
    Android GDB 调试
  • 原文地址:https://www.cnblogs.com/rausen/p/4253763.html
Copyright © 2011-2022 走看看