您的位置:首页 > 其它

1034. Head of a Gang (30)

2015-09-05 23:30 316 查看
原题地址:http://www.patest.cn/contests/pat-a-practise/1034

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.

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

解题思路:找连通分量,并找出每个连通分量中权重最大的节点。

代码如下:

#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>
#define MAX 2010
using namespace std;

int G[MAX][MAX];
int mark[MAX];
int gangs[MAX];
int num[MAX];
int weight[MAX];
int id[MAX];
int N,K,cou,idc;
map<string, int> nameMap;
map<int, string> name2Map;
vector<string> names;

int parse(char* _a){
return (_a[0]-'A')*26*26 + (_a[1]-'A')*26 + (_a[2]-'A');
}

void dfs(int t){
if(id[cou] == -1) id[cou] = t;
mark[t] = cou;
num[cou]++;
if(weight[t] > weight[id[cou]]) id[cou] = t;
gangs[cou] += weight[t];
for(int i=0;i<idc;i++){
if(G[i][t] && !mark[i]) {
dfs(i);
}
}
}

int main(){
freopen("1034.txt","r",stdin);

memset(G,0,sizeof(G));
memset(mark,0,sizeof(mark));
memset(gangs,0,sizeof(gangs));
memset(id,-1,sizeof(gangs));
memset(weight,0,sizeof(gangs));
memset(num,0,sizeof(num));

scanf("%d%d",&N,&K);
idc = 0;
for(int i = 0; i< N; i++){
char _a[5], _b[5];
int w;
scanf("%s %s %d",_a,_b,&w);
string a(_a);
string b(_b);
map<string, int>::iterator it = nameMap.find(a);
if(it == nameMap.end()){
nameMap[a] = idc;
name2Map[idc] = a;
idc++;
}
it = nameMap.find(b);
if(it == nameMap.end()){
nameMap[b] = idc;
name2Map[idc] = b;
idc++;
}

G[nameMap[a]][nameMap[b]] = G[nameMap[b]][nameMap[a]] = 1;
weight[nameMap[a]] += w;
weight[nameMap[b]] += w;
}
cou = 0;
int sum = 0;
for(int i = 0; i < idc; i++){
if(weight[i] && !mark[i]){
cou++;
dfs(i);
if(gangs[cou] > 2*K && num[cou] > 2) {sum++; names.push_back(name2Map[id[cou]]);}
}
}
sort(names.begin(),names.end());
printf("%d\n",sum);
for(int i = 0; i < names.size();i++){
printf("%s %d\n",names[i].c_str(),num[mark[nameMap[names[i]]]]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: