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-02 17:28
25届每天都在焦虑找工作的事情0offer情绪一直很低落硬撑着面了一个岗位岗位有应酬的成分面试的时候hr给我出各种场景题问的问题比较犀利&nbsp;有点压力面的感觉感觉有点回答不上来本来就压抑的情绪瞬间爆发了呢一瞬间特别想哭觉得自己特别没用没绷住掉眼泪了事后想想觉得自己挺有病的&nbsp;真的破大防了
喜欢唱跳rap小刺猬...:我觉得没关系吧,之前有一次面试leader给我压力面,我顶住了压力,结果入职的时候发现组里氛围很差,果断跑路。其实从面试就能大概看出组的情况,面试体验好的组倒是不一定好,但是面试体验不好的组。。。就很难说
面试尴尬现场
点赞 评论 收藏
分享
陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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