zoukankan      html  css  js  c++  java
  • B

    病毒扩散问题,SARS病毒最初感染了一个人就是0号可疑体,现在有N个学生,和M个团队,只要团队里面有一个是可疑体,那么整个团队都是可疑体,问最终有多少个人需要隔离...

    再简单不过的并查集,只需要不断的合并每一行就行可,到最后查询一个所有与0相同的树根就行了

    //////////////////////////////////////////////////////////////////
    #include<stdio.h>

    const int maxn  = 100005;

    int f[maxn];
    int Find(int x)
    {
        if(f[x] != x)
            f[x] = Find(f[x]);
        return f[x];
    }

    int main()
    {
        int N, M;

        while(scanf("%d%d", &N, &M), N+M)
        {
            int i, u, v, T;

            for(i=0; i<N; i++)
                f[i] = i;

            while(M--)
            {
                scanf("%d%d", &T, &u);
                u = Find(u);

                while(--T)
                {
                    scanf("%d", &v);
                    v = Find(v);
                    f[v] = u;
                }
            }

            int ans = 1;
            u = Find(0);

            for(i=1; i<N; i++)
            {
                if(Find(i) == u)
                    ans++;
            }

            printf("%d ", ans);
        }

        return 0;
    }
  • 相关阅读:
    Tomcat压缩传输设置
    Spring事务的开启方式
    Transactional参数说明
    Java获取异常堆栈信息
    ElasticSearch的matchQuery与termQuery区别
    156-PHP strrpos和strripos函数
    155-PHP stripos函数
    154-PHP strpos函数
    153-PHP htmlentities函数
    152-PHP htmlspecialchars函数
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4667592.html
Copyright © 2011-2022 走看看