PAT甲级 —— 1034 Head of a Gang (30分)

一、题目

  • 题目链接: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 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 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

  • 题意说明:给出若干个人之间的通话记录,每条记录可以看做有向边,构成一张图。图可能不连通,而是分成若干组。每个组内各成员间的通话总时长若超过给定阈值K且人数大于2人,则认为这组人是一个“团伙”,而其中和别人通话总时长最多的人是“头目”。现要输出团伙数量、每个团伙的人数和头目姓名

二、三种解法

0. BFS和DFS遍历图

1. 初始思路:邻接表+有向图+DFS

  • 初始思路:

    1. 根据样例可以看出,通话记录是有方向的,A向B的通话总时长和B向A的通话总时长是两条不同的记录,于是可以把每条记录看成一个带权有向边,建立通话关系图。接下来用套DFS或BFS遍历模板,找出所有连通块计算总通话时间
    2. 邻接表适合存储有向边边权,使用邻接表存图。同时此题中还要存储每个人的通话总时长(用来找头目),可以单独设置一个数组存点权。在输入记录时,每条记录都把通话双方的点权同时增大
    3. 最后输出要求按头目名称字母顺序排列,可以用map<string,int>自动排序
  • 满分代码

    #include<iostream>
    #include<vector>
    #include<map>
    #include<string>
    using namespace std;
    
    typedef struct NODE
    {
    	int v;	//端点 
    	int w;	//边权 
    	
    	NODE(int _v,int _w):v(_v),w(_w){}
    }NODE;
    
    const int MAXN = 2010;		//通话记录1000条,人数最多可能2000 
    bool vis[MAXN] = {false}; 	//访问标记 
    int weight[MAXN] = {0};		//每个人的权重(点权)
    vector<NODE> Adj[MAXN];		//邻接表 
    
    map<string,int> mp;			//人名	->	邻接表下标 
    string int2string[MAXN];	//邻接表下标  ->  人名
    
    int N,K,personNum = 0;		//记录数、权重阈值、总人数 
    map<string,int> gang;		//找出的团伙头目和人数,map自动按string字母有序 
    
    //把人名转换为Adj邻接表下标返回 
    int string2int(string str)
    {
    	//如果是新人,分配一个下标 
    	if(mp.find(str) == mp.end())
    	{
    		mp[str] = personNum;
    		int2string[personNum] = str;
    		personNum++;
    	}
    	return mp[str];
    }
    
    //DFS遍历nowVisit所在的连通块,计算总权重、人数、头目 
    void DFS(int nowVisit,int &head,int &gangMember,int &totalWeight)
    {
    	gangMember++;
    	vis[nowVisit] = true;
    	if(weight[nowVisit] > weight[head])
    		head = nowVisit;
    	
    	//找到和nowVisit通话的所有人,继续DFS
    	for(int i=0;i<Adj[nowVisit].size();i++)
    	{
    		totalWeight+=Adj[nowVisit][i].w;
    		//cout<<"cheak:"<<int2string[nowVisit]<<" "<<int2string[Adj[nowVisit][i].v]<<" "<<totalWeight<<'\n';
    		if(!vis[Adj[nowVisit][i].v])			
    			DFS(Adj[nowVisit][i].v,head,gangMember,totalWeight);					
    	} 
    }
    
    //访问所有连通块,每块是一个可能的gang 
    void DFSTrave()
    {
    	for(int i=0;i<personNum;i++)
    	{
    		if(!vis[i])
    		{
    			int head = i,gangMember = 0,totalWeight=0;
    			DFS(i,head,gangMember,totalWeight);
    			
    			if(gangMember>2 && totalWeight>K)
    				gang[int2string[head]] = gangMember;	
    		}	
    	} 
    	
    }
    
    int main()
    {
    	cin>>N>>K;
    	
    	string personA,personB;
    	int a,b,time;
    	
    	//遍历N条通话记录,初始化邻接表和权重表 
    	for(int i=0;i<N;i++)
    	{
    		//personA向personB打了time分钟电话 
    		cin>>personA>>personB>>time;
    		a = string2int(personA);
    		b = string2int(personB);
    		weight[a] += time;
    		weight[b] += time;				
    		Adj[a].push_back(NODE(b,time));	//在邻接表中记录一条有向边 
    	}
    	
    	//DFS遍历所有联通块,找出所有gang 
    	DFSTrave();
    	
    	//输出gang信息 
    	cout<<gang.size()<<endl;
    	for(map<string,int>::iterator it = gang.begin();it!=gang.end();it++)
    		cout<<it->first<<" "<<it->second<<endl;	
    	
    	return 0;	
    } 
    /*
    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
    */ 
    

