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

题意

找满足以下两个要求的帮派:
(1) 帮派人数大于2人
(2)帮派的总联系度(通话时长)大于给定的K
除此之外找到满足要求的帮派的老大,老大是帮派中单人联系度最大的人。
输出要求:
先输出满足要求的帮派数,
然后按帮派老大的名字的字典序输出,同时后面输出这个帮派的人数

思路

首先需要将每个人的名字映射出一个唯一编号(id),id和名字需要互相映射。
之后根据联系建图,然后基于dfs,对每个人染色,每次dfs染色的人一定是同一个帮派的人(即每次dfs给有联系的人染同一种颜色),dfs过程中统计需要的信息,最后根据题意处理一下即可。

理清思路,题目实现起来不难,我在本题中使用了比较多STL容器来辅助处理数据。

代码

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
const int P = 2e3+5;
int id;
map<int,string>mp; //id:name
map<string,int>mp2;//name:id
vector<int>edge[P];
int w[P]; //w[i]:编号为i的人的联系度
set<int>st; //所有的人(里面存每个人的编号)
bool vis[P]; //vis[i]标记i这个人是否被染色
int color_id; //帮派编号
vector<int>vec[P]; //每个帮派的人
int head[P]; //每个帮派里的老大
int w_total[P];
//w_total[i]:帮派编号i所有人联系度的和(最后统计时需要/2,因为两个人联系是互相的,统计了两遍)
//ans
struct Node
{string h;int cnt;bool operator<(const Node &x)const{return h < x.h;}
} node[P];
//每个人映射一个唯一id
inline int get_id(string s)
{if(mp2[s])return mp2[s];else{mp[++id] = s;mp2[s] = id;return id;}}
//建图
inline void add_edge(int x_id,int y_id,int v)
{edge[x_id].push_back(y_id);edge[y_id].push_back(x_id);w[x_id] += v;w[y_id] += v;st.insert(x_id);st.insert(y_id);
}
//dfs 染色
void dfs(int x)
{int cnt = edge[x].size();for(int i = 0; i < cnt; ++i){int to = edge[x][i];if(!vis[to]){vis[to] = true;vec[color_id].push_back(to);if(w[to] > w[head[color_id]]) head[color_id] = to;w_total[color_id] += w[to];dfs(to);}}return;
}int main()
{int n,k;cin>>n>>k;for(int i = 0; i < n; ++i){string x,y;int t;cin>>x>>y>>t;int x_id = get_id(x);int y_id = get_id(y);add_edge(x_id,y_id,t);}for(set<int>:: iterator it=st.begin(); it!=st.end(); it++){if(!vis[*it]){color_id++;vec[color_id].push_back(*it);w_total[color_id] += w[*it];head[color_id] = *it;vis[*it] = true;dfs(*it);}}int t = 0;for(int i = 1; i <= color_id; ++i){int cnt = vec[i].size();if(cnt > 2 && w_total[i]/2 > k){node[t].h = mp[head[i]];node[t].cnt = cnt;t++;}}sort(node,node+t);cout<<t<<endl;for(int i = 0; i < t; ++i){cout<<node[i].h<<" "<<node[i].cnt<<endl;}return 0;
}

$Daily English

Shut out all of your past except that which help you weather your tomorrows.
放下那些不能帮助你前行的过去。

PAT:1034 Head of a Gang (30分)相关推荐

  1. 1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people‘s pho

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 One way that the police finds the head of a gang is to check peop ...

  2. 1034 Head of a Gang (30 分) 【难度: 中 / 知识点: 并查集】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 首先不难想到的是并查集,不过这里有一个关键的 ...

  3. PAT A1034 Head of a Gang (30 分)

    刚刚开始学习图论,这是一道图的遍历题,目前对我来说独立完成还是有点困难的...自己想了很久,参考了书上的代码,也调试了很久,希望做后面的题目能有进步吧. #include <cstdio> ...

  4. pat 1034. Head of a Gang (30)

    题意:求边权大于k并且点数大于2的联通块,输出其中的重要点,重要点:联通块中与该点相连的边权最大 丢set里乱搞 #include<bits/stdc++.h> using namespa ...

  5. 1034 Head of a Gang (30 分)

    题目 题目链接 题解 并查集. 注意坑点:帮派的人数必须大于2. 代码 #include<bits/stdc++.h> using namespace std; #define PSI p ...

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

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

  7. PAT甲级1076 Forwards on Weibo (30 分) :[C++题解]图论、bfs

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: BFS如何搜前k层?统计前k层的点数. ac代码 #include<bits/stdc++.h> using names ...

  8. PAT甲级1068 Find More Coins (30 分):[C++题解]DP、背包问题、dp输出方案

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:m是背包容量,a1,a2,....,ana_1,a_2,....,a_na1​,a2​,....,an​是n个物品,第i个物品的体积是 ...

  9. PAT甲级1045 Favorite Color Stripe (30 分):[C++题解]最佳彩色带、DP、公共子序列变形

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:这是一个公共子序列的问题.但是有点变式,序列a和序列b不是完全等价的,序列a的每个元素可以对应多个相同元素,而且有些元素可以不使用.比 ...

最新文章

  1. Elasticsearch 6.3.1、Head插件 安装及配置
  2. git git git
  3. C/C++中的段错误(Segmentation fault)[转]
  4. NSTimer注意内存泄露(真该死)
  5. php防错处理,更好的PHP错误处理
  6. 拉普拉斯时域卷积定理_如何证明频域卷积定理
  7. tps 数据库写并发衡量_硬核干货!抗住百万高并发的 6 个关键技术!
  8. sql server 开源_开源工具SQL Server安全注意事项
  9. VO、DTO、POJO、PO的区别
  10. python之调用科大讯飞的在线语音识别
  11. java 文件递归删除文件夹_JAVA利用递归删除文件和文件夹
  12. keil5写c语言的步骤,keil5使用C51的详细操作步骤
  13. Lodop,前端自定义打印
  14. scala面向对象总结
  15. 教育行业数据防泄密解决方案
  16. 059.迪杰斯特拉(Dijkstra)算法的原理以及解决最短路径问题
  17. 阿里算法实习生面试回忆
  18. Python爬取整个网页的数据
  19. 齐博 php7,齐博cmsv7.0后台getshell
  20. MeterSphere | 超好用的开源测试平台

热门文章

  1. 中北大学c语言程序设计作业答案,C语言程序设计
  2. iphone7参数_来自iPhone8用户的真实体验---这次我们不谈参数,只聊体验
  3. discuz mysql_搭建Discuz! (mysql+apache+Discuz! )
  4. 和cnn结合_写给小白的R-CNN介绍
  5. 内存颗粒和闪存颗粒的区别_浅谈闪存颗粒二三事
  6. 从零开始学习docker(二十)RoutingMesh--Ingress负载均衡
  7. SpringMVC 使用hibernate返回list
  8. 如何入手卷积神经网络
  9. 我的研究生这三年(含腾讯AI Lab实习、CVPR发表经验)
  10. 服务器2012r2系统远程登陆,服务器2012r2系统远程登陆