立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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


#include<bits/stdc++.h>
using namespace std;
struct Set{//作为辅助求解的集合类int head=-1;//头目int weight=0;//集合的总权值int num=0;//集合的人数
};
const int MAXV=2005;
int weight[MAXV]={0};//点权
bool visit[MAXV]={false};//结点是否已被访问
Set gang[MAXV];//辅助的计算Gang的数组
vector<vector<int>>graph(MAXV);
void DFS(int v,int start){visit[v]=true;//将该节点设置为已访问++gang[start].num;//递增该集合人数gang[start].weight+=weight[v];//增加该集合总权值if(gang[start].head==-1)//更新该集合头目gang[start].head=v;else if(weight[v]>weight[gang[start].head])gang[start].head=v;for(int i:graph[v])if(!visit[i])DFS(i,start);
}
int main(){int N,K;scanf("%d%d",&N,&K);unordered_map<string,int>STOI;//将名字映射到一个整数vector<string>ITOS;//将整数映射到名字for(int i=0;i<N;++i){//读入数据getchar();string s1,s2;cin>>s1>>s2;int w;scanf("%d",&w);if(STOI.find(s1)==STOI.cend()){//如果STOI中没有改名字,将名字加入STOI,并同步更新ITOSSTOI[s1]=ITOS.size();ITOS.push_back(s1);}if(STOI.find(s2)==STOI.cend()){STOI[s2]=ITOS.size();ITOS.push_back(s2);}weight[STOI[s1]]+=w;//更新点权weight[STOI[s2]]+=w;//更新点权graph[STOI[s1]].push_back(STOI[s2]);//向图中增加无向边graph[STOI[s2]].push_back(STOI[s1]);//向图中增加无向边}for(int i=0;i<2*N;++i)//深度优先遍历if(!visit[i])DFS(i,i);map<string,int>result;//存储最终输出结果,利用map自动按头目名字排序for(int i=0;i<N;++i)//遍历找到符合条件的Gangif(gang[i].num>2&&gang[i].weight/2>K)result.insert({ITOS[gang[i].head],gang[i].num});printf("%d\n",result.size());for(auto i=result.cbegin();i!=result.cend();++i)printf("%s %d\n",(i->first).c_str(),i->second);return 0;
}

耗时:

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

  1. PAT:1034 Head of a Gang (30分)

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

  2. 【PAT - 甲级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 ...

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

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

  4. 1034 Head of a Gang (30 分)

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

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

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

  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. 3分和30分文章差距在哪里?

    好的分析和可视化,可以提供大量的信息,同时兼顾简洁优雅. 今天我们抛开实验设计.方法和工作量等因素,仅从文章最吸引人的图片来讨论3分和30分(顶级)文章差距在哪里? 以2017年8月25日发表在Sci ...

  8. 微生物组:3分和30分文章差距在哪里?

    好的分析和可视化,可以提供大量的信息,同时兼顾简洁优雅. 今天我们抛开实验设计.方法和工作量等因素,仅从文章最吸引人的图片来讨论3分和30分(顶级)文章差距在哪里? 以2017年8月25日发表在Sci ...

  9. PAT甲级1038 Recover the Smallest Number (30 分):[C++题解]贪心、排列成最小的数、字符串

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心: 对于字符串a和b,如果 a+b < b+a (这里+代表字符串中的连接)代表字典序更小.举例 a = 321 , b ...

最新文章

  1. Matlab 重命名
  2. fastText原理和文本分类实战
  3. nginx 配置expires
  4. autoload.php beanbun_Beanbun: 简单开放的 PHP 爬虫框架
  5. pip3 安装pycrypto 时报错
  6. windows2000/xp运行命令全集
  7. fireFox模拟 post请求、上传插件,火狐浏览器中文postman插件
  8. JMETER 线程组
  9. HDU2000 ASCII码排序【字符串排序】
  10. Hadoop 基本数据类型
  11. icmp判断可达_伪造目标不可达的ICMP数据包
  12. Linux驱动移植——ENC28J60以太网控制器
  13. Hybird接口的理解
  14. 图纸管理协同办公软件推荐
  15. 【LensFlare镜头光晕】Unity3D奇葩实现
  16. 传统的企业如何实现数字化转型?
  17. html中3d哪个方向是x轴,详解用CSS绘制3D旋转立方体
  18. 音乐制作录音宿主软件-MAGIX ACID Pro 10 Suite 10.0.5.35 x64 WiN
  19. 如何在Mac上刻录DVD以获取可播放的视频?
  20. 外星人系统下载Alienware X17R2系统dell原厂预装版 WIN11-21H2外星人原厂WIN11系统下载,附创建带F12 SupportAssist OS Recovery恢复功能教程

热门文章

  1. Linux 内存管理 | 物理内存管理:物理内存、内存碎片、伙伴系统、slab分配器
  2. 数据结构与算法 | 二分查找
  3. linux 如何查看应用程序进程号、端口
  4. 使用 ebpf 深入分析容器网络 dup 包问题
  5. RabbitMQ管理(3)——Web端管理
  6. 从零开始玩转JMX(二)——Condition
  7. 写了 30 多个 Go 常用文件操作的示例,收藏这一篇就够了
  8. AV1解码器dav1d性能提升100%
  9. 零拷贝(Zero Copy)
  10. 腾讯数字生态大会倒计时4天:请收下这份最全的TEG参会攻略~