PAT1034;Head of a Gang
1034. Head of a Gang (30)
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 思路
图的连通性和dfs问题。
1.map + vector构造一个图,另外用一个map统计每个人的weight,一个map储存满足条件的黑帮,一个map负责标记一个节点是否访问过。
2.dfs时根据每个人的weight不断更新黑帮老大head,并统计相关联的节点数countNode,sum为该黑帮内所有人weight之和。
注:sum/2其实就是黑帮的总通话时长。
3.将满足条件(sum/2)> K 和黑帮人数在2人以上(countNode > 2)的黑帮数据储存到cluster中。
4.cluster为空输出0,否则遍历输出。 代码
#include<map>
#include<vector>
#include<iostream>
using namespace std; map<string,int> weight;
map<string,int> cluster;
map<string,vector<string>> graph;
map<string,bool> visits; void dfs(const string& a,string& head,int& countNode,int& sum)
{
int curmax = weight[head];
if(weight[a] > curmax)
head = a;
countNode++;
sum += weight[a];
visits[a] = true;
for(int i = 0; i < graph[a].size(); i++)
{
if(!visits[graph[a][i]])
dfs(graph[a][i],head,countNode,sum);
}
} int main()
{
int N,K;
while(cin >> N >> K)
{
for(int i = 0; i < N; i++)
{
string a,b;
int w;
cin >> a >> b >> w;
weight[a] += w;
weight[b] += w;
graph[b].push_back(a);
graph[a].push_back(b);
visits[a] = visits[b] = false;
}
int cnt = 0;
for(auto it = graph.begin(); it != graph.end(); it++)
{
if(visits[it->first])
continue;
cnt++;
string head = it->first;
int countNode = 0,sum = 0;
dfs(it->first,head,countNode,sum);
if((sum/2) > K && countNode > 2)
cluster[head] = countNode;
else
cnt--;
}
if(cluster.empty())
{
cout << 0 << endl;
continue;
}
cout << cnt << endl;
for(auto it = cluster.begin(); it != cluster.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
}
}
PAT1034;Head of a Gang的更多相关文章
- pat1034. Head of a Gang (30)
1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...
- PAT1034. Head of a Gang ——离散化+并查集
题意:成员A与成员B通话 ,成员B与成员C通话,则 ABC即为一个团伙,一共有若干个团伙,每个团伙的人数大于2且相互通话时间超过一定值即为黑帮,每个黑帮伙里有一个BOSS,boss是与各个成员打电话最 ...
- 1034. Head of a Gang (30)
分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...
- [BZOJ1370][Baltic2003]Gang团伙
[BZOJ1370][Baltic2003]Gang团伙 试题描述 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: ...
- Head of a Gang (map+邻接表+DFS)
One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...
- 九度OJ 1446 Head of a Gang -- 并查集
题目地址:http://ac.jobdu.com/problem.php?pid=1446 题目描述: One way that the police finds the head of a gang ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- 1034. Head of a Gang
One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...
- 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 ...
随机推荐
- InfoQ访谈:Webkit和HTML5的现状和趋势
原网址: http://www.infoq.com/cn/interviews/status-and-trends-of-webkit-and-html5 个人一些不成熟的见解,望讨论和指正. 节选 ...
- Leetcode_144_Binary Tree Preorder Traversal
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42876699 Given a binary tree, r ...
- TrueType字体的后缀名解释
OpenType标准定义了OpenType文件名称的后缀名.包含TureType字体的OpenType文件后缀名为.ttf,包含PostScript字体的文件后缀名为.OTF.如果是包含一系列True ...
- 如何取得ChipmunkConstraint实例对象的私有属性
在 如何用代码禁用SpriteBuilder中创建的关节 一篇中提到了要想禁用一个关节就需要将其无效化. 然后我们在重新创建新关节时,可以参考该关节的原始参数. 但是代码中只能直接访问到bodyA和b ...
- VirtualBox安装RHEL之后配置桥接网络
VirtualBox安装RHEL之后配置桥接网络 1 如果主机是Intel (R) Ethernet Connection I217-LM上网的: 2 如果主机是无线上网的, 如ipconfig显示如 ...
- OpenCV 直线检测
/*------------------------------------------------------------------------------------------*\ This ...
- 巧用FineReport搭建成本管控监测系统
一.应用背景 企业在近几年快速发展,规模也越来越大,而信息传递与反馈手段却仍然比较落后,随着信息技术的不断发展,人们开始通过尝试利用技术手段改善这种环境.企业的项目不断增多,而作为高层管理者,通过层级 ...
- IOS 与ANDROID框架及应用开发模式对比一
IOS 和ANDROID操作系统都是目前流行的移动操作系统,被移动终端和智能设备大量采用,两者都采用了先进的软件技术进行设计,为了方便应用开发两者都采用了先进的设计模式.两者在框架设计上都采用了什么技 ...
- XMPP系列(二)----用户注册和用户登录功能
1.创建一个新工程 2.导入XMPP框架 最新的XMPP框架下载地址:https://github.com/robbiehanson/XMPPFramework 将XMPP的几个文件夹拖进工程中,需要 ...
- web报表工具FineReport的JS编辑框和URL地址栏语法简介
JS编辑框: 1.FineReport的js. 作为一款BS产品,browser端的JavaScript是必不可少的. FineReport中的js是已经调用了finereport.js的. 大家知道 ...