1034 Head of a Gang
2016-03-06 10:40
441 查看
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
解题思路:一道并查集的题目,名字用hash的办法变成int类型,所以开个26*26*26的s数组就行。用一个数组记录每个人总的通话时间,用于最后找出head。并查集找出团队,再在团队中找到通话时间最长的人就是结果。因为结果需要按字母序输出,所以我先将结果保存下来,排序之后再输出的。
做题的时候并查集的压缩路径的地方写错了,导致测试点2 4一直答案错误,还是太粗心。
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
解题思路:一道并查集的题目,名字用hash的办法变成int类型,所以开个26*26*26的s数组就行。用一个数组记录每个人总的通话时间,用于最后找出head。并查集找出团队,再在团队中找到通话时间最长的人就是结果。因为结果需要按字母序输出,所以我先将结果保存下来,排序之后再输出的。
做题的时候并查集的压缩路径的地方写错了,导致测试点2 4一直答案错误,还是太粗心。
#include<iostream> #include<stdio.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; int group[26 * 26 * 26]; int ti[26 * 26 * 26]; vector<int> groupCount[26 * 26 * 26]; struct res{ int name; int size; }; bool cmp(const res&r1, const res&r2){ return r1.name < r2.name; } int find(int x){ int res = x; while (group[res] != res){ res = group[res]; } while (x != res){ int temp = group[x]; group[x] = res; x = temp; } return res; } void join(int x, int y){ int fx = find(x); int fy = find(y); if (fx != fy){ group[fy] = fx; } } int main(){ for (int n, k; scanf("%d%d", &n, &k) != EOF;){ for (int i = 0; i < 26 * 26 * 26; i++){ group[i] = i; ti[i] = 0; groupCount[i] = vector<int>(); } for (int i = 0; i < n; i++){ char p1[4]; char p2[4]; int temp; scanf("%s %s %d", p1, p2, &temp); int index1 = (p1[0] - 'A') * 26 * 26 + (p1[1] - 'A') * 26 + (p1[2] - 'A'); int index2 = (p2[0] - 'A') * 26 * 26 + (p2[1] - 'A') * 26 + (p2[2] - 'A'); join(index1, index2); ti[index1] += temp; ti[index2] += temp; } for (int i = 0; i < 26 * 26 * 26; i++){ find(i); } for (int i = 0; i < 26 * 26 * 26; i++){ if (ti[i]>0){ groupCount[group[i]].push_back(i); } } vector<res>out; for (int i = 0; i < 26 * 26 * 26; i++){ if (groupCount[i].size() > 2){ int name = groupCount[i][0]; int maxTime = ti[groupCount[i][0]]; int totalTime = 0; for (int j = 0; j < groupCount[i].size(); j++){ totalTime += ti[groupCount[i][j]]; if (ti[groupCount[i][j]] >= maxTime){ maxTime = ti[groupCount[i][j]]; name = groupCount[i][j]; } } if (totalTime > k*2){ res temp; temp.name = name; temp.size = groupCount[i].size(); out.push_back(temp); } } } sort(out.begin(), out.end(), cmp); printf("%d\n", out.size()); for (int i = 0; i < out.size(); i++){ printf("%c%c%c %d\n", (char)(out[i].name / (26 * 26) + 'A'), (char)((out[i].name % (26 * 26)) / 26 + 'A'), (char)(out[i].name % 26 + 'A'), out[i].size); } } return 0; }
相关文章推荐
- java 线程的几种状态
- [LeetCode] Bulb Switcher
- 一年经验IOS开发者的面试记录(问题涵盖比较广)
- 简析ARP解析过程
- JDBC中遇到的一些错误及其解决办法
- 《软件架构设计.第二版解析软件架构概念》阅读笔记——到底什么是软件架构?
- 在服务器上创建git仓库
- 从资源到科研再到工作
- HTMl5的sessionStorage和localStorage
- Tomcat之一系列新手问题全解决
- 嵌入式硬件和软件的一些基础知识:
- lintcode-easy-Reverse Linked List
- MongoDB监控项--如何使用命令得到
- jmp_buf的使用,结构定义为数组
- HDU 5637 Transform 搜索
- 工厂模式理解
- 自学 java 笔记 day9(继承下)
- leetcode:Best Time to Buy and Sell Stock II 【Java】
- CentOS 7 配置163源
- linux安全第二周总结