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
解题思路:找连通分量,并找出每个连通分量中权重最大的节点。
代码如下:
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; }
相关文章推荐
- Java中的左移右移<<、>>、>>>
- ZOJ 3693 Happy Great BG 要加eps
- GM(1,1)程序设计
- android入门(六) UI线程阻塞及其优化
- Java第一周:1.1
- TCP客户端和服务器介绍
- css3中-webkit-text-size-adjust详解
- uva 11988 - Broken Keyboard (a.k.a. Beiju Text)----链表快速插入
- phpExcel中文帮助手册
- Java实现文件的读写,复制
- CSS 让标点符号不出现在行首
- HDU 1024 Max Sum Plus Plus(二维数组转化为一维数组)
- [JavaWeb基础] 016.Struts2 国际化配置
- 【FLEX4.6从浅入深,】比4.5内容增加了很多新的属性
- [LinkerScript.7] 简单的链接器脚本命令: 区域别名 - Simple Linker Script Commands : Region Alias
- 快速掌握iOS开发 - 多线程开发技巧
- 查询oracle中所有用户信息
- 一 、前言
- angular attrs.$observe和$scope.$watch的区别
- Java第一周:1.17