题目https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624

题意:

给定n条记录(注意不是n个人的记录),两个人之间的关系的权值为这两个人之间所有电话记录的时间之和。

一个连通块的权值为所有关系权值之和。

如果一个连通块节点数大于2,且权值大于给定的k,称这是一个gang,拥有关系权值和最多的人是gang的头。

要求输出gang的数量,每个gang的头,每个gang的人数。按照gang的头的字典序排序。

思路:

bfs求连通块。有几个注意点:

1、给定的是n条记录,$n<=1000$, 这说明人数应该是2000以内而不是1000以内。否则会有段错误

2、每一条记录都要考虑,bfs时不能仅判断这个节点是否被访问,还应该设置边的vis数组。

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n, k;
map<string, int>mp;
map<int, string>revmp;
const int maxn = ;
int g[maxn][maxn];
int tot = ; struct node{
int head;
int mxweight = -;
int num;
}gang[maxn];
int ans = ;
bool vis[maxn];
bool evis[maxn][maxn]; void bfs()
{
for(int i = ; i < tot; i++){
if(!vis[i]){
int weight = ;
queue<int>que;
que.push(i);
vis[i] = true;
//printf("\n%d ", i); while(!que.empty()){
int now = que.front();que.pop();
gang[ans].num++;
int res = ;
for(int j = ; j < tot; j++){
res += g[now][j];
if(!evis[now][j] && g[now][j]){
weight += g[now][j];
evis[now][j] = evis[j][now] = true;
}
if(!vis[j] && g[now][j]){
que.push(j);
vis[j] = true;
}
}
if(res > gang[ans].mxweight){
gang[ans].mxweight = res;
gang[ans].head = now;
}
}
if(weight > k && gang[ans].num > ){
ans++;
}
else{
gang[ans].num = ;
gang[ans].mxweight = -;
gang[ans].head = ;
}
}
}
} bool cmp(node a, node b)
{
return revmp[a.head] < revmp[b.head];
} int main()
{
scanf("%d%d", &n, &k);
for(int i = ; i < n; i++){
string a, b;
int val;
cin>>a>>b>>val; if(mp.find(a) == mp.end()){
revmp[tot] = a;
mp[a] = tot++;
}
if(mp.find(b) == mp.end()){
revmp[tot] = b;
mp[b] = tot++;
}
g[mp[a]][mp[b]] += val;
g[mp[b]][mp[a]] += val;
}
//cout<<tot<<endl;
// for(int i = 0; i < tot; i++){
// for(int j = 0; j < tot; j++){
// printf("%d ", g[i][j]);
// }
// printf("\n");
// }
bfs();
printf("%d\n", ans);
sort(gang, gang + ans, cmp);
for(int i = ; i < ans; i++){
cout<<revmp[gang[i].head]<<" "<<gang[i].num<<endl;
}
return ;
}

PAT甲级1034 Head of a Gang【bfs】的更多相关文章

  1. PAT甲级1034. Head of a Gang

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

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

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

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

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

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

  6. PAT甲级1091 Acute Stroke【三维bfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805375457411072 题意: 求三维的连通块 思路: 简单b ...

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

  8. 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 phon ...

  9. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. APPLE-SA-2019-3-25-1 iOS 12.2

    APPLE-SA-2019-3-25-1 iOS 12.2 iOS 12.2 is now available and addresses the following: CFStringAvailab ...

  2. 集合-Comparator和Comparable

    文章内容参考博客:https://www.cnblogs.com/xujian2014/p/5215082.html 1.Comparable Comparable是排序接口,当一个类实现了Compa ...

  3. vue中怎么实现获取当前点击对象this

    应用场景 在评论列表中,有很多条评论(通过循环出来的评论列表),评论的文字有多跟少,默认展示2行超出显示点击查看更多,,要点击查看更多对当前的这条评论进行全部评论展示! 问题描述 要是在传统的点击事件 ...

  4. 转:对UI自动化测试的一些感悟

    不断发掘自动化测试对各个团队的附加价值,这样才能得到来自四面八方的支持,没有将自动化加入项目过程的自动化都达不到预期的效果. UI自动化框架 把UI自动化框架设计成一个拼图性质的架构.把每个特性都设计 ...

  5. 【原创】大数据基础之Spark(3)Spark Thrift实现原理及代码实现

    spark 2.1.1 一 启动命令 启动spark thrift命令 $SPARK_HOME/sbin/start-thriftserver.sh 然后会执行 org.apache.spark.de ...

  6. 【原创】大叔经验分享(44)hdfs副本数量

    当hdfs空间不足时,除了删除临时数据或垃圾数据之外,还可以适当调整部分大目录的副本数量,多管齐下: 1 查看 $ hdfs dfs -ls /user/hive/warehouse/temp.db/ ...

  7. CENTOS 7 安装 TINYPROXY 代理服务器

    https://www.cnblogs.com/new_2050/p/7658508.html

  8. ALU底层方法及计算机整数加减乘除模拟

    ALU是计算机CPU的核心,即 算术逻辑单元(arithmetic and logic unit)ALU有几大功能,是计算机计算最基础的功能:1.算术运算:包含加法.减法等2.逻辑运算:主要是布尔运算 ...

  9. win10+caffe+GPU

    由于学习需要,决定安装caffe,之前用的都是基于theano的keras.听说win下caffe很难配置,经过一个下午和晚上的配置终于成功,以此记录. 我的电脑:win10 64位,N卡GTX950 ...

  10. webstorm主要快捷键

    1.  必备快捷键 Ctrl+/:注释当前行    F11:全屏    Alt+数字:切换打开第N个文件    Ctrl+Shift+P:打开命令面板    Ctrl+P:搜索项目中的文件    Ct ...