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 用户登录密码md5加密

    jade文件 div.login ul.inp-content  li span= '用户名:' input.ui-input1#input1(placeholder='请输入手机号')  li sp ...

  2. AT2386 Colorful Hats (乱搞题,思维题)

    分情况讨论的神题... max不等于min + 1 或者不等于min,这种情况显然不存在. 如果都等于一个数 有两种情况: 互相独立,那么a[i]肯定==n-1 有相同的,那么a[i]一定不是独立的. ...

  3. rsync常用命令和使用方法

    rsync是一个远程数据同步工具,可以实现数据的增量备份,这点比scp要好,scp只能全量备份.同步可以保持文件原有属性,传输过程加密,数据传输全. rsync 的传输模式有:        1. 本 ...

  4. git Bash 学习

    ,ranh新建一个本地仓库并与github连接的方法 注:该终端也具有按tab键补全功能,应该合理应用 1. 新建一个文件夹,并将git bash的位置转到相应文件夹下(cd 命令转移) 2.git ...

  5. jQuery.each() - jQuery 遍历方法使用介绍

    定义和用法 each() 方法规定为每个匹配元素规定运行的函数. 提示:返回 false 可用于及早停止循环. jQuery.each()方法大概有如下几种用法,下面分别进行介绍: 1.选择器.eac ...

  6. 模块numpy的用法

    https://blog.csdn.net/qq351469076/article/details/78817378 机器学习三剑客之Numpy: https://www.jianshu.com/p/ ...

  7. Codeforces:68A-Irrational problem(暴力大法好)

    A- Irrational problem p Time Limit: 2000MS Memory Limit: 262144K 64bit IO Format: %I64d& %I64 De ...

  8. 手机注册过哪些网站37kfenxi.com,查询注册过哪些网站

    注册过哪些网站?发现这么一个网站,https://www.37kfenxi.com?_=cnblogs 可以根据手机号码查询注册过哪些网站,然后通过大数据分析出机主的性格,爱好等. 据说还可以查老板, ...

  9. sql中over的用法

    over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用.其参数:over(partition by columnname1 order by c ...

  10. Android开发——HandlerThread以及IntentService详解

    .HandlerThread Android API提供了HandlerThread来创建线程.官网的解释是: //Handy class for starting a new thread that ...