1034. Head of a Gang (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. pat1034. Head of a Gang (30)

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

  2. PAT1034. Head of a Gang ——离散化+并查集

    题意:成员A与成员B通话 ,成员B与成员C通话,则 ABC即为一个团伙,一共有若干个团伙,每个团伙的人数大于2且相互通话时间超过一定值即为黑帮,每个黑帮伙里有一个BOSS,boss是与各个成员打电话最 ...

  3. 1034. Head of a Gang (30)

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

  4. [BZOJ1370][Baltic2003]Gang团伙

    [BZOJ1370][Baltic2003]Gang团伙 试题描述 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: ...

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

  6. 九度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 ...

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

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

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

  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. mysql语法、特殊符号及正则表达式的使用

    http://blog.csdn.net/pipisorry/article/details/46773545 sql语言 结构化的查询语言.(Structured Query Language),是 ...

  2. mysql进阶(十七)Cannot Connect to Database Server

    Cannot Connect to Database Server 缘由 由于不同的项目中使用的数据库用户名与密码出现了不一致的情况,在其中之前较早一个项目执行过程中出现"The user  ...

  3. 主流列式数据库评测:InfiniDB

    ).本文测试的InfiniDB版本是2010年12月20日发布的2.02版,下载文件名分别为InfiniDB64-2.0.2-2.exe 和InfiniDB64-ent-2.0.2-2.exe.安装文 ...

  4. hibernate 动态多数据库

    最近老师给了一个任务,需求是这样的 服务器A上有一张表,里面存放了若干个服务器的信息,表的字段包括: private int id; private String serverName; privat ...

  5. AngularJS进阶(十一)AngularJS实现表格数据的编辑,更新和删除

    AngularJS实现表格数据的编辑,更新和删除 效果 实现 首先,我们先建立一些数据,当然你可以从你任何地方读出你的数据 var app = angular.module('plunker', [' ...

  6. OpenCV 闭合轮廓检测

    这个好像是骨头什么的,但是要求轮廓闭合,于是对图片进行一下膨胀操作,再次检测轮廓就好了. // A closed contour.cpp : 定义控制台应用程序的入口点. // #include &q ...

  7. 使用Gradle发布SNAPSHOT版本到JCenter(oss.jfrog.org)

    回顾历史 发布SNAPSHOT版本的问题 解决问题 完整脚本 使用方法 本文原创. 转载请注明CSDN博客出处: http://blog.csdn.net/maosidiaoxian/article/ ...

  8. Android UI之View的加载机制(二)

    转载请标明出处:http://blog.csdn.net/sk719887916/article/details/39961201,作者:skay 对于接触安卓开不到一年的自己来说,总结下view的生 ...

  9. objective-c中关于类型编码的解释

    在某些情况下,我们需要动态的向一个类插入一个实例方法(也可以是一个类方法):这时我们可以用class_addMethod函数来完成: BOOL class_addMethod ( Class cls, ...

  10. LeetCode(35)-Path Sum

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...