pat 甲级 1034. Head of a Gang (30)
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 threshold, 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 题意:gang是一个帮派群体,现在给出一张图,找到这张图上的所有帮派以及每个帮派的头目和帮派人数。其中要成为一个帮派,要满足以下要求:
1:帮派人数大于等于3 2:帮派中人与人的通话总时长要高于一个界限threshhold.
思路:dfs
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 10000+5
typedef long long ll;
struct gang{
int num;
string head;
gang() {}
gang(int num,string head):num(num),head(head) {}
bool operator < (const gang&b) {
return head < b.head;
}
};
vector<gang>res;
int n,m, threshold;
map<string, int>Map;
bool is_connect[N_MAX][N_MAX];
string name[N_MAX];
int weight[N_MAX],vis[N_MAX]; int max_time = , id,num=,sum=; bool flag = ;
void dfs(int x) {
sum += weight[x];
num++;
if (sum/ > threshold)flag = true;
vis[x] = true;
if (max_time < weight[x]) {
max_time = weight[x];
id = x;
}
for (int i = ; i < n;i++) {
if (!is_connect[x][i]||vis[i])continue;
dfs(i);
}
} int main() {
while (cin>>m>>threshold) {
n = ;//n-1为人数
for (int i = ; i < m;i++) {
string from, to; int cost;
cin >> from >> to >> cost;
if (Map[from] == ) { Map[from] = n; name[n++] = from; }
if (Map[to] == ) { Map[to] = n; name[n++] = to; }
is_connect[Map[from]][Map[to]] = ;
is_connect[Map[to]][Map[from]] = ;
weight[Map[from]] += cost; weight[Map[to]] += cost;
}
for (int i = ; i < n;i++) {
max_time = ,flag = ,num=,sum=;
if (!vis[i]){
dfs(i);
if (flag&&num>) {
res.push_back(gang(num, name[id]));
}
}
}
sort(res.begin(),res.end());
printf("%d\n",res.size());
for (int i = ; i < res.size();i++) {
printf("%s %d\n",res[i].head.c_str(),res[i].num);
} }
return ;
}
pat 甲级 1034. Head of a Gang (30)的更多相关文章
- PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)
1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's p ...
- PAT甲级1034. Head of a Gang
PAT甲级1034. Head of a Gang 题意: 警方找到一个帮派的头的一种方式是检查人民的电话.如果A和B之间有电话,我们说A和B是相关的.关系的权重被定义为两人之间所有电话的总时间长度. ...
- pat 甲级 1034 ( 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 pho ...
- PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]
题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...
- PAT甲级1034 Head of a Gang【bfs】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...
- 【PAT甲级】1034 Head of a Gang (30 分)
题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- PAT甲题题解-1034. Head of a Gang (30)-并查集
给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...
- PAT (Advanced Level) 1034. Head of a Gang (30)
简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
随机推荐
- c++ 作业 10月13日 进制转换最简单方法,控制c++输出格式方法 教材50的表格自己实践一下 例题3.1 setfill() setw()
#include <iostream> #include <iomanip> using namespace std; int main(){ // int i; // cou ...
- 1074: [SCOI2007]折纸origami
Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 372 Solved: 229[Submit][Status][Discuss] Descriptio ...
- JavaScript设置div中的文字滚动起来 实现滚动效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux系统kernel参数优化
目录 iptables相关 单进程最大打开文件数限制 内核TCP参数方面 内核其他TCP参数说明 众所周知在默认参数情况下Linux对高并发支持并不好,主要受限于单进程最大打开文件数限制.内核TCP参 ...
- Linux基础-Linux常用命令
Linux(/'lainʌks/)系统特点:稳定,安全,开源(一切皆文件) 装上SSH协议就可以连接Linux 装虚拟机(SSH) win用xshell工具 Linux命令:每日一个linux命令 p ...
- ubuntu 压缩 解压 命令大全
ubuntu下文件压缩/解压缩命令总结 http://blog.csdn.net/luo86106/article/details/6946255 .gz 解压1:gunzip FileName.gz ...
- python3+openCV实现图片的人脸人眼检测,原理+参数+源代码
上学时候用matlab学过一些图像处理的基础知识,当时课程作业是用haar实现人脸检测 but当时是心思根本不在图像处理上,so找了个同学帮忙做的,自己没上心 然鹅天道好轮回,现在捡起来了原来的算法一 ...
- has value '1.8', but '1.7' is required
使用java7,自己又想在空闲时间学一些java8的新特性,故在安装完1.7之后又安装了1.8 eclispe在启动时报’has value ‘1.8’,but’1.7’ is required’的错 ...
- HDU2586 How far away ?
一.描述 很久没写代码了,在之前一直在参与准备ASC比赛和美赛,现在又重新捡起来.目标是两个月后的邀请赛. 这题是树链拋分解决LCA问题的一个模板题. 首先介绍下树链拋分的基本思想. 对于任意一颗树, ...
- Gym 100829S_surf 动态规划的优化
题目大意是,非你若干个任务,任务分别对应开始时间.预期收益.持续时间三项指标,让你从中选择一个受益最大的方案(没有开始时间相同的任务). 于是,标准状态转移方程应当为,设DP[K]为选择了前K个任务的 ...