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. Java反射---对象池

    在很多Java  EE 框架中都需要根据配置文件信息来创建Java对象,从配置文件读取的只是i某个类的字符串类名,程序就需要根据该字符串来创建对应的实例,就必须使用反射. 下面程序就实现了一个简单的对 ...

  2. Ubuntu快速截图

    以前截图,都是按Print键全屏截图,Alt+Print可以截当前的窗口.同时把系统自带的截图工具放到面板上,用的时候点击一下,再选择区域截图,很是不方便.不过,Ubuntu允许自己定义快捷键.要自己 ...

  3. java垃圾回收机制,以及常用的回收算法

    记得之前去平安面试的时候,面试官问到了垃圾回收,我当时也就是说说了垃圾回收的原理,但是具体有哪些实现策略,我当时是懵的. 概念: Java的垃圾回收机制是Java虚拟机提供的能力,用于在空闲时间以不定 ...

  4. 64位ubuntu14.04配置adb后提示没有那个文件或目录

    1.配置完adb环境变量后在终端输入adb: ameyume@ameyume-HP-450-Notebook-PC:~$ adb /home/ameyume/adt-bundle-linux-x86_ ...

  5. Linux网络设置(第二版) --互联网寻址过程

    Linux网络设置 --互联网寻址过程 1.TCP/IP与OSI参考模型比较 TCP/IP OSI 物理层 网卡 数据链路层 * MAC地址 网络层 IP,ICMP,ARP协议 传输层 TCP,UDP ...

  6. 根据分析查看相关知识点分析iOS 三种录制视频方式

    这篇文章讨论了关于如何配置视频捕获管线 (pipeline) 和最大限度地利用硬件性能的一些不同选择. 这里有个使用了不同管线的样例 app,可以在 GitHub 查看. 第一种:UIImagePic ...

  7. Visual studio2010和Modelsim配置SystemC开发(转)

    本文转自一博文. 一.编译System库 1. 下载SystemC library source code, 到http://www.systemc.org注册会员账号后,即可下载SystemC li ...

  8. iOS自定义多参数类型方法

    前几天做自定义UIAlertView的时候,想仿造系统自带的初始化方法做一个AlertView,里面涉及到不确定多参数的设置和使用问题.这里做一下记录. 我自定义了一个方法: - (instancet ...

  9. 开源项目AndroidReview学习小结(2)

    读书破万卷下笔如有神 作为入门级的android码农的我,还是需要多多研读开源代码 下面继续接着上一篇的分析,这一篇主要介绍第一个tab,ReviewFragment的分析,界面看起来简单,背后的逻辑 ...

  10. LeetCode之旅(15)-Odd Even Linked List

    题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...