PAT 1034. Head of a Gang (30)
2015-08-15 14:01
399 查看
1034. Head of a Gang (30)
时间限制100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
这道题也是一个考察Union-find操作的问题,题目表意上并没有什么陷阱,因此较为简单。
代码如下:
#include <iostream> #include <string> #include <iomanip> #include <set> #include <map> #include <cstring> using namespace std; typedef struct node{ int size; string head; int value; int max; }node; map<string,int> value; map<string,string> root; map<string,node> gang; map<string,int> result; string findSet(string a) { string ta=root[a]; if(ta==a) return a; else { string tta=findSet(ta); root[a]=tta; return tta; } } void Union(string a,string b) { string ta,tb; ta=findSet(a); tb=findSet(b); root[tb]=root[ta]; } int main(void) { int N,K; cin>>N>>K; while(N--) { string a,b; int v; cin>>a>>b>>v; if(value.count(a)<=0) { value[a]=v; root[a]=a; } else value[a]+=v; if(value.count(b)<=0) { value[b]=v; root[b]=b; } else value[b]+=v; Union(a,b); } map<string,string>::iterator it1; for(it1=root.begin();it1!=root.end();it1++) { string theRoot=findSet(it1->first); int thevalue=value[it1->first]; if(gang.count(theRoot)<=0) { node temp; temp.head=it1->first; temp.size=1; temp.value=thevalue; temp.max=temp.value; gang[theRoot]=temp; } else { gang[theRoot].size++; gang[theRoot].value+=thevalue; if(gang[theRoot].max<thevalue) { gang[theRoot].head=it1->first; gang[theRoot].max=thevalue; } } } map<string,node>::iterator it2; for(it2=gang.begin();it2!=gang.end();it2++) if(it2->second.value/2>K&&it2->second.size>2) result[it2->second.head]=it2->second.size; map<string,int>::iterator it3; int num=result.size(); if(num==0) { cout<<"0"; return 0; } else cout<<num<<endl; for(it3=result.begin();it3!=result.end();it3++) cout<<it3->first<<" "<<it3->second<<endl; }
相关文章推荐
- 游戏创意 - 轰鱼
- js常用方法:
- Eclipse创建Maven Web工程并实现Tomcat热部署
- 连接池简介
- Android 设置状态栏 显示 或 隐藏 SystemUI flag
- onReceivedTitle() 没被调用的解决
- 检索(一)线性表检索、集合检索
- URAL - 1949 The Best Picture in the Galaxy
- 创建堆,堆排序的详细实现过程,C++完整代码
- Github 初次使用 commit命令提交失败
- java中的clone
- 项目规划阶段-乙方要求的东西在项目周期例会应该说明
- 海量小文件存储与Ceph实践
- 句柄类思想
- libgdx API之Actor.setZIndex():渲染有序
- hdu5386Cover 暴力枚举
- 软件列表
- 用Discuz 搭建个人论坛
- SYSTICK配置
- Delphi操作Unicode字符