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 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

分析
我用的是邻接表法建图map<string,vector<string>>,然后用dfs来遍历连通分量;注意最后求每个连通分量weight总和时要除以2;
 #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int num=,sum;
string head;
map<string,int> result;
map<string,vector<string>> G;
map<string,int> weight;
map<string,int> visited;
void dfs(string s){
visited[s]=;
num++;
sum+=weight[s];
if(weight[s]>weight[head]) head=s;
for(int i=;i<G[s].size();i++)
if(visited[G[s][i]]==)
dfs(G[s][i]);
}
int main(){
int n,k,weigh,tag=,temp=;
string s1,s2;
cin>>n>>k;
for(int i=;i<n;i++){
cin>>s1>>s2>>weigh;
weight[s1]+=weigh; weight[s2]+=weigh;
G[s1].push_back(s2);
G[s2].push_back(s1);
visited[s1]=visited[s2]=;
}
auto it=visited.begin();
for(;it!=visited.end();it++){
if(it->second==){
num=sum=;
head=it->first;
dfs(it->first);
}
if(num>&&sum/>k)
result[head]=num;
}
cout<<result.size()<<endl;
for(auto itt=result.begin();itt!=result.end();itt++)
cout<<itt->first<<" "<<itt->second<<endl;
return ;
}

PAT 1034. Head of a Gang的更多相关文章

  1. PAT 1034 Head of a Gang[难][dfs]

    1034 Head of a Gang (30)(30 分) One way that the police finds the head of a gang is to check people's ...

  2. PAT 1034. Head of a Gang (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...

  3. PAT 1034. Head of a Gang[bug]

    有一个两分的case出现段错误,真是没救了,估计是要写bfs的形式,可能栈溢出了 #include <cstdio> #include <cstdlib> #include & ...

  4. PAT甲级1034. Head of a Gang

    PAT甲级1034. Head of a Gang 题意: 警方找到一个帮派的头的一种方式是检查人民的电话.如果A和B之间有电话,我们说A和B是相关的.关系的权重被定义为两人之间所有电话的总时间长度. ...

  5. pat 甲级 1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

  6. PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

    1034 Head of a Gang (30 分)   One way that the police finds the head of a gang is to check people's p ...

  7. pat 甲级 1034 ( Head of a Gang )

    1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's pho ...

  8. 1034 Head of a Gang (30 分)

    1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's pho ...

  9. PAT甲级1034 Head of a Gang【bfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...

随机推荐

  1. 数据分析在web交互设计中的作用 页面跳出率 100% 原因分析

    通过分析访问的路径,发现,访问者访问其他页面,直接跳出 页面跳出率  100% 说明: 连作者都发现的导航路径不清晰 对导航进行改版:清晰.明了

  2. go语言笔记——包的概念本质上和java是一样的,通过大小写来区分private,fmt的Printf不就是嘛!

    示例 4.1 hello_world.go package main import "fmt" func main() { fmt.Println("hello, wor ...

  3. smarty用法

    smarty学习指南 在smarty的模板设计部分我简单的把smarty在模板中的一些常用设置做了简单的介绍,这一节主要来介绍一下如何在smarty中开始我们程序设计.下载Smarty文件放到你们站点 ...

  4. 过河 2005年NOIP全国联赛提高组(离散化+dp)

    1105 过河 2005年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description 在河上有一 ...

  5. 互斥的数(hash)

    1553 互斥的数  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold     题目描述 Description 有这样的一个集合,集合中的元素个数由给定的N决定, ...

  6. codevs1026商务旅行

    1036 商务旅行  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 某首都城市的商人要经常到各城镇去做 ...

  7. JavaScript--什么是函数

    函数是完成某个特定功能的一组语句.如没有函数,完成任务可能需要五行.十行.甚至更多的代码.这时我们就可以把完成特定功能的代码块放到一个函数里,直接调用这个函数,就省重复输入大量代码的麻烦. 如何定义一 ...

  8. 如何获取<a>标签的Id

    案例: <a id='213' href='javascript:void(0);'onclick=DealFun(this.Id);>删除</a> 解决方案: functio ...

  9. MySQL简明教程---级联约束

  10. C#学习-执行存储过程

    使用存储的优点 1.执行更快.直接写sql脚本会有个解析编译的过程. 2.修改方便.当业务改变时,只需要改存储过程,不需要修改C#代码 3.传递Sql脚本数据相对更小 缺点: 1.使用存储过程,数据库 ...