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 ;
}
 

最新文章

  1. 拦截UIViewController的popViewController事件
  2. 阿里云分布式关系数据库DRDS笔记
  3. 如何下载youtube上面的视频
  4. 【C#】【Thread】Semaphore/SemaphoreSlim信号量
  5. 减少HTTP请求之将图片转成二进制并生成Base64编码,可以在网页中通过url查看图片(大型网站优化技术)
  6. jQuery全选与反选,且解决点击只执行一次的问题
  7. 多线程-GCD学习笔记
  8. mysql日志文件相关的配置【1】
  9. overload的一点思考
  10. 重拾CSS基础—开篇
  11. python css功能补充讲解
  12. 【bzoj 3669】[Noi2014]魔法森林
  13. Down Payment 和 Deposit的差异
  14. gcc -02引起内存溢出&#39;unsigned i&#39;应修订为&#39;volatile unsigned i&#39;
  15. redis实现简单的分布式锁
  16. Sketch小妙招:在线分享设计
  17. 【转】Paxos算法2-算法过程
  18. SpringBoot(十一)_springboot热部署
  19. MessageBox.show显示窗口在最上层
  20. iOS的AssetsLibrary框架访问所有相片

热门文章

  1. JAVA生成EXCEL模板
  2. 深入理解yield from语法
  3. MDI设置父子窗体
  4. .NET实现一个简单的IOC容器
  5. kerberos系列之kerberos安装
  6. 懂一点Python系列——快速入门
  7. [C++]请麻烦压一下定理的棺材板啦
  8. python多重继承的属性和方法调用顺序问题和对迭代器的初步理解
  9. UCF Local Programming Contest 2016 J题(二分+bfs)
  10. vue命令式组件和插件编写