1034 Head of a Gang (30分)

1. 题目

2. 思路

使用并查集归属集合

3. 注意点

  1. 可以使用map<string, string> 模拟int类型的并查集,减少了string和int类型转换的问题
    因为c++的map,值如果不存在会自动初始化
map<string, string> father; //定义

// 查找root
string findfather(string x){
    if(father[x] == ""){
        father[x] = x;
    }else{
        while(father[x] != x){
            x = father[x];
        }
    }
    return x;
}
  1. 对于集合的总权重的计算, 存放map<string, int> sum
  • 如果两个name本来就是一个节点,sum[findfather(n1)] + weight;
  • 如果两个name不是一个节点, sum[findfather(n1)] + sum[findfather(n2)] + weight

4. 代码

#include<cstdio>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>

using namespace std;

map<string, string> father;
map<string, int> weights;
map<string, int> sum;

string findfather(string x){
    if(father[x] == ""){
        father[x] = x;
    }else{
        while(father[x] != x){
            x = father[x];
        }
    }
    return x;
}

bool cmp(string s1, string s2){
    return weights[s1] > weights[s2];
}

void collect(int k){
    vector<string> res;
    map<string, set<string> > col;
    for(auto a:father){
        col[findfather(a.first)].insert(a.first);
    }
    for(auto a:col){
        if(a.second.size() > 2 && sum[findfather(a.first)] > k){
            vector<string> temp(a.second.begin(), a.second.end());
            sort(temp.begin(), temp.end(), cmp);
            res.push_back(temp[0]);
        }
    }
    sort(res.begin(), res.end(), [](string s1, string s2){
        return s1 < s2;
    });
    printf("%d\n", res.size());
    for(auto a:res){
        printf("%s %d\n", a.c_str(), col[findfather(a)].size());
    }
}

int main(){
    int n, k;
    scanf("%d %d", &n, &k);
    char n1[10], n2[10];
    int w;
    for(int i=0;i<n;i++){
        scanf("%s %s %d", n1, n2, &w);
        weights[n1] += w;
        weights[n2] += w;
        string f1 = findfather(n1);
        string f2 = findfather(n2);
        if(f1 != f2){
            sum[f1] += sum[f2];
            father[f2] = f1;
        }
        sum[f1] += w;
    }
    collect(k);
}

原文地址:https://www.cnblogs.com/d-i-p/p/12404155.html

时间: 2024-10-27 13:46:17

1034 Head of a Gang (30分)的相关文章

PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

1034 Head of a Gang (30 分) 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

1034 Head of a Gang (30)(30 分)

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 calls made b

1034. Head of a Gang (30)

注意这里n是电话数,电话数不超过1000代表人数不超过两千,...好坑.... 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

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

PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

题目 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 calls mad

A1034 Head of a Gang (30分)

一.技术总结 这一题是关于图的遍历的,首先拿到题目理解题意,可以发现第一个需要考虑的问题是如何存储的问题. 然后就是考虑使用哪种遍历方法的问题,这里使用DFS遍历的方法. 然后还有就是如何存储字符串和编号的问题,使用map<string, int>,进行解决.最后就是关于统计每一个连通分量是否达标,一个是人数,一个是阀值.所以需要重新开辟一个空间来进行记录.也是使用map,用权重最大的字符串,进行对应的人数. 同时如果使用邻接矩阵,和一个bool数组用于记录结点是否被访问过是常规操作. 对于D

PAT (Advanced Level) 1034. Head of a Gang (30)

简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<string> #include<queue> #include<stack> #include<algorithm> #include<iostream> using namespace st

PAT 1034 Head of a Gang[难]

1034 Head of a Gang (30)(30 分) 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 lengt

pat1034. Head of a Gang (30)

1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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.