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

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

  2. PAT甲级1034. Head of a Gang

    PAT甲级1034. Head of a Gang 题意: 警方找到一个帮派的头的一种方式是检查人民的电话.如果A和B之间有电话,我们说A和B是相关的.关系的权重被定义为两人之间所有电话的总时间长度. ...

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

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

  5. PAT甲级1034 Head of a Gang【bfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...

  6. 【PAT甲级】1034 Head of a Gang (30 分)

    题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...

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

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

  8. PAT甲题题解-1034. Head of a Gang (30)-并查集

    给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...

  9. PAT (Advanced Level) 1034. Head of a Gang (30)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

随机推荐

  1. linux中管道(pipe)一谈

    /*********************************************** 管道(pipe)是Linux上进程间通信的一种方式,其是半双工(数据流只能在一个方向上流动(还需要经过 ...

  2. mysql 5.7安装步骤:

    .下载完成后解压: 3.在mysql要目录下创建 my.ini 文件,如上图,文件内容如下,basedir 和 datadir 修改为相应地址: [mysql] # 设置mysql客户端默认字符集 d ...

  3. 二、Linux 系统启动过程

    Linux 系统启动过程 linux启动时我们会看到许多启动信息. Linux系统的启动过程并不是大家想象中的那么复杂,其过程可以分为5个阶段: 内核的引导. 运行 init. 系统初始化. 建立终端 ...

  4. kubernetes搭建dashboard-v1.10.1

    一键部署脚本(或者可使用helm安装): wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/ ...

  5. python字符串处理方法

    一.combine & duplicate 字符串结合和复制 字符和字符串可以用来相加来组合成一个字符串输出: 字符或字符串复制输出. 二.Extract &Slice 字符串提取和切 ...

  6. mac terminal基本命令

    文件目录 首先要清楚几个文件目录: " / "  :根目录 " ~ " :用户主目录的缩写.例如当前用户为esther,那么" ~ "展开来 ...

  7. android静默安装和智能安装(转)

    Android 静默安装和智能安装的实现方法 http://blog.csdn.net/fuchaosz/article/details/51852442 Android静默安装实现方案,仿360手机 ...

  8. 【Word Break】cpp

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  9. 【Decode Ways】cpp

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

  10. 【Single Num II】cpp

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...