2. 修改思路:邻接矩阵+无向图+DFS

  • 上面的代码虽然满分了,但是有一个潜在问题!!我把每条通话记录看做有向边,因此建立出来图的每个部分不一定是强连通分量,不能保证从任意点出发都能DFS搜索到整个连通块。举例来说,示例1中AAA为头目的团伙通话网络图如下,从AAA或BBB出发都DFS能搜索到所有结点,但是从CCC出发DFS就不能搜索到AAA和BBB,这将导致DFSTrave函数把CCC看做一个单独的团伙,之后从AAA或BBB搜索时,由于CCC已做过访问标记,AAA和BBB会被看做另一个单独的团伙。
    在这里插入图片描述

  • 因此,上面能AC完全是因为运气好,DFS过程正好是从能访问整个团伙成员的点开始的

  • 为了保证每个团伙都是有向图中的一个强连通分量,就不得不为每条通话记录建两条有向边,这样就很麻烦了。所以还不如改成无向图用邻接矩阵存储,修改后满分代码如下

    #include<iostream>
    #include<vector>
    #include<map>
    #include<string>
    using namespace std;
    
    const int MAXN = 2010;		//通话记录1000条,人数最多可能2000 
    bool vis[MAXN] = {false}; 	//访问标记 
    int weight[MAXN] = {0};		//每个人的权重 
    int G[MAXN][MAXN] = {0};	//邻接矩阵 
     
    map<string,int> mp;			//人名	->	邻接表下标 
    string int2string[MAXN];	//邻接表下标  ->  人名
    
    int N,K,personNum = 0;		//记录数、权重阈值、总人数 
    map<string,int> gang;		//找出的团伙头目和人数,map自动按string字母有序 
    
    //把人名转换为邻接矩阵下标返回 
    int string2int(string str)
    {
    	//如果是新人,分配一个下标 
    	if(mp.find(str) == mp.end())
    	{
    		mp[str] = personNum;
    		int2string[personNum] = str;
    		personNum++;
    	}
    	return mp[str];
    }
    
    //DFS遍历一个连通块,计算总权重、人数、头目 
    void DFS(int nowVisit,int &head,int &gangMember,int &totalWeight)
    {
    	gangMember++;
    	vis[nowVisit] = true;
    	if(weight[nowVisit] > weight[head])
    		head = nowVisit;
    	
    	for(int i=0;i<personNum;i++)
    	{
    		if(G[nowVisit][i] > 0)	//从nowVisit可以到达i
    		{
    			totalWeight += G[nowVisit][i];
    			G[nowVisit][i] = G[i][nowVisit] = 0;	//删除这条边,避免回头
    			
    			//cout<<"cheak:"<<int2string[nowVisit]<<" "<<int2string[i]<<" "<<totalWeight<<'\n';
    			if(!vis[i])			
    				DFS(i,head,gangMember,totalWeight);			
    		} 
    	} 
    }
    
    //访问所有连通块,每块是一个可能的gang 
    void DFSTrave()
    {
    	for(int i=0;i<personNum;i++)
    	{
    		if(!vis[i])
    		{
    			int head = i,gangMember = 0,totalWeight=0;
    			DFS(i,head,gangMember,totalWeight);
    			
    			if(gangMember>2 && totalWeight>K)
    				gang[int2string[head]] = gangMember;	
    		}	
    	} 
    	
    }
    
    int main()
    {
    	cin>>N>>K;
    	
    	string personA,personB;
    	int a,b,time;
    	
    	//遍历N条通话记录,初始化邻接表和权重表 
    	for(int i=0;i<N;i++)
    	{
    		//personA向personB打了time分钟电话 
    		cin>>personA>>personB>>time;
    		a = string2int(personA);
    		b = string2int(personB);
    		weight[a] += time;
    		weight[b] += time;				
    		G[a][b] += time;
    		G[b][a] += time; 
    	}
    	
    	//DFS遍历所有联通块,找出所有gang 
    	DFSTrave();
    	
    	//输出gang信息 
    	cout<<gang.size()<<endl;
    	for(map<string,int>::iterator it = gang.begin();it!=gang.end();it++)
    		cout<<it->first<<" "<<it->second<<endl;	
    	
    	return 0;	
    } 
    
  • 改用邻接表存无向图后,AAA头目的团伙示意图如下,注意一下G[nowVisit][i] = G[i][nowVisit] = 0;这句,这是为了避免从AAA开始DFS计算了AAA->BBB的边权后,在从BBB开始DFS搜索时又重复计算BBB->AAA
    在这里插入图片描述

