A1034 Head of a Gang (30分)

一、技术总结

  1. 这一题是关于图的遍历的,首先拿到题目理解题意,可以发现第一个需要考虑的问题是如何存储的问题。
  2. 然后就是考虑使用哪种遍历方法的问题,这里使用DFS遍历的方法。
  3. 然后还有就是如何存储字符串和编号的问题,使用map<string, int>,进行解决。最后就是关于统计每一个连通分量是否达标,一个是人数,一个是阀值。所以需要重新开辟一个空间来进行记录。也是使用map,用权重最大的字符串,进行对应的人数。
  4. 同时如果使用邻接矩阵,和一个bool数组用于记录结点是否被访问过是常规操作。
  5. 对于DFS函数,需要考虑的就是参数问题,这一题中一个当前结点编号为一个i参数,还有就是head作为记录权值最大的一个作为参数,在一个就是记录人数numMember作为参数,再一个就是totalValue,总权值相加看是否达到阀值。
  6. 其余细节参考代码

二、参考代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2010;
const int INF = 1000000000;
map<int, string> intToString;//编号->姓名
map<string, int> stringToInt;//姓名->编号
map<string, int> Gang;//head->人数
int G[maxn][maxn] = {0}, weight[maxn] = {0};//邻接矩阵和点权weight
int n, k, numPerson;//边数n,下限k, 总人数numPerson
bool vis[maxn] = {false};//标记是否被访问
//DFS函数访问单个连通块,nowVisit为当前访问的编号
//head为头目,numMember为成员编号,totalValue为连通块的总边权
void DFS(int nowVisit, int& head, int& numMember, int& totalValue){
    numMember++;//成员人数加一
    vis[nowVisit] = true;//标记为已访问
    if(weight[nowVisit] > weight[head]){
        head = nowVisit;
    }
    for(int i = 0; i < numPerson; i++){
        if(G[nowVisit][i] > 0){
            totalValue += G[nowVisit][i];//连通块的总边权增加该边权
            G[nowVisit][i] = G[i][nowVisit] = 0;
            if(vis[i] == false){
                DFS(i, head, numMember, totalValue);
            }
        }
    }
}
//DFSTrace函数遍历整个图
void DFSTrace(){
    for(int i = 0; i < numPerson; i++){
        if(vis[i] == false){
            int head = i, numMember = 0, totalValue = 0;//头目、成员数、总边权
            DFS(i, head, numMember, totalValue);
            if(numMember > 2 && totalValue > k){
                //head人数为numMember
                Gang[intToString[head]] = numMember;
            }
        }
    }
}
//change函数返回姓名str对应的编号
int change(string str){
    if(stringToInt.find(str) != stringToInt.end()){
        return stringToInt[str];
    }else{
        stringToInt[str] = numPerson;
        intToString[numPerson] = str;
        return numPerson++;
    }
}
int main(){
    int w;
    string str1, str2;
    cin >> n >> k;
    for(int i = 0; i < n; i++){
        cin >> str1 >> str2 >> w;
        int id1 = change(str1);
        int id2 = change(str2);
        weight[id1] += w;
        weight[id2] += w;
        G[id1][id2] += w;
        G[id2][id1] += w;
    }
    DFSTrace();
    cout << Gang.size() << endl;
    for(auto it = Gang.begin(); it != Gang.end(); it++){
        cout << it->first << " " << it->second << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/tsruixi/p/12364425.html

时间: 2024-10-10 21:18:48

A1034 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

A1034. 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 all the phone calls made b

1034 Head of a Gang (30分)

1. 题目 2. 思路 使用并查集归属集合 3. 注意点 可以使用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(fat

1034 Head of a Gang (30 分)

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 Aand B, we say that A and B is related. The weight of a relation is defined to be the total time length of

pta08-图7 公路村村通 (30分)

08-图7 公路村村通   (30分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N):随后的M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本.为简单起见,城镇从1到N编号. 输出格式: 输出村村通需要的最低成本.如果输入数据不足以保证畅通,则输出−1,表示需要建设更多公路. 输入样例: 6

PTA 07-图5 Saving James Bond - Hard Version (30分)

07-图5 Saving James Bond - Hard Version   (30分) This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of lan

5-10 公路村村通 (30分)

5-10 公路村村通   (30分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数NN(\le 1000≤1000)和候选道路数目MM(\le 3N≤3N):随后的MM行对应MM条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本.为简单起见,城镇从1到NN编号. 输出格式: 输出村村通需要的最低成本.如果输入数据不足以保证畅通,则输出-1?1,

pta5-9 Huffman Codes (30分)

5-9 Huffman Codes   (30分) In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redundancy Codes", and hence printed his name in the history of computer science. As a professor who gives the final exam problem o

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.