zoukankan      html  css  js  c++  java
  • NBUT 1451 Elise (暴力+并查集)

    • [1451] Elise

    • 时间限制: 1000 ms 内存限制: 65535 K
    • 问题描述
    • Elise is the Spider Queen. She has a skill, Spider Form(蜘蛛形态).

       

      When she transformed to the spider, there will be some small spiders around her.

       

      But she has a problem - the small spiders will have infighting because of less common interest. So Elise decided to train their interesting.

       

      At least, they must have common interest no matter directly or indirectly.

       

      How to train theirs interest in least cost? We assume that train a interest for a spider cost 1 strength and there are at most 100 interests in total.

    • 输入
    • This problem contains several cases.
      The first line of each case is an integer N (1 ≤ N ≤ 100), indicates the number of spiders.
      Then N lines followed.
      The ith line contains an integer Ti (0 ≤ Ti ≤ 100) that indicates the number of this spider's interest and Ti strings indicate the interests. You can assume there are only lowercase letters in the strings and no longer than 20.
    • 输出
    • For each case, you should output the least cost.
    • 样例输入
    • 5
      1 kill
      1 kill
      2 sleep sing
      2 sing fart
      1 fart
    • 样例输出
    • 1
    • 提示
    • In this case, the spider 1 or 2 just need to learn to sleep or sing or fart. So it's 1.
      
    • 来源
    • XadillaX

    题意: 蜘蛛女皇-伊莉丝有很多个小蜘蛛,如果小蜘蛛没有共同的兴趣爱好的话就会内讧,为了让小蜘蛛们不内哄,伊莉丝能够花费1点体力让一个小蜘蛛培养一个兴趣。求最少花费的体力是多少?

    分析:因为涉及到集合,所以就想到了并查集,给每一个兴趣编号,每个小蜘蛛也有一个编号,暴力枚举每一个小蜘蛛的兴趣爱好,如果有其他的蜘蛛和这个蜘蛛有共同的兴趣爱好,那么就把这两个蜘蛛合并到一个集合里面去,那么最后的答案就是,所有集合个数-1

    这已经算是最暴力的解法了O(n^4),但是如果用set在查找相同兴趣的地方优化一下,能够将复杂度降到O(n^3)

    可能是数据水了,就让我水过去了

    有个要注意的地方就是兴趣可能为0

    #pragma comprint(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<string>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<map>
    #include<stdlib.h>
    #include<time.h>
    #include<algorithm>
    #define LL __int64
    #define FIN freopen("in.txt","r",stdin)
    const int MAXN=100+5;
    using namespace std;
    map<string,int> mat;
    vector<int> G[MAXN];
    char str[20+5];
    int p[MAXN];
    int findfa(int x)
    {
        return p[x]==x?x:p[x]=findfa(p[x]);
    }
    int main()
    {
        int n,m;
        while(scanf("%d",&n)!=EOF)
        {
            mat.clear();
            int tot=1,cnt=0;                          //cnt记录兴趣为0的个数
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&m);
                if(m==0) cnt++;
                G[i].clear();                         //清空vector
                p[i]=i;                               //初始化并查集
                for(int j=1;j<=m;j++)
                {
                    scanf("%s",str);
                    if(!mat[str]) mat[str]=tot++;     //给兴趣编号
                    G[i].push_back(mat[str]);
                }
            }
            if(cnt==n) {printf("%d
    ",cnt);continue;} //如果全为0,则每个小蜘蛛都要学习
            for(int i=1;i<=n;i++)
            {
                if(G[i].size()==0) continue;
                for(int j=0;j<G[i].size();j++)
                {
                    for(int k=1;k<=n;k++)
                    {
                        if(i==k) continue;
                        if(G[k].size()==0) continue;
                        for(int l=0;l<G[k].size();l++)
                        {
                            if(G[i][j]==G[k][l])
                            {
                                int x=findfa(i);
                                int y=findfa(k);
                                if(x!=y)
                                {
                                    p[x]=y;
                                }
                            }
                        }
                    }
                }
            }
            int ans=0;
            for(int i=1;i<=n;i++)
            {
                if(p[i]==i) ans++;
            }
            printf("%d
    ",ans-1);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    tomcat 登录主页成功 点击Manager App 401 等问题
    servlet
    jsp 记录1 bs/cs
    java jar
    Java 第四课 对象 类
    java 第五课 异常
    Java 第三课 数组排序
    java 第二课 标识符
    java 第一课 笔记
    maven 项目问题集锦
  • 原文地址:https://www.cnblogs.com/clliff/p/4749286.html
Copyright © 2011-2022 走看看