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 threshold 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

题目分析:刚开始想怎么才能利用字符串来表示图 觉得得使用map或者类似哈希函数将字符转化为数字 但一直想不到什么好方法 看了柳神的博客后...哇 还可以这样
具体就是利用 两个map来存节点与对应的值 其实也就是将每一个值对应为一个数字 这种想法感觉是哈希函数的一种实现
最后利用dfs来遍历图 找到需要的解
注意的是对于每条存在的边都需要纳入计算 而每个节点只需要且只能遍历一次
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
map<string, int>stringtoint;
map<int, string>inttostring;
map<string, int>ans;
int N, K;
int number = ;
int G[][];
int weight[];
int Collected[];
int STOI(string s)
{
if (stringtoint[s] == )
{
stringtoint[s] = number;
inttostring[number] = s;
return number++;
}
else
return stringtoint[s];
}
void dfs(int u, int& head, int& totalnumber, int& totalweight)
{
Collected[u] = ;
totalnumber++;
if (weight[head] < weight[u])
head = u;
for (int v = ; v < number; v++)
{
if (G[u][v])
{
totalweight += G[u][v];
G[u][v] = G[v][u] = ;
if (!Collected[v])
dfs(v, head, totalnumber, totalweight);
}
}
}
int main()
{
cin >> N >> K;
string s1, s2;
int w;
for (int i = ; i < N; i++)
{
cin >> s1 >> s2 >> w;
G[STOI(s1)][STOI(s2)]+=w;
G[STOI(s2)][STOI(s1)]+=w;
weight[STOI(s1)] += w;
weight[STOI(s2)] += w;
}
for (int i = ; i < number; i++)
{
int head = i;
int totalnumber = ;
int totalweight = ;
if (!Collected[i])
dfs(i, head, totalnumber, totalweight);
if (totalnumber > && totalweight > K)
ans[inttostring[head]] = totalnumber;
}
cout << ans.size() << endl;
for (auto it : ans)
cout << it.first << " " << it.second << endl;
return ;
}
 

1034 Head of a Gang (30分)(dfs 利用map)的更多相关文章

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

  2. 【PAT甲级】1034 Head of a Gang (30 分)

    题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...

  3. pat 甲级 1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

  4. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  5. 1034. Head of a Gang (30)

    分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...

  6. 1034 Head of a Gang (30)(30 分)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  7. PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

    题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

  8. PAT 1034. Head of a Gang (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...

  9. 1034. Head of a Gang (30) -string离散化 -map应用 -并查集

    题目如下: One way that the police finds the head of a gang is to check people's phone calls. If there is ...

随机推荐

  1. 编写程序实现根据考试成绩将成绩分为A,B,C,D四档。

    score = float(input("请输入你的成绩:"))if 90 <= score <= 100: print("你的成绩为A档")eli ...

  2. elementui 在原生方法参数里,添加参数

    公司有个项目需求需要在一个列表中分别上传图片,饿了么的方法不支持传递index,可以这样传递: :on-change="(file,fileList)=>{return changeF ...

  3. R调用C++示例

    sourceCpp {Rcpp}:Source C++ Code from a File or String sourceCpp(file = "", code = NULL, e ...

  4. 洛谷P1000超级马里奥的神奇解法

    话说上过洛谷的都知道,有一道经典例题P1000超级马里奥,这一题,可以说是非常简单非常经典,但是就算如此,还是可以人才辈出,我是个比较循规蹈矩的人(雾),所以我的代码就比较平常,也就是直接输出了所要求 ...

  5. ClassLoader&双亲委派&类初始化过程

    1.class sycle 类加载的生命周期:加载(Loading)–>验证(Verification)–>准备(Preparation)–>解析(Resolution)–>初 ...

  6. (转)协议森林08 不放弃 (TCP协议与流通信)

    协议森林08 不放弃 (TCP协议与流通信) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! TCP(Transportation ...

  7. JetBrains Quest 3

    起因 今天早上看了一篇博文: 此时的我:"哎,这等好事不等我!" 然后......我打开官方推特: "什么?最后的任务?" 于是就有了这篇文章 开始操作 1.神 ...

  8. 文件合并cat and paste

    cat 纵向合并 cat file1 file 2 paset横向合并 wc用法 sort用法

  9. [Linux][C][gcc] Linux GCC 编译链接 报错ex: ./libxxx.so: undefined reference to `shm_open'

    本人原创文章,文章是在此代码github/note的基础上进行补充,转载请注明出处:https://github.com/dramalife/note. 以librt丶用户自定义动态库libxxx 和 ...

  10. JDK dump

    1. 查看整个JVM内存状态 jmap -heap 1237(pid) 2.生成dump文件 jmap -dump:file=文件名.dump 1237(pid)