PAT(甲级)1034
来源:互联网 发布:mysql btree索引原理 编辑:程序博客网 时间:2024/06/05 22:46
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 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 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10Sample Output 1:
2AAA 3GGG 3Sample Input 2:
8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10Sample Output 2:
0
#include <iostream>#include <map>#include <cstring>#include <algorithm>///////////////////////////////////////////////////////////using sets is more complicate than using dfs to search a graph// try grap nest timeusing namespace std;const int SIZE = 1003;int total_weight[SIZE];map <string,int> m,res;map <string,int> m_set[SIZE];void swap(int &num1,int &num2){int tmp =num1;num1 =num2;num2 = tmp;}void merge_set(int &n1,int &n2,int end){if(n1 == end){ swap(n1,n2); }/*else if(n2 != end){if(m_set[n1].size() < m_set[n2].size()){swap(n1,n2);}}*/map<string,int>::iterator iter = m_set[n2].begin();while(iter != m_set[n2].end()){m_set[n1].insert(*iter);m[iter->first] = n1;iter++;}total_weight[n1] += total_weight[n2];if(n2 !=end){iter = m_set[end].begin();m_set[n2].clear();while(iter != m_set[end].end()){m_set[n2].insert(*iter);m[iter->first] = n2;iter++;}total_weight[n2] = total_weight[end];}m_set[end].clear();total_weight[end] =0;}int main(){int N,cnt=0;int K,weight;char name1[8],name2[8];//freopen("test.txt","r",stdin);memset(total_weight,0,sizeof(total_weight));scanf("%d%d",&N,&K);for(int i=0;i<N;i++){scanf("%s%s%d",name1,name2,&weight);if(!strcmp(name1,name2)) //ignore self-call continue;map<string,int>::iterator iter1 = m.find(name1);map<string,int>::iterator iter2 = m.find(name2);int number;if( iter1== iter2){ // new set number = cnt; m.insert(make_pair(name1,cnt));m.insert(make_pair(name2,cnt)); cnt++;}else{if(iter1 == m.end()){number = iter2->second;m[name1] = number;}else if (iter2 == m.end()){number = iter1->second;m[name2] = number;}else{int number1 = iter1->second;int number2 = iter2->second;if(number1 == number2){number = number1;}else { //different sets cnt--;merge_set(number1,number2,cnt); m_set[number1][name1] += weight;m_set[number1][name2] += weight;total_weight[number1] += weight; continue;}}}total_weight[number] += weight;m_set[number][name1] += weight;m_set[number][name2] += weight;}for(int i = 0;i<cnt;i++){ // number of groupsint size = m_set[i].size();if(size > 2 && total_weight[i] > K){ map<string,int>::iterator iter = m_set[i].begin(); map<string,int>::iterator iter1 = iter; while(iter != m_set[i].end()){ //find max weight if(iter1->second < iter->second) iter1 = iter; iter++;}iter1->second = size;res.insert(*iter1);}}int size = res.size();printf("%d\n",size);map<string,int>::iterator iter = res.begin();while(iter != res.end()){printf("%s %d\n",(iter->first).c_str(),iter->second);iter++;} return 0;}
- PAT(甲级)1034
- PAT甲级1034
- PAT甲级 1034 -- 没有注释
- PAT 甲级
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- PAT(甲级)1013
- PAT(甲级)1014
- 使用scrapy-redis分布式爬虫 准备工作
- test7.20
- Light oj 1158 - Anagram Division(状压+记忆化)
- 第十章 动态选路协议
- java面试题二十一 异常
- PAT(甲级)1034
- leetcode:Sort List 使用归并排序的解决思路
- Vmware虚拟机下三种网络模式配置
- 3.3.3 Differ对象
- HDU 1181 DFS
- PAT(甲级)1035
- 静态成员函数的作用与静态数据成员的初始化
- PAT(甲级)1036
- LeetCode 19_Remove Nth Node From End of List