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 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. AGang” is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold 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 threshold, 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

这道题我不会。。。太难了在这里插入图片描述
看的这位大佬的

地址:https://blog.csdn.net/c_285767939/article/details/80411783?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.baidujs&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.baidujs

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <string> 
#include <vector>
using namespace std;
//结构体edge保存输入信息
struct edge{
    int from;
    int to;
    int value;
};

const int maxn=2010;            //题目说输入边的数量最多1000,最坏情况是每次都不相同那么就是2000人
int ufs[maxn],weight[maxn];     //ufs并查集数组,weight数组存储每个人的点权
int n,kk;                       //n表示输入的通话数,kk表示团体满足一个gang的阈值
int countt=0,edgecount=0,gangcount=0;   //分别表示人员总数,通话总数,满足gang团体总数
vector<edge> alledge;           //存储所有通话边的向量
int ufs_num[maxn]={1};          //表示人员i所在的团体的人员数
int ufs_total[maxn]={0};        //表示人员i所在的团体的通话总量
map<string,int> STI;            //将题目输入的人名转换成int
map<int,string> ITS;            //将代表人的int和名字的键值对

//最后要把所有head按字典序输出,使用algorithm头文件的sort函数
bool cmp(int a,int b)       
{
    string sa=ITS[a];
    string sb=ITS[b];
    for(int i=0;i<3;i++)
    {
        char ca=sa[i];
        char cb=sb[i];
        if(ca!=cb) return ca<cb;
    }
}

//并查集查找 因为是quick-find所以直接一步查找到位
int find(int k)
{
    return ufs[k];
}
//并查集合并操作,在本题里不止做合并,还有ufs_total和ufs_num数组的更新,传入的参数分别是通话记录里两个人和本次通话权值
void unionn(int a,int b,int v)
{
    int ra=find(a);
    int rb=find(b);
    if(ra==rb)               //如果两个人在同一个连通块里,那么找到该连通块所有的人,把ufs_total加上这次边的权值
    {
        for(int i=0;i<maxn;i++)
        {
            if(ufs[i]==ra) ufs_total[i]+=v;
        } 
        return;
    }
    int oldat=ufs_total[ra],oldbt=ufs_total[rb];
    int oldan=ufs_num[ra],oldbn=ufs_num[rb];
    if(weight[ra]>=weight[rb])
    {
        for(int i=0;i<maxn;i++)
        {
            if(ufs[i]==rb) {
                ufs[i]=ra;ufs_total[i]+=oldat+v;ufs_num[i]+=oldan;
            }
            else if(ufs[i]==ra){
                ufs_total[i]+=oldbt+v;ufs_num[i]+=oldbn;
            } 
        }
    } else if(weight[ra]<weight[rb]){
        for(int i=0;i<maxn;i++)
        {
            if(ufs[i]==ra) 
            {
                ufs[i]=rb;ufs_total[i]+=oldbt+v;ufs_num[i]+=oldbn;
            }
            else if(ufs[i]==rb) 
            {
                ufs_total[i]+=oldat+v;ufs_num[i]+=oldan;
            }

        }
    }
}
//在第一次读入输入数据时调用,把人名转换成int
int nametoint(string s)
{
    if(STI.find(s)!=STI.end()) return STI[s];
    else {
        STI[s]=countt;
        ITS[countt]=s;
        return countt++;
    }
}

int main()
{
    //初始化并查集,每个人指向自己
    for(int i=0;i<maxn;i++)
    {
        ufs[i]=i;
    }
    scanf("%d %d",&n,&kk);
    char a[4],b[4];
    string sa,sb;
    int ia,ib,value;
    for(int i=0;i<n;i++)  //第一次处理输入数据。至于为什么要用char数组转一次string。。因为不喜欢用cin
    {
        scanf("%s %s %d",a,b,&value);
        sa=a;sb=b;
        //把string的名字转换成int
        ia=nametoint(sa);
        ib=nametoint(sb);
        //把通话信息整合成结构体存在向量里
        edge e;edgecount++;
        e.from=ia;e.to=ib;e.value=value;
        alledge.push_back(e);
        //更新点权
        weight[ia]+=value;
        weight[ib]+=value;
    }

    fill(ufs_num,ufs_num+maxn,1);
    //第二次处理输入数据,做并查集合并
    for(int i=0;i<alledge.size();i++)
    {
        ia=alledge[i].from;
        ib=alledge[i].to;
        value=alledge[i].value;
        unionn(ia,ib,value);
    }
    int visit[maxn]={0}; //用数组简单做判断:这个团体的信息是否已经处理过
    int gangi[maxn];     //当找到一个满足条件的gang,把它的head加入到这个数组,最后再做输出,因为要按字典序,不能找到一个就直接输出
    int gangsize=0;
    for(int i=0;i<maxn;i++)
    {
        if(visit[ufs[i]]==0)
        {
            visit[ufs[i]]=1;
            if(ufs_num[ufs[i]]>2)
            {
                if(ufs_total[ufs[i]]>kk)
                {
                    gangi[gangsize++]=ufs[i]; //遍历并查集如果当前点指向的head没被处理过,就进入检查人员和通话总量是否满足条件
                }
            }
        }
    }
    sort(gangi,gangi+gangsize,cmp);//按字典序排序
    printf("%d\n",gangsize);
    for(int i=0;i<gangsize;i++)
    {
        cout<<ITS[gangi[i]]<<" "<<ufs_num[gangi[i]]<<endl;
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值