PAT(图)——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. 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

题目大意:

给出图的边权和点权,求出节点数大于2且边权之和大于k的连通分量,输出个数和点权最大的点。

题目解析:

第一次做连通图的题目,花了2个小时才解决这道思路清晰的题。

具体代码:

#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
#define MAXN 2005
int people_num=0;//连通分量个数
map<string,int> m;
map<int,string> m0;
int A[MAXN][MAXN]= {0}; //存放人之间联系
int T[MAXN]= {0}; //存放每个人通话时间
bool vis[MAXN]= {false};
vector<int> res;//存放满足条件的团伙序号
struct node {
	int alltime;//总通话时长
	int leader;//头目
	int maxtime;//最大通话时长
	int num;//人数
} gang[MAXN];
int gang_num=0;
void dfs(int v) {
	if(vis[v]==false)
		gang[gang_num].num++;//把i放到第gang_num个团伙
	vis[v]=true;
	if(T[v]>gang[gang_num].maxtime) {
		gang[gang_num].leader=v;
		gang[gang_num].maxtime=T[v];
	}
	for(int i=0; i<people_num; i++)
		if(A[v][i]!=0) {
			gang[gang_num].alltime+=A[v][i];
			A[v][i]=A[i][v]=0;//用完了这条线就不需要考虑了
			dfs(i);
		}
}
bool cmp(int a,int b) {
	return m0[gang[a].leader]<m0[gang[b].leader];
}
int main() {
	int n,k;
	cin>>n>>k;
	for(int i=0; i<n; i++) {
		string a,b;
		int time;
		cin>>a>>b>>time;
		if(m.find(a)==m.end()) { //建立人名到数字的映射
			m[a]=people_num;
			m0[people_num]=a;
			people_num++;
		}
		if(m.find(b)==m.end()) {
			m[b]=people_num;
			m0[people_num]=b;
			people_num++;
		}
		A[m[a]][m[b]]+=time;
		A[m[b]][m[a]]+=time;
		T[m[a]]+=time;
		T[m[b]]+=time;
	}
	for(int i=0; i<people_num; i++)
		if(vis[i]==false) {//如果这个人没有访问到
			dfs(i);
			gang_num++;
		}
	for(int i=0; i<gang_num; i++)
		if(gang[i].num>2&&gang[i].alltime>k)
			res.push_back(i);
	sort(res.begin(),res.end(),cmp);
	cout<<res.size()<<endl;
	for(int i=0; i<res.size(); i++)
		cout<<m0[gang[res[i]].leader]<<" "<<gang[res[i]].num<<endl;
	return 0;
}
全部评论

相关推荐

07-03 16:02
门头沟学院 Java
点赞 评论 收藏
分享
这不纯纯作弊了吗😢😢😢
编程界菜鸡:信这个的这辈子有了,这智商你靠啥都没用
你找工作的时候用AI吗?
点赞 评论 收藏
分享
牛客92804383...:在他心里你已经是他的员工了
点赞 评论 收藏
分享
Twilight_m...:表格简历有点难绷。说说个人看法: 1.个人基本情况里好多无意义信息,什么婚姻状况、健康状况、兴趣爱好、户口所在地、身份证号码、邮政编码,不知道的以为你填什么申请表呢。 2.校内实践个人认为对找工作几乎没帮助,建议换成和测开有关的项目,实在没得写留着也行。 3.工作经历完全看不出来是干什么的,起码看着和计算机没啥关系,建议加强描述,写点你在工作期间的实际产出、解决了什么问题。 4.个人简述大而空,看着像AI生成,感觉问题最大。“Python,C,C++成为我打造高效稳定服务的得力工具”、“我渴望凭借自身技术知识与创新能力,推动人工智能技术的应用发展,助力社会实现智能化转型”有种小学作文的美感。而且你确定你个人简述里写的你都会嘛?你AI这块写的什么“深入研究”,发几篇顶会的硕博生都不一定敢这么写。而且你AI这块的能力和软测也完全无关啊。个人简述建议写你对哪些技术栈、哪些语言、哪些生产工具的掌握,写的有条理些,而且最好是和测开强相关的。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务