博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT 1034. Head of a Gang
阅读量:4319 次
发布时间:2019-06-06

本文共 2613 字,大约阅读时间需要 8 分钟。

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 10
Sample Output 1:
2AAA 3GGG 3
Sample Input 2:
8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 2:
0 分析 我用的是邻接表法建图map
>,然后用dfs来遍历连通分量;注意最后求每个连通分量weight总和时要除以2;
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 int num=0,sum; 7 string head; 8 map
result; 9 map
> G;10 map
weight;11 map
visited;12 void dfs(string s){13 visited[s]=0;14 num++;15 sum+=weight[s];16 if(weight[s]>weight[head]) head=s;17 for(int i=0;i
>n>>k;25 for(int i=0;i
>s1>>s2>>weigh;27 weight[s1]+=weigh; weight[s2]+=weigh;28 G[s1].push_back(s2);29 G[s2].push_back(s1);30 visited[s1]=visited[s2]=1;31 }32 auto it=visited.begin();33 for(;it!=visited.end();it++){34 if(it->second==1){35 num=sum=0; 36 head=it->first;37 dfs(it->first);38 }39 if(num>2&&sum/2>k)40 result[head]=num;41 }42 cout<
<
first<<" "<
second<

 

 

转载于:https://www.cnblogs.com/A-Little-Nut/p/8300900.html

你可能感兴趣的文章
JDK工具(一)–Java编译器javac
查看>>
深入.NET框架与面向对象的回顾
查看>>
merge http://www.cplusplus.com/reference/algorithm/merge/
查看>>
Python-DB接口规范
查看>>
改变label中的某字体颜色
查看>>
[转]SQL SERVER 的排序规则
查看>>
SQLServer锁原理和锁的类型
查看>>
Eclipse中SVN的安装步骤(两种)和使用方法[转载]
查看>>
C语言函数的可变参数列表
查看>>
七牛云存储之应用视频上传系统开心得
查看>>
struts2日期类型转换
查看>>
Spark2-数据探索
查看>>
大数据初入门
查看>>
Java学习笔记-类型初始化
查看>>
设计模式原则之单一职责原则
查看>>
Android:日常学习笔记(10)———使用LitePal操作数据库
查看>>
鱼那么信任水,水却煮了鱼
查看>>
HTML5 Canvas ( 文字的度量 ) measureText
查看>>
Http和Socket连接区别
查看>>
Arrays基本使用
查看>>