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. jQuery的DOM操作之设置和获取HTML、文本和值 html()text()val()

    1. html()方法: 此方法类似于JavaScript中的innerHTML属性,可以用来读取或者设置某个元素中的html内容. <html> <head> <met ...

  2. 列表中文字太多 溢出使用省略号css方法

    我们经常会遇到文字太多,而为了不打破原有布局,需要将多出文字用省略号代替,实现以下效果: 文字太太太太多多多啦...... 这个不多. html:这是个列表.ul/ol都行. <ul> & ...

  3. 【实践练习一】Git以及Github的使用

           以前经常在同学大神那听说过Github这神器,虽敬佩久已,奈何却无缘使用.好吧,我承认,主要还是不会用,一看网站全是英文的,想想还是不要为难自己了.然而现在还是要为难自己了,趁着早上刚学 ...

  4. php soapclient 超时 设置

    用php的soapclient,默认是60秒.可在php.ini里配置, 重启APache 或者在PHP代码里做设置 ini_set('default_socket_timeout', 300);// ...

  5. python *args,**kwargs用法

    *args用于接受传入的值,无限制,但是不能接收key类型的,如c=2 def fun(*args): for i in args: print(i) print("test") ...

  6. CloudSim源代码学习——虚拟机(VM)

    package org.cloudbus.cloudsim; import java.util.ArrayList;//This class provides methods to manipulat ...

  7. Linux 学习笔记之超详细基础linux命令 Part 1

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122   说明:主要是在REHL Server 6操作系统下进行的测试 --字符界面虚拟终端与图形界面之间的切 方法:[ ...

  8. 如何将 asp.net core 应用进行 docker 容器部署

    asp.net core 部署在 docker 容器中比较简单,但常因asp.net core程序发布的问题造成容器无法正常启动.现在把详细的操作的步骤记录如下: 一.asp.net core web ...

  9. win10下解压版mysql-8.0.12安装教程

    内容转载于:https://blog.csdn.net/hust_hqq/article/details/80572133 在他之上添加了一个:服务名无效的解决方法 1.官网下载安装包 网址:http ...

  10. FastReport脚本把数据绑定到文本控件上

    public class ReportScript { private void Data25_BeforePrint(object sender, EventArgs e)//Data25是指需要绑 ...