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;
}