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 ...
随机推荐
- 《java入门第一季》之Integer类和自动拆装箱概述
/ * int 仅仅是一个基本类型.int有对应的类类型,那就是Integer. * 为了对基本数据类型进行更多的操作,更方便的操作,Java就针对每一种基本数据类型提供了对应的类类型--包装类类型 ...
- Cocos2d中update与fixedUpdate的区别(六)
它如何工作呢? update:和fixedUpdate:方法实际这样工作. Cocos2D将从iOS接口中取得时间间隔(delta)在你的游戏代码执行期间,并且检查fixedUpdate:方法在间隔期 ...
- Leetcode_141_Linked List Cycle
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42833739 Given a linked list, d ...
- umask函数的用法 - 如何进行权限位的设置
下面程序创建了两个文件,创建foo文件时,umask值为0,创建第二个时,umask值禁止所有组和其他用户的访问权限. 测试结果: 测试结果可以看出更改进程的文件模式掩码并不影响其父进程(常常是she ...
- HashMap二三事
先看看hashmap在整个Collection中的位置 HashMap中存储数据的结构是 /** * The table, resized as necessary. Length MUST Alwa ...
- 网站开发进阶(十)如何将一个html页面嵌套在另一个页面中
如何将一个html页面嵌套在另一个页面中 1.IFrame引入 <IFRAME NAME="content_frame" width=100% height=30 margi ...
- LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II
1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...
- 面试心得随谈&线程并发的总结
---恢复内容开始--- 线程同步有两种实现方式: 基于用户模式实现和用内核对象实现.前者偏于轻量级,性能也更好,但是只能用于同一进程间的线程同步,后者重量级,性能消耗更大,跨进程. 研读了一下win ...
- 从小故事来谈nginx负载均衡
负载均衡 负载均衡是任何一个有一定规模的互联网企业都会考虑的问题,负载方式很多,有依靠硬件实现的,也有依靠软件实现负载的. 今天来聊聊使用软件来负载的方式 你可能听过各自负载的方式,比如常见的ngin ...
- TensorFlow学习记录(一)
windows下的安装: 首先访问https://storage.googleapis.com/tensorflow/ 找到对应操作系统下,对应python版本,对应python位数的whl,下载. ...