1034. Head of a Gang (30)
2016-02-02 20:23
393 查看
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
分析通话记录,若某个团体的人数大于3.通话总时间大于K,则输出通话时间最多的head和团队人数。
总体并不难,我做题时候遇到的主要难点在于团队的通话表达,使用了map本以为妥妥超时,没想到这题对时间复杂度要求似乎不高。
本题有多种解法,可以将字符对应为数字,然后转为图,最后通过遍历解决。
我用到了遍历,但是并没有建立图,而是通过map将各个结点连接起来,本质思路还是一致的。
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
分析通话记录,若某个团体的人数大于3.通话总时间大于K,则输出通话时间最多的head和团队人数。
总体并不难,我做题时候遇到的主要难点在于团队的通话表达,使用了map本以为妥妥超时,没想到这题对时间复杂度要求似乎不高。
本题有多种解法,可以将字符对应为数字,然后转为图,最后通过遍历解决。
我用到了遍历,但是并没有建立图,而是通过map将各个结点连接起来,本质思路还是一致的。
#include<iostream> #include<algorithm> #include<map> #include<vector> #include<string> #include<queue> using namespace std; struct group{ vector<string> talked; int time; bool checked; group(){ time=0,checked=false; } void add(string b) { for(int i=0;i<talked.size();i++){ if(talked[i]==b) return; } talked.push_back(b); } }; struct boss{ string head; int groupnum; }; bool cmp(boss a,boss b){ return a.head<b.head; } int main(){ freopen("in.txt","r",stdin); int k,n; int num=0; while(scanf("%d%d",&n,&k)!=EOF){ string a,b; int time; map<string,group> mm;; for(int i=0;i<n;i++) { cin>>a>>b>>time; mm[a].time+=time; mm[b].time+=time; mm[a].add(b); mm[b].add(a); } vector<boss>heads; for(map<string,group> ::iterator it=mm.begin();it!=mm.end();it++){ if(it->second.checked==false) { int total=0; queue<string> mq; boss tboss; tboss.head=it->first; it->second.checked=true; tboss.groupnum=0; int maxtime=0; mq.push(it->first); while(!mq.empty()) { string temp=mq.front(); mq.pop(); group temp_m=mm[temp]; total+=temp_m.time; if(temp_m.time>maxtime) { tboss.head=temp; maxtime=temp_m.time; } tboss.groupnum++; for(int i=0;i<temp_m.talked.size();i++) { if(mm[temp_m.talked[i]].checked==false) { mq.push(temp_m.talked[i]); mm[temp_m.talked[i]].checked=true; } } } if(tboss.groupnum>=3&&total>2*k) { heads.push_back(tboss); } } } sort(heads.begin(),heads.end(),cmp); cout<<heads.size()<<endl; for(int i=0;i<heads.size();i++) { cout<<heads[i].head<<" "<<heads[i].groupnum<<endl; } } return 0; }
相关文章推荐
- 微软手机 能靠Surface Phone卷土重来吗?
- Spark&Spark性能调优实战
- 工作:作一个简单的选择判断
- Oracle DELETE和TRUNCATE 的区别
- 主席树学习笔记
- MySQL教程-原理篇-事务
- Servlet的一些API使用介绍
- [poj2796]Feel Good(单调栈)
- drua
- Maven学习 (一) 搭建Maven环境
- vm11下安装ubuntu14.04出现的网络问题解决方案
- UESTC 1272 Final Pan's prime numbers(乱搞)
- LeetCode:Word Pattern
- 数据库设计
- C#成神之路<6> 数据类型:浮点以及布尔详解
- poj2449 Remmarguts' Date
- IIC 驱动的注册
- 读代码大全2——第五章 软件构建中的设计
- ubuntu 安装 4.4内核版本
- Postgres 9.4 feature highlight: REPLICA IDENTITY and logical replication