3. 试试广度优先:邻接矩阵+无向图+BFS

  • 套广度优先模板,稍微改一下就行了

  • 满分代码

    #include<iostream>
    #include <queue>
    #include<map>
    #include<string>
    using namespace std;
    
    const int MAXN = 2010;		//通话记录1000条,人数最多可能2000 
    bool inqueue[MAXN] = {false}; //访问标记 
    int weight[MAXN] = {0};		//每个人的权重 
    int G[MAXN][MAXN] = {0};	//邻接矩阵 
     
    map<string,int> mp;			//人名	->	邻接表下标 
    string int2string[MAXN];	//邻接表下标  ->  人名
    
    int N,K,personNum = 0;		//记录数、权重阈值、总人数 
    map<string,int> gang;		//找出的团伙头目和人数,map自动按string字母有序 
    
    //把人名转换为邻接矩阵下标返回 
    int string2int(string str)
    {
    	//如果是新人,分配一个下标 
    	if(mp.find(str) == mp.end())
    	{
    		mp[str] = personNum;
    		int2string[personNum] = str;
    		personNum++;
    	}
    	return mp[str];
    }
    
    //BFS遍历一个连通块,计算总权重、人数、头目 
    void BFS(int s,int &head,int &gangMember,int &totalWeight)
    {
    	queue<int> q;
    	q.push(s);
    	inqueue[s] = true;
    	while(!q.empty())
    	{
    		int top = q.front();
    		q.pop();
    		gangMember++;
    		if(weight[top]>weight[head])
    			head = top;
    			
    		for(int i=0;i<personNum;i++)
    		{
    			if(G[top][i] > 0)	//从nowVisit可以到达i
    			{
    				totalWeight += G[top][i];
    				G[top][i] = G[i][top] = 0;	//删除这条边,避免回头
    				
    				//cout<<"cheak:"<<int2string[nowVisit]<<" "<<int2string[i]<<" "<<totalWeight<<'\n';
    				if(!inqueue[i])		
    				{
    					q.push(i);
    					inqueue[i] = true;	
    				}			
    			} 	
    		}
    	}  
    }
    
    //访问所有连通块,每块是一个可能的gang 
    void BFSTrave()
    {
    	for(int i=0;i<personNum;i++)
    	{
    		if(!inqueue[i])
    		{
    			int head = i,gangMember = 0,totalWeight=0;
    			BFS(i,head,gangMember,totalWeight);
    			
    			if(gangMember>2 && totalWeight>K)
    				gang[int2string[head]] = gangMember;	
    		}	
    	} 
    	
    }
    
    int main()
    {
    	cin>>N>>K;
    	
    	string personA,personB;
    	int a,b,time;
    	
    	//遍历N条通话记录,初始化邻接表和权重表 
    	for(int i=0;i<N;i++)
    	{
    		//personA向personB打了time分钟电话 
    		cin>>personA>>personB>>time;
    		a = string2int(personA);
    		b = string2int(personB);
    		weight[a] += time;
    		weight[b] += time;				
    		G[a][b] += time;
    		G[b][a] += time; 
    	}
    	
    	//BFS遍历所有联通块,找出所有gang 
    	BFSTrave();
    	
    	//输出gang信息 
    	cout<<gang.size()<<endl;
    	for(map<string,int>::iterator it = gang.begin();it!=gang.end();it++)
    		cout<<it->first<<" "<<it->second<<endl;	
    	
    	return 0;	
    } 
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云端FFF

所有博文免费阅读,求打赏鼓励~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值