PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)
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 threthold 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
题意:
给定n条记录(注意不是n个人的记录),两个人之间的关系的权值为这两个人之间所有电话记录的时间之和。
一个连通块的权值为所有关系权值之和。
如果一个连通块节点数大于2,且权值大于给定的k,称这是一个gang,拥有关系权值和最多的人是gang的头。
要求输出gang的数量,每个gang的头,每个gang的人数。按照gang的头的字典序排序。
题解:
bfs求连通块。有几个注意点:
1、给定的是n条记录,n<=1000, 这说明人数应该是2000以内而不是1000以内。否则会测试点3运行时错误。
2、每一条记录都要考虑,bfs时不能仅判断这个节点是否被访问,还应该设置边的vis数组。
错误点:
1、忘记排序,测试点2,5答案错误,否则会测试点3运行时错误
2、n<=1000, 这说明人数应该是2000以内而不是1000以内
AC代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
using namespace std;
map<string,int>m1;
map<int,string>m2;
vector<int>v[];//给定的是n条记录,n<=1000, 这说明人数应该是2000以内而不是1000以内。测试点3运行时错误
int ok[];//标记有没有被访问过
struct node{//存储最终答案
string head;
int num;
}ans[];
int w[];//计算每个点的权重
int n,minw;
queue<int>q;
int k=;//map里的个数
int ans_num=;//答案个数
bool cmp(node x,node y){
return x.head<y.head;
}
void bfs(int x){
int w_sum=;//计算总权重
int num=;//计算人数
int max_w=w[x];
int head=x;
q.push(x);
ok[x]=;//标记有没有被访问过
num++;
w_sum+=w[x];
while(!q.empty()){
int a=q.front();
q.pop();
for(int i=;i<v[a].size();i++){
int b=v[a].at(i);
if(ok[b]) continue;
if(max_w<w[b]){//找出权重的为头目
head=b;
max_w=w[b];
}
ok[b]=;
q.push(b);
w_sum+=w[b];
num++;
}
}
w_sum/=;
if(num>= && w_sum>minw){
//cout<<"答案:num"<<num<<" "<<m2[head]<<" "<<ans_num<<" w_sum "<<w_sum<<endl;
node answer;
answer.num=num;
answer.head=m2[head];
ans[++ans_num]=answer;
}
}
int main(){
cin>>n>>minw;
memset(w,,sizeof(w));
memset(ok,,sizeof(ok));
for(int i=;i<=n;i++){
if(!v[i].empty()) v[i].clear();
}
for(int i=;i<=n;i++){
string na1,na2;
int ww;
cin>>na1>>na2>>ww;
if(!m1[na1]) {
m2[k]=na1;
m1[na1]=k++;
}
if(!m1[na2]) {
m2[k]=na2;
m1[na2]=k++;
}
w[m1[na1]]+=ww;
w[m1[na2]]+=ww;
v[m1[na1]].push_back(m1[na2]);
v[m1[na2]].push_back(m1[na1]);
}
for(int i=;i<=n;i++){
if(!ok[i]){
bfs(i);
}
}
cout<<ans_num<<endl;
sort(ans+,ans++ans_num,cmp);//排序,一开始忘记排了
for(int i=;i<=ans_num;i++){
cout<<ans[i].head<<" "<<ans[i].num<<endl;
}
return ;
}
PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)的更多相关文章
- pat 甲级 1034. Head of a Gang (30)
1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...
- PAT甲级1034. Head of a Gang
PAT甲级1034. Head of a Gang 题意: 警方找到一个帮派的头的一种方式是检查人民的电话.如果A和B之间有电话,我们说A和B是相关的.关系的权重被定义为两人之间所有电话的总时间长度. ...
- 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 ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)
1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight Wi assigne ...
- PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to ...
- 【PAT甲级】1034 Head of a Gang (30 分)
题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...
- PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]
题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...
- PAT甲级1034 Head of a Gang【bfs】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...
随机推荐
- sql index改怎么建
https://stackoverflow.com/questions/11299217/how-can-i-optimize-this-sql-query-using-indexes ------- ...
- 高性能集群(HPC
串行计算与并行计算1.串行计算串行计算是指在单个计算机(拥有单个中央独立单元) 上执行软件写操作.CPU 逐个使用一系列指令解决问题.为了加快处理速度,在原有的串行计算的基础上演变出并行计算2.并行计 ...
- CentOS升级Openssl至openssl-1.1.0
1.查看原版本 wget http://www.openssl.org/source/openssl-1.1.0c.tar.gz openssl version 2.解压安装tar zxf opens ...
- Java8新特性--函数式编程
在jdk8中什么是函数式接口: 1.被@FunctionalInterface注解修饰的. 2.接口里边只有一个非default的方法. 满足以上2个条件的即为函数式接口,ps:即使一个接口没有被@F ...
- mac下解决中文乱码的问题
最近在做自己的博客项目,需要在公司和家代码同步,当然代码托管到了码云上面,但是公司是win系统,家里是mac系统,奇怪的是在公司.....算了不废话了,直接正题吧. 就是代码在公司中文显示没毛病,然后 ...
- git将一个分支的内容替换为另一分支内容
假设我想将我的linux分支内容替换master分支的内容. # 切换到master分支 git checkout master # 再将本地的master分支重置成linux git reset - ...
- postgresql学习笔记--基础篇 -psql工具
--创建用户 CREATE ROLE pguser WITH ENCRYPTED PASSWORD 'pguser'; --创建表空间目录 mkdir -p /database/pg10/pg_tbs ...
- BZOJ1997 HNOI2010 平面图判定 planar (并查集判二分图)
题意 判断一个存在哈密顿回路的图是否是平面图. n≤200,m≤10000n\le200,m\le10000n≤200,m≤10000 题解 如果一定存在一个环,那么连的边要么在环里面要么在外面.那么 ...
- Linux 查看系统配置参数
原文链接:http://www.cnblogs.com/aric2016/p/10971690.html 查看 cpu信息: cat /proc/cpuinfo 查看内存信息: grep MemTot ...
- Jquery检验输入值
1.检验邮件 function chkEmail(strEmail) { if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w ...