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,字符串和数字的对应保存的更多相关文章

  1. 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 ...

  2. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...

  3. 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 ...

  4. 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 ...

  5. 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历

    二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历   二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则 ...

  6. 【PAT】1091 Acute Stroke(30 分)

    1091 Acute Stroke(30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the s ...

  7. [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 ...

  8. PAT甲级——1131 Subway Map (30 分)

    可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30  ...

  9. 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 ...

  10. PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)

    1147 Heaps (30 分)   In computer science, a heap is a specialized tree-based data structure that sati ...

随机推荐

  1. 学Java的18天,今天老师讲构造方法;

    上一篇讲到方法的调用和简单的构造方法,今天继续加深,加参数或者该参数: package sklx; public class Car{ //设三个属性 private String 品牌; priva ...

  2. JavaScript复杂判断的更优雅写法

    摘要: 写代码是一门艺术. 原文:JavaScript 复杂判断的更优雅写法 作者:Think. 公众号:大转转fe Fundebug经授权转载,版权归原作者所有. 前提 我们编写js代码时经常遇到复 ...

  3. 请输入经过encode编码的URL

    美团门店映射: <?php $url = "http://manage.test.kdb.kudianbao.com/Branch/mt_mdysh1d"; $res = u ...

  4. mac 上运行httpserver的问题

    如果你的系统是window 我建议你安装http-server 非常好用, 如果是mac,系统自带的就有httpserver 服务,没有必要在安装 按照我说的来操作 首先 sudo apachectl ...

  5. Mysql数据库的(行记录)详细操作

    在Mysql管理软件中, 可以通过sql语句中的dml语言来实现数据的操作, 包括 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据的删除 使用SELECT查询数据 ...

  6. imooc《JavaScript深入浅出》上的一个 arraysSimilar 函数

    任务 请在 index.html 文件中,编写 arraysSimilar 函数,实现判断传入的两个数组是否相似.具体需求: 数组中的成员类型相同,顺序可以不同.例如 [1, true] 与 [fal ...

  7. React报错:Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix,

    今天在开发时报了以下错误,记录一下 我们不能在组件销毁后设置state,防止出现内存泄漏的情况 出现原因直接告诉你了,组件都被销毁了,还设置个锤子的state啊 解决方案: 利用生命周期钩子函数:co ...

  8. switch和if语句

    if :基本语法: 1.单分支语句 : if(条件){代码块}else{代码块} 2.多分支语句 :if(条件){代码块} else if(条件){代码块}else{代码块} * 不要忘记添加else ...

  9. 【读书笔记】iOS-手势识别

    一,事件处理机制 事件是当用户手指触及屏幕,或地屏幕上滑动,或摇晃设备等时候,系统不断地把这些事件通过消息发送给应用程序对象.在iOS设备中能够捕获的事件有3种:触摸事件,移动事件和多媒体远程控制事件 ...

  10. html中用href 实现点击链接弹出文件下载对话框

    浏览器支持能够打开的格式,他都会默认直接在线打开(比如word或图片),不支持的格式,他就会弹出下载提示.最好是做成.rar格式.xlsx的文件.浏览器自带下载功能. <body> < ...