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. nodejs mysql模块简单封装

    nodejs 简单的封装一些mysql模块 实现一个方法根据不同传参进行增删改查 首先要 npm install mysql 代码如下 function data(objHost,sql,callba ...

  2. 1045: [HAOI2008] 糖果传递

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4897  Solved: 2457[Submit][Status][Discuss] Descript ...

  3. vue 项目中使用mock假数据实现前后端分离

    也是查了很多的资料,整理出来.实现了前后端的分离,用到的技术vue-cli,webpack,node,json-server.首先全局安装json-server cnpm i json-server ...

  4. Python中关于集合的介绍及用法

    一.集合的含义及创建方法 集合(set)是一种无序的并且里面存放不同元素的序列. 集合可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因 ...

  5. DRF工程搭建

    环境安装与配置 DRF需要以下依赖: Python (2.7, 3.2, 3.3, 3.4, 3.5, 3.6) Django (1.10, 1.11, 2.0) DRF是以Django扩展应用的方式 ...

  6. Codeforces Round #456 (Div. 2) A. Tricky Alchemy

    传送门:http://codeforces.com/contest/912/problem/A A. Tricky Alchemy time limit per test1 second memory ...

  7. HDOJ 2120 Ice_cream's world I

    Ice_cream's world I ice_cream's world is a rich country, it has many fertile lands. Today, the queen ...

  8. IOS开发---菜鸟学习之路--(十六)-将Image转换为Base64

    我们直接在.m文件的引用头文件部分 和 @interface   AddPictureViewController () 之间  加入 增加部分的代码 然后就可以使用图片转Base64了 #impor ...

  9. git:多个sshkey配置

    克隆项目: 使用git clone +项目.git地址 例如: 创建SSH Key: ssh-keygen -t rsa -C +邮箱地址 sshkey自定义保存:创建后在第二步(enter file ...

  10. 看似不是dfs的dfs HDU-1455

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...