NBUT 1451 Elise (map +并查集)

  • [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

    题意: 有很多个小蜘蛛,如果小蜘蛛没有共同的兴趣就会内讧,让一个小蜘蛛培养一个兴趣需要一个单位的花费,为了让小蜘蛛们不内讧,需要给部分蜘蛛培养兴趣,求最少花费?

    分析:属于集合问题,很自然的就想到了并查集,难点在于是字符串的并查集而不是数字,所有需要用到map,数据比较小,可以直接暴力。注意:蜘蛛的兴趣数可能为0。

    /*
    Author: ZXPxx
    Memory: 244 KB		Time: 296 MS
    Language: G++		Result: Accepted
    */
    
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<vector>
    #include<map>
    using namespace std;
    
    const int mx=100+5;
    int p[mx],n,m;
    
    int find(int x) {
        return p[x] == x ? x : p[x] = find(p[x]);
    }
    void Union(int a,int b) {
        int x=find(a);
        int y=find(b);
        if(x!=y) {
            p[x]=y;
        }
    }
    int main() {
        while(~scanf("%d",&n)) {
            for(int i=1; i<=n; i++)
                p[i]=i;                               //初始化并查集
            map<string,int> mat;
            vector<int> G[mx];
            mat.clear();
            int num=1,cnt=0;                          //cnt记录 兴趣为0的蜘蛛数
            for(int i=1; i<=n; i++) {
                scanf("%d",&m);
                if(m==0)
                    cnt++;
                G[i].clear();                         //清空vector
                char s[25];
                for(int j=1; j<=m; j++) {
                    scanf("%s",s);
                    if(!mat[s])
                        mat[s]=num++;               //给兴趣编号
                    G[i].push_back(mat[s]);
                }
            }
            if(cnt==n) {
                printf("%d\n",cnt);                   //如果全为0,则每个小蜘蛛都要学习  同一种兴趣
                continue;
            }
            for(int i=1; i<=n; i++)
                for(int j=0; j<G[i].size(); j++)
                    for(int k=i+1; k<=n; k++)
                        for(int l=0; l<G[k].size(); l++)
                            if(G[i][j]==G[k][l])
                                Union(i,k);         //兴趣相同,合并
            int ans=0;
            for(int i=1; i<=n; i++) {
                if(p[i]==i)
                    ans++;
            }
            printf("%d\n",ans-1);
        }
        return 0;
    }
    

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-12 11:41:25

NBUT 1451 Elise (map +并查集)的相关文章

HDU 3172 Virtual Friends (map+并查集)

These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends' friends, their friends' friends' friends, and so on), h

hihoCoder - 1066 - 无间道之并查集 (并查集 + map)

#1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息--额,说远了,总之,小Hi和小Ho决定趁着这朗朗春光出去玩. 但是刚刚离开居住的宾馆不久,抄近道不小心走入了一条偏僻小道的小Hi和小Ho就发现自己的前方走来了几个彪形大汉,定睛一看还都是地地道道的黑人兄弟!小Hi和小Ho这下就慌了神,捡肥皂事小,这一身百把来斤别一不小心葬身他乡可就没处说去了. 就在两人正举足无措之时,为首的黑叔叔

1034. Head of a Gang (30) -string离散化 -map应用 -并查集

题目例如以下: One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone call

HDU-4879-ZCC loves march(map+set+并查集)

Description On a m*m land stationed n troops, numbered from 1 to n. The i-th troop's position can be described by two numbers (xi,yi) (1<=xi<=m,1<=yi<=m). It is possible that more than one troop stationed in the same place. Then there are t mi

【日常学习】【并查集+map】codevs2639 约会计划题解

然而我居然让诸城一中悲剧机房的C++可以编译了··· 直接上题目 题目描写叙述 Description cc是个超级帅哥,口才又好.rp极高(这句话似乎降rp),又非常的幽默,所以非常多mm都跟他关系不错. 然而.最关键的是,cc可以非常好的调解各各妹妹间的关系.mm之间的关系及其复杂.cc必须严格掌握她们之间的朋友关系,好一起约她们出去,cc要是和不是朋友的两个mm出去玩.后果不堪设想-- cc仅仅掌握着一些mm之间的关系.可是cc比較聪明.他知道a和b是朋友,b和c 是朋友,那么a和c也是朋

杭电3172--Virtual Friends(并查集+map)

Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6844    Accepted Submission(s): 1952 Problem Description These days, you can do all sorts of things online. For example, you can u

[Swust OJ 772]--Friend(并查集+map的运用)

题目链接:http://acm.swust.edu.cn/problem/772/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 每个人都有朋友,朋友也有很多种,比如: 石友--情谊坚贞的朋友. 挚友--志同道合的朋友. 益友--于己有帮助的朋友. 网友--在互联网结识的朋友. 闺友--闺房中无话不谈的朋友. 君子交:指道义之交,即在道义上相互支持的朋友. 竹马之交:少年时骑竹马为戏的朋友,指自幼相交的朋友,等等. 现在dear

HDU 5441 离线处理 + 并查集

题意:给n个节点m条带权值边的无向图.然后q个问题,每次询问点对的数目,点对需要满足的条件是:1)连通:2)其路径的最大权值不能超过询问值. 分析:如果没次询问一次,dfs一次,很可能超时,因此可以用并查集.离线处理,把边按权值排序,把问题按大小排序.然后离线的过程就是不断向图中加边的过程. 比如样例如下: 然后离线处理,排完序后将会是一条一条的加边:问题也排了序,因此是个累加过程... 1 #include <cstdio> 2 #include <iostream> 3 #in

并查集+贪心 HDU4424

先对边进行从大到小的排序 并查集保存节点数和长度 Conquer a New Region Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1289    Accepted Submission(s): 429 Problem Description The wheel of the history rolling forward, o