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 all the phone calls made between the two persons. A “Gang” is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threthold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.

Input Specification:
Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N lines follow, each in the following format:

Name1 Name2 Time

where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:
For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1:

8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 1:

2
AAA 3
GGG 3

Sample Input 2:

8 70
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 2:

0

解题思路

  题目大意: 给你N个通话记录,通话记录分别是通话的两个人A和B的Name,以及通话时间Time,如果两个人有通话记录,认定他们是一个团伙的,权重以通话时长为标准,比如A和B通话,B和C通话,那么A、B、C是一个团伙的,规定团伙必须超过两个人。一个团伙中,通话时间最长且超过给定阈值K的人是该团伙的老大。
  解题思路: 一个通话记录确定两个结点的通路,通话时间可以看做两者的权重,N张通话记录确定一张关系图,而一个团伙对应图中的连通分量,我们可以对整张图做深度优先搜索,确定连通分量的个数,然后判断该连通分量是否存在符合要求的大佬,统计大佬个数。、
  由于每个人的ID是通过字符串给出的,需要离散化。遍历的过程中需要实时更新权值最大的节点,使用map容器来完成对图的构建,以及使用map来对节点权值的索引;可以说此题就是在考察对map的灵活使用,当要遍历某个数据结构的时候,如果要检索的索引是字符串,就要考虑使用map,并且使用map来存储数据,天然已经按字符串的字典序排好了。最后输出的时候避免排序

/*
** @Brief:No.1034 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2018-12-11 
** @status: Accepted!
*/
#include <iostream>
#include <string.h>
#include <map>
#include <vector>
using namespace std;
 
string head;
int cnt,total;
map<string,int>weight;
map<string,bool>visit;
map<string,vector<string> >adjlist;
map<string,int>res;
 
void DFS(string start)
{
    visit[start]=1;
    cnt++;
    total+=weight[start];
    if(weight[start]>weight[head])head=start;
    vector<string>::iterator it=adjlist[start].begin();
 
    for(;it!=adjlist[start].end();it++)
    {
       if(visit[*it]==0)
       {
           DFS(*it);
       }
    }
}
int main()
{
    int N,K,T,i=0;
    cin>>N>>K;
    string member1,member2;
    while(i<N)
    {
        cin>>member1>>member2>>T;
        weight[member1]+=T;
        weight[member2]+=T;
        adjlist[member1].push_back(member2);
        adjlist[member2].push_back(member1);
        visit[member1]=0;
        visit[member2]=0;
        i++;
    }
 
    map<string,bool>::iterator ite=visit.begin();
    for(;ite!=visit.end();ite++)
    {
        if(ite->second==0)
        {
            total=0;
            cnt=0;
            head=ite->first;
            DFS(ite->first);
        }
        if(cnt>2&&total/2>K)res[head]=cnt;
    }
    map<string,int>::iterator it=res.begin();
    cout<<res.size()<<endl;
    for(;it!=res.end();it++)cout<<it->first<<" "<<it->second<<endl;
    return 0;
}

在这里插入图片描述

总结

  30分的题,确实挺难的,一下子没做出来,最后参考了@浙大太子爷 的解题博客才搞定的,不得不说,大佬的代码写的挺好的。能够灵活运用map也是牛批。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值