PAT A1034 Head of a Gang (30 分)——图遍历DFS,字符串和数字的对应保存
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 threthold 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 <stdio.h>
#include <string>
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
const int maxn = ;
map<string, int> s2i;
map<int, string> i2s;
map<string,int> gang;
int n, k, num = ;
int g[maxn][maxn], w[maxn];
bool vis[maxn];
int change(string s) {
if (s2i.find(s) == s2i.end()) {
i2s[num] = s;
s2i[s] = num;
return num++;
}
else {
return s2i[s];
}
}
void dfs(int v,int &count,int &weight,int& head) {
vis[v] = true;
count++;
if (w[v] > w[head]) {
head = v;
}
for (int i = ; i < num; i++) {
if (g[v][i] != ) {
weight += g[v][i];
g[v][i] = ;
g[i][v] = ;
if (vis[i] == false) {
dfs(i, count, weight, head);
}
}
}
}
void dfsTrave() {
fill(vis, vis + maxn, false);
for (int i = ; i < num; i++) {
int count = , weight = , head = i;
if (vis[i] == false) {
dfs(i,count,weight,head);
}
if (count > && weight > k) {
gang[i2s[head]] = count;
}
}
}
int main() {
fill(g[], g[] + maxn * maxn, );
fill(w, w + maxn, );
scanf("%d %d", &n, &k);
for (int i = ; i < n; i++) {
string s1, s2;
int t;
cin >> s1 >> s2 >> t;
getchar();
int id1 = change(s1);
int id2 = change(s2);
w[id1] += t;
w[id2] += t;
g[id1][id2] += t;
g[id2][id1] += t;
}
dfsTrave();
printf("%d\n", gang.size());
for (auto it = gang.begin(); it != gang.end(); it++) {
cout << it->first << " " << it->second << endl;
}
system("pause");
return ;
}
注意点:dfs遍历图,就是中间的计算比较复杂,算总weight时要注意环,算一条边就把那条边置为0,可以防止重复计算
PAT A1034 Head of a Gang (30 分)——图遍历DFS,字符串和数字的对应保存的更多相关文章
- PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)
1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides ...
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- PAT A1110 Complete Binary Tree (25 分)——完全二叉树,字符串转数字
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- 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 ...
- 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历
二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则 ...
- 【PAT】1091 Acute Stroke(30 分)
1091 Acute Stroke(30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the s ...
- [PAT] 1143 Lowest Common Ancestor(30 分)
1143 Lowest Common Ancestor(30 分)The lowest common ancestor (LCA) of two nodes U and V in a tree is ...
- PAT甲级——1131 Subway Map (30 分)
可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 ...
- PAT 甲级 1076 Forwards on Weibo (30分)(bfs较简单)
1076 Forwards on Weibo (30分) Weibo is known as the Chinese version of Twitter. One user on Weibo m ...
- PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)
1147 Heaps (30 分) In computer science, a heap is a specialized tree-based data structure that sati ...
随机推荐
- Apache SkyWalking的架构设计【译文】
Apache SkyWalking提供了一个功能强大并且很轻量级的后端.在此,将介绍为什么采用以下方式来设计它,以及它又是如何工作的. 架构图 对于APM而言,agent或SDKs仅是如何使用libs ...
- java动态代理--一个简单的例子
这几天看视频看到了java的动态代理,这里写一个小例子.在写例子的时候发现:认为自己会了,和能写出来真不是一个概念.还是要多写代码,然后写博客再深入一些,费曼学习法--教,是最好的学. 1.什么是动态 ...
- ngx-echart地图
一.运行截图 二.代码 html代码: <div style="padding:24px;"> <p style="font-size: 16px;ma ...
- 使用普通用户执行 docker
CentOS 版本 7.4,Docker 版本 docker-1.13 及以下 ll /var/run/docker.sock srw-rw----. 1 root root 0 May 25 14: ...
- [转]Serif和Sans-serif字体的区别
在西方国家罗马字母阵营中,字体分为两大种类:Sans Serif和Serif,打字机体虽然也属于Sans Serif,但由于是等宽字体,所以另外独立出Monospace这一种类,例如在Web中,表示代 ...
- FHQ Treap小结(神级数据结构!)
首先说一下, 这个东西可以搞一切bst,treap,splay所能搞的东西 pre 今天心血来潮, 想搞一搞平衡树, 先百度了一下平衡树,发现正宗的平衡树写法应该是在二叉查找树的基础上加什么左左左右右 ...
- jQuer插件满屏气泡飘落动画效果
飘落动画效果插件引用: <script src="https://cdn.bootcss.com/JQuery-Snowfall/1.7.4/snowfall.jquery.min.j ...
- JMeter http(s)测试脚本录制器的使用
JMeter http(s)测试脚本录制器的使用 by:授客 QQ:1033553122 http(s) Test Script Recorder允许Jmeter在你使用普通浏览器浏览web应用时,拦 ...
- Oracle 关闭数据库(未使用Oracle Restart)
Oracle关闭数据库(未使用Oracle Restart) by:授客 QQ:1033553122 SHUTDOWN [选项] 选项说明: NORMAL-语句执行后,不允许创建新的连接:等待所有当前 ...
- Angular基础(八) Observable & RxJS
对于一个应用来说,获取数据的方法可以有很多,比如:Ajax, Websockets, LocalStorage, Indexdb, Service Workers,但是如何整合多种数据源.如何避免BU ...