题目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. BZOJ-2308 小z的袜子(莫队)

    题目链接 题意 $n$点$m$次询问区间内随机取两个数是相同数的概率 思路 莫队入门题,对询问按块排序后更新答案,复杂度$O(n\sqrt{n})$ 代码 //#pragma comment(link ...

  2. jquery.ajax()详解

    jQuery.ajax() 函数详解 traditional 如果你希望使用传统方式来序列化参数,将该属性设为true. 传递数组时, traditional必须为true var arr = []; ...

  3. LeakCanary监测内存泄漏的原理

    监测机制利用了Java的WeakReference和ReferenceQueue,通过将Activity包装到WeakReference中,被WeakReference包装过的Activity对象如果 ...

  4. Android Fragment 生命周期及其正确使用(建议使用自定义View替换Fragment)

    使用Fragment 官方例子中显示: 例如:一个学生Fragment,需要传入studentId,进行http请求显示,那么setArguments后防止杀掉Fragment后,参数为0,显示不了数 ...

  5. Mysql --数据的增删改

    插入数据 INSERT 更新数据 UPDATE 删除数据 DELETE 一. 在mysql管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 1.使用insert实现数据的插入 2.u ...

  6. noj算法 素数环 回溯法

    描述: 把1到20这重新排列,使得排列后的序列A满足:a. 任意相邻两个数之和是素数b. 不存在满足条件a的序列B使得:A和B的前k(0 <= k <= 19)项相同且B的第k+1项比A的 ...

  7. 金蝶K/3 报销相关SQL语句

    金蝶K/3 报销相关SQL语句 use AIS20180607113701 select fopenid,* from dbo.t_XunTong_User where Fname ='' go us ...

  8. [原创]Xilinx工具关联UEStudio

    UE安装目录如下: C:\Program Files (x86)\IDM Computer Solutions\UEStudio\UEStudio.exe 对于ISE工具,在Editor -> ...

  9. RxJS操作符(二)

    一.Observable的性质 三种状态:nex, error, complete 进入到Error状态: ) .filter(val=>{ throw '出错了' }) .take() .re ...

  10. python之re正则简单够用

    0. 1.参考 Python正则表达式指南 https://docs.python.org/2/library/re.html https://docs.python.org/2/howto/rege ...