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. HTML中body与html的关系

    转载自张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com] 一.相关基础 一个div块级元素没有主动为其设置宽度和高度,浏览器会为其分配可使用的最大宽度(比如全屏宽度),但是不 ...

  2. HTML中令人惊喜的全局属性

    1.accesskey 属性 : 规定激活元素的快捷键. 浏览器支持:几乎所有浏览器均 accesskey 属性,除了 Opera. 定义和用法 accesskey 属性规定激活(使元素获得焦点)元素 ...

  3. JQuery瀑布流特效(练习)

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  4. VUE路由转场特效,WebAPP的前进与后退

    一.效果图 二.思路 1. 定义两个 CSS 过度动画,前进与后退: slide-right-enter   和   slide-left-enter 2. 给路由配置meta信息,设置各个路由的级别 ...

  5. 不用Visual Studio,5分钟轻松实现一张报表

    常规的报表设计,如RDLC.水晶报表等,需要安装Visual Studio,通过VS提供的报表设计界面来设计报表,通过VS设计报表对.NET开发者而言非常方便,但是对于非开发人员,要安装4G的一个VS ...

  6. 使用过AsyncTask、EventBus、Volley以及Retrofit,必须好好了解handler运行机制

    我们都知道在UI线程中不能进行耗时操作,例如数据读写.网络请求.Android 4.0开始,在主线程中进行网络请求甚至会抛出Android.os.NetworkOnMainThreadExceptio ...

  7. 前端路由简介以及vue-router实现原理

    后端路由简介 路由这个概念最先是后端出现的.在以前用模板引擎开发页面时,经常会看到这样 http://www.xxx.com/login 大致流程可以看成这样: 浏览器发出请求 服务器监听到80端口( ...

  8. puppeteer入门

    转自: https://www.jianshu.com/p/a89d8d6c007b 作者: ppCode puppeteer新手入门(chromium下载跳坑) ppCode 关注 2017.12. ...

  9. ORACLE11g下如何利用SQL DEVELOPER连接上数据库

    最近在学习数据库的相关内容,在sqlplus敲了几天命令行窗口后,想尝试一下用sql developer 连接上数据库但一直没有实现.在网上查询了相关资料后现在终于弄好了,就来写下此篇博文与大家分享! ...

  10. AspNet Core2 浏览器缓存使用

    Core2中使用Microsoft.AspNetCore.Mvc下的ResponseCacheAttribute特性来控制Http Get请求的缓存 原理是设置http请求 响应头的Cache-con ...