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> # ...
随机推荐
- PAT 乙级 1015
题目 题目地址:PAT 乙级 1015 题解 常规题,难点在于理清楚排序规则,通过比较简洁的方式进行编码: 在这里我选择使用vector进行存储,并使用sort方法排序,因为本题不是简单按照大小排序, ...
- 小技巧之padding-bottom实现等比例图片缩放
1.padding-bottom 如果用%来表示的话,计算是根据父元素的width的值进行计算的. 例:父元素.wrapper的width是100px,height设置为0, padding-bott ...
- 深入解析AJAX的原理
AJAX:Asynchronous JavaScript And Xml(异步的JS和XML) 同步:客户端发起请求>服务端的处理和响应>客户端重新载入页面(循环) 异步:客户端实时请求& ...
- 宏基笔记本升级bios(2012-12-28-bd 写的日志迁移
首先到宏基官网下载中心 去下载你需要的新版本的bios安装包如图: 我的是宏基4750g的win7旗舰版64位,这里一定要根据自己的电脑的型号和安装的系统来选择,你可以选择最新的版本也可以选择老的版本 ...
- 【python学习】新手基础程序练习(二)
Ι 继续上一节得内容,这里主要是对各种知识的理解以及如何运用. 一.执行 Python 脚本的两种方式 1.把python执行文件加到计算机的环境变量中,然后新建文件把程序写在新文件里,再通过cmd命 ...
- Tame Me【驯服我】
Tame Me “Good morning,” said the fox. 早上好,狐狸说 “Good morming,” the little prince responded politely,a ...
- CSAPP 缓冲区溢出试验
缓冲区溢出试验是CSAPP课后试验之一,目的是: 更好的理解什么是缓冲区溢出 如何攻击带有缓冲区溢出漏洞的程序 如何编写出更加安全的代码 了解并理解编译器和操作系统为了让程序更加安全而提供的几种特性 ...
- B1023 组个最小数 (20分)
B1023 组个最小数 (20分) 给定数字 0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意 0 不能做首位).例如:给定两个 0,两个 1,三个 ...
- 11,scrapy框架持久化存储
今日总结 基于终端指令的持久化存储 基于管道的持久化存储 今日详情 1.基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的 ...
- BZOJ 4247: 挂饰
背包裸题 #include<cstdio> #include<algorithm> using namespace std; int F[2005]; struct node{ ...