链接:https://www.nowcoder.com/questionTerminal/6572f529c966464c88ccf75cc0062a8c
来源:牛客网
One way that the police finds the head of a gang is to check people'sphone calls. If there is a phone call between A and B, we say that Aand B is related. The weight of a relation is defined to be the totaltime length of all the phone calls made between the two persons. A"Gang" is a cluster of more than 2 persons who are related toeach other with total relation weight being greater than a giventhreshold K. In each gang, the one with maximum total weight is thehead. Now given a list of phone calls, you are supposed to find thegangs and the heads.
输入描述:
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.
输出描述:
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.
输入
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 AAA 3 GGG 3
题目意思:
找到犯罪团伙的头目,犯罪团伙的头目是超过2个人的团伙,并且通话时间满足大于一个阈值,所以两个点之间的权重可以考虑为点权 (index,weight) 的形式。
思路:
DFS 每个连通子图,由于要求按名字的字母顺序来排序,所以考虑用map,因为map的key是排好序的(按字母升序)
还有一点,统一用name(string类型)来唯一地标记每一个人(节点)。
#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
//用map 的结构建立图中人名之间的对应关系
map<string,vector<string> >adjlist; // store the relationship with each person
map<string,int> weight;// store the weight each person
map<string,int> visited;// store the visited person
map<string,int> ans;// store the answer
int cnt,sum;
string head;
void DFS(string str)
{
visited[str]=1;
cnt++;
sum+=weight[str];
if(weight[str]>weight[head])
head=str;
else if(weight[str]==weight[head]&& str<head)
head=str;
vector<string>::iterator it;
for(it=adjlist[str].begin();it!=adjlist[str].end();it++)
{
if(visited[*it]==0)
DFS(*it);
}
}
int main()
{
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++)
{
string name1,name2;
int time;
cin>>name1>>name2>>time;
adjlist[name1].push_back(name2);
adjlist[name2].push_back(name1);
visited[name1]=0;
visited[name2]=0;
weight[name1]+=time;
weight[name2]+=time;
}
map<string,int>::iterator it;
for(it=visited.begin();it!=visited.end();it++)
{
if(visited[it->first]==0)
{
cnt=0; // 对于每一个分量,初始化时候要注意对DFS中的sum 和个数 赋 0
sum=0;
head=it->first;
DFS(it->first); //遍历每个连通分量
if(cnt > 2 && sum/2>k)
ans[head]=cnt;
}
}
cout<<ans.size()<<endl;
map<string,int>::iterator iter;
for(iter=ans.begin();iter!=ans.end();iter++)
cout<<iter->first<<" "<<iter->second<<endl;
return 0;
}