题目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. 高并发秒杀系统--Service事务管理与继承测试

    [Spring IoC的类型及应用场景]  [Spring事务使用方式] [Spring事务的特性] [Spring事务回滚的理解] [Service声明式事务的配置] 1.配置事务管理器 2.配置基 ...

  2. PERFECT NUMBER PROBLEM(思维)

     题目链接:https://nanti.jisuanke.com/t/38220 题目大意:这道题让我们判断给定数字是否为完美数字,并给来完美数字的定义,就是一个整数等于除其自身之外的所有的因子之和. ...

  3. Bootstrap模态框钩子事件

    事件类型   描述show.bs.modal show 方法调用之后立即触发该事件.如果是通过点击某个作为触发器的元素,则此元素可以通过事件的 relatedTarget 属性进行访问.shown.b ...

  4. MVC ModelState.AddModelError使用

    cshtml:红色部分为使用方法  PostNextDay是 字段 <div id="postNextDay" class="col-xs-12 col-sm-6 ...

  5. ssh-copy-id 拷贝用户秘钥

    生成秘钥 ssh-keygen -t [rsa|dsa] 将会生成密钥文件和私钥文件 id_rsa,id_rsa.pub或id_dsa,id_dsa.pub 将 .pub 文件复制到B机器的 .ssh ...

  6. Oracle 数据文件迁移

    背景 这两天做一个oracle数据库迁移,以前都是用exp.imp来走,这次用到了expdp.impdp,的确有些优势,但同时又想起了只是拷贝数据文件迁移的方式,其实这个方式不常用做迁移,更多用在磁盘 ...

  7. [Linux]ln -al输出内容解析

    第一项的第一位是文件类型:-: 表示普通文件d: 目录b: 块特殊文件c: 字符特殊文件l:  符号链接文件p: 命名管道文件FIFOs: 套接字文件 第二项是该文件的inode的链接计数,即stat ...

  8. 布思算法Java实现

    public String multiply(String Q,String M){ char Q0 = '0'; String A = get01(Q.length(),"0") ...

  9. Java_String&StringBuilder&StringBuffer类

    目录 一.String类 二."==" 和 "equals"的区别 三.StringBuffer和StringBuilder 一.String类 String为 ...

  10. 安装酷痞到IIS7.x共用80端口Windows(64位)系统下运行多个酷痞

    需求: 1.酷痞直接运行的模式是自宿主运行.由于win系统一般都由iis提供多个网站服务,并首先占用了80端口,如果想酷痞可以直接通过主机头和iis共用80端口会出现这种运行模式的需求. 以下是实现方 ...