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 ...
随机推荐
- canvas-star7.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CSS之Normalize.css的使用(重置表)
本文译自Normalize.css官网: http://nicolasgallagher.com/about-normalize-css/ Normalize.css 只是一个很小的CSS文件,但它在 ...
- React中使用百度地图API
今天我们来搞一搞如何在React中使用百度地图API好吧,最近忙的头皮发麻,感觉身体被掏空,所以很久都没来写博客了,但今天我一定要来一篇好吧 话不多说,我们直接开始好吧 特别注意:该React项目是用 ...
- JMeter 检查点之响应断言(Response Assertion)
检查点之响应断言(Response Assertion) by:授客 QQ:1033553122 JMeter断言用于对sampler(采样器)进行额外检查,且在相同作用域中,每执行完一个samp ...
- Kotlin入门(7)循环语句的操作
上一篇文章介绍了简单分支与多路分支的实现,控制语句除了这两种条件分支之外,还有对循环处理的控制,那么本文接下来继续阐述Kotlin如何对循环语句进行操作. Koltin处理循环语句依旧采纳了for和w ...
- OCaml相关
1.open Core.Std 时报Unbound module Core 先安装库 $ opam init $ opam install core $ opam install utop 在~/.o ...
- HashTree【转】
http://blog.csdn.net/yang_yulei/article/details/46337405 在各种数据结构(线性表.树等)中,记录在结构中的相对位置是随机的.因此在机构中查找记录 ...
- Jenkins修改workspace和build目录
Jenkins: Change Workspaces and Build Directory Locations 转自: http://ingorichter.blogspot.jp/2012/02 ...
- C# 动态方法和静态方法的区别
C# 动态方法和静态方法的区别 (转) 动态方法与静态方法的区别: 1,使用方法上的区别:动态方法,在使用时需要先创建实例,才能调用实例方法,而静态方法则不需要,直接使用即可. 示例代码如下:静态方法 ...
- January 07th, 2018 Week 01st Sunday
To remember is to disengage from the present. 铭记过去就是放弃当下. To remember the past doesn't mean we would ...