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. java 定位工具

    #查看JVM所有进程及启动类信息以及PID jps -mlvV #查看JVM运行各种状态信息,包括GC,类加载,堆内存信息,jit编译信息等jstat -gcutil <PID> (堆内存 ...

  2. hbase查询_Phoenix及hbase repl命令行两种方式

    一.Phoenix(jdbc)登陆 1.cd /home/mr/phoenix/bin(此路径每个环境里面有可能不一样)2../sqlline.py localhost 二.shell repl Hb ...

  3. Api基类

    基类 class BaseController extends Controller{ public $outData = ['code'=>0,'msg'=>'ok']; public ...

  4. 【HNOI 2003】 激光炸弹

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1218 [算法] 二维前缀和 [代码] #include<bits/stdc++ ...

  5. Text段、Data段和BSS段

    不同的compiler在编译的过程中对于存储的分配可能略有不同,但基本结构大致相同. 大体上可分为三段:Text段.Data段和BSS段. text段用于存放代码,通常情况下在内存中被映射为只读,但d ...

  6. oracle学习笔记(二十) 子程序——函数与触发器

    子程序--函数 语法 之前select语句中使用的函数,都是SQL内置函数,我们可以通过自定义函数更满足我们的需要. 自定义函数的语法和存储过程差不多. create [or replace] $fu ...

  7. POJ 1873 计算几何

    思路: 二进制枚举一下要删哪些点 求个凸包,算一下贡献 //By SiriusRen #include <cmath> #include <cstdio> #include & ...

  8. org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException。

    jdk1.8环境tomcat运行项目报错, org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException.解决方法:更改jdk1.7

  9. OFDM同步算法之Minn算法

    minn算法代码 算法原理 训练序列结构 T=[B B -B -B],其中B表示由长度为N/4的复伪随机序列PN,ifft变换得到的符号序列 (原文解释):B represent samples of ...

  10. PAT甲级1016Phone Bills

    #include<iostream> #include<cstdio> #include<cstdlib> #include<vector> #incl ...