题目描述
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.
输入说明
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.
输入样例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
输出样例1
2
AAA 3
GGG 3
输入样例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
输出样例2
0
分析
本题首先要把名字赋予一个特定的数字id进行存储然后利用邻接矩阵对边之间的关系进行存储,可以用map<>对姓名和id还有id和姓名进行相互映射。
然后进行bfs()返回在同一集合中的总人数temp,计算其通话总时间sum和通话时间最大的人,如果temp>2同时sum>2*ts(总门槛),则属于一个帮派。
C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4;
vector<string> ansn;
set<string> sname;
map<string,int> mp; //姓名,id映射
map<int,string> ying; //id,姓名映射
int n,ts,idx,Time,g[N][N],dist[N];
int ans,psize[N];
bool st[N],bfst[N]; //st用来存储此人是否被遍历过,bfst则是判断此人是否在一轮bfs中被遍历过
int bfs(int u)
{
memset(bfst,0,sizeof bfst);
memset(dist,0,sizeof dist);
int capa=1;
st[u]=true;
bfst[u]=true;
queue<int> q;
q.push(u);
while(q.size())
{
auto t = q.front();
q.pop();
for(int i=0;i<idx;i++)
{
dist[t]+=g[t][i];
if(!bfst[i] && g[t][i])
{
q.push(i);
st[i]=true;
bfst[i]=true;
capa++;
}
}
}
if(capa<3) capa=0; //帮派人数小于3,直接返回0
return capa;
}
int main()
{
cin>>n>>ts;
string name1,name2;
for(int i=0;i<n;i++)
{
cin>>name1>>name2>>Time;
if(!sname.count(name1))
{
mp[name1]=idx;
ying[idx]=name1;
idx++;
sname.insert(name1);
}
if(!sname.count(name2))
{
mp[name2]=idx;
ying[idx]=name2;
idx++;
sname.insert(name2);
}
g[mp[name1]][mp[name2]]+=Time;
g[mp[name2]][mp[name1]]+=Time;
}
for(int i=0;i<idx;i++)
{
if(!st[i])
{
int temp=bfs(i);
if(temp) //temp非0则进行下一步运算,反之则跳过节省时间
{
double sum=0;
int maxp=-1,idxp=-1;
for(int i=0;i<idx;i++)
{
sum+=dist[i];
if(maxp<dist[i])
{
idxp=i;
maxp=dist[i];
}
}
if(sum>ts*2)
{
ansn.push_back(ying[idxp]);
ans++;
psize[idxp]=temp;
}
}
}
}
sort(ansn.begin(),ansn.end());
cout<<ans<<endl;
if(ans>0)
{
for(int i=0;i<ans;i++)
{
cout<<ansn[i]<<" "<<psize[mp[ansn[i]]]<<endl;
}
}
return 0;
}