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
读懂题之后就想用图的知识做,每个连通分支如果结点个数不超过2直接不考虑,所有边权值和不超过k也不考虑,搜索的过程中找到当前连通分支权值最大的点,给出的是字符串,可以hashing一个值,这个值不是连续的,再对应到连续的值1~c,
题目中最多不超过2000 = 2 * 1000,然后就可以用mp记录边的值了,累加的,val记录点的值,也是累加的,这样一来,邻接矩阵就有了。。。然后进行搜多,vis标记访问,最后把答案排序。。。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
struct info {///记录答案 写成结构体方便排序 头 和 数量
int id,num;///id就是 head of a gang , num是人数
info() {
id = num = ;
}
}ans[];///记录答案
int n,k,c,d,pos[],mp[][],vis[],val[],ant,temp;///pos记录hashing值对应1~c的第几个值
char s1[],s2[],s[][];
int getval(char *t) {///hashing值
return (t[] - 'A') * * + (t[] - 'A') * + t[] - 'A';
}
void dfs(int p) {///搜索某个连通分支
vis[p] = ;///标记
ans[ant].num ++;///人数加一
if(val[p] > val[ans[ant].id])ans[ant].id = p;///更新head
for(int i = ;i <= c;i ++) {
if(!mp[p][i])continue;///无权边 即为 两人无关
if(p < i)temp += mp[p][i];///仅当p < i 就是按同一个方向 累加关系值 防止重复和漏加
if(vis[i] || !mp[p][i])continue;
dfs(i);
}
}
bool cmp(info a,info b) {
return strcmp(s[a.id],s[b.id]) < ;
}
int main() {
scanf("%d%d",&n,&k);
for(int i = ;i < n;i ++) {
scanf("%s%s%d",s1,s2,&d);
int x,y;
if(!pos[getval(s1)]) {
x = pos[getval(s1)] = ++ c;
strcpy(s[x],s1);///反过来要知道 pos对应的字符串
}
else x = pos[getval(s1)];
if(!pos[getval(s2)]) {
y = pos[getval(s2)] = ++ c;
strcpy(s[y],s2);
}
else y = pos[getval(s2)];
mp[x][y] += d;
mp[y][x] += d;
val[x] += d;
val[y] += d;
}
for(int i = ;i <= c;i ++) {
if(!vis[i]) {
temp = ;
dfs(i);
if(ans[ant].num > && temp > k)ant ++;///满足条件才算
else ans[ant].num = ,ans[ant].id = ;
}
}
printf("%d\n",ant);
sort(ans,ans + ant,cmp);
for(int i = ;i < ant;i ++) {
printf("%s %d\n",s[ans[i].id],ans[i].num);
}
}

1034 Head of a Gang (30)(30 分)的更多相关文章

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

  2. 1034 Head of a Gang (30 分)(图的遍历or并查集)

    dfs #include<bits/stdc++.h> using namespace std; ; int mp[N][N]; int weight[N]; int vis[N]; ma ...

  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 甲级 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 ...

  5. pat 甲级 1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

  6. PAT 1034 Head of a Gang[难][dfs]

    1034 Head of a Gang (30)(30 分) One way that the police finds the head of a gang is to check people's ...

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

  8. etectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30))

    # 函数原型detectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30)) # gray需要识别的图片 # 1.03:表示每次图像尺寸减小 ...

  9. PAT甲级1034. Head of a Gang

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

  10. c# 时间格式处理,获取格式: 2014-04-12T12:30:30+08:00

    C#  时间格式处理,获取格式: 2014-04-12T12:30:30+08:00 一.获取格式: 2014-04-12T12:30:30+08:00 方案一:(局限性,当不是当前时间时不能使用) ...

随机推荐

  1. 封装CLLocationManager定位获取经纬度

    创建调用方法,在.h文件里 #import <Foundation/Foundation.h> @interface RMMapLocation : NSObject { void (^s ...

  2. 高性能HTTP加速器Varnish安装与配置(包含常见错误)

    Varnish是一款高性能的开源HTTP加速器.挪威最大的在线报纸Verdens Gang使用3台Varnish取代了原来的12台Squid,性能竟然比曾经更好.Varnish 的作者Poul-Hen ...

  3. 前端标签--js--css大致思路

    html标签语言在块级和内联标签的基础上进行页面的设计,设计的时候主要是注意标签块间的距离位置等信息,设计盒子的浮动,盒子的位置,盒子之间的联系. 在设计网页之前一定要判断好该设计多少个盒子,什么样的 ...

  4. struct timeval 和 struct timespec

    struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 測试代码例如以下: #include <stdio.h> #include ...

  5. JS常见事件以及函数

    1.js enter键激发事件 document.onkeydown = function (e) {            if (!e) e = window.event;             ...

  6. Docker入门系列5:常见问题小结

    重启容器 再次运行容器: docker start container_id 然后 docker attach container_id 就可以继续下命令了. [编辑]命名 --name [编辑]端口 ...

  7. 自定义一个处理图片的HttpHandler

    有时项目里我们必须将图片进行一定的操作,例如水印,下载等,为了方便和管理我们可以自定义一个HttpHander 来负责这些工作 后台: public class ImageHandler : IHtt ...

  8. COGS1752. [BOI2007]摩基亚Mokia

    1752. [BOI2007]摩基亚Mokia ★★☆   输入文件:mokia.in   输出文件:mokia.out   简单对比时间限制:5 s   内存限制:128 MB [题目描述] 摩尔瓦 ...

  9. 【BZOJ4861】[Beijing2017]魔法咒语 矩阵乘法+AC自动机+DP

    [BZOJ4861][Beijing2017]魔法咒语 题意:别看BZ的题面了,去看LOJ的题面吧~ 题解:显然,数据范围明显的分成了两部分:一个是L很小,每个基本词汇长度未知:一个是L很大,每个基本 ...

  10. EasyRTMP+EasyDSS实现一套完整的紧急视频回传直播与存储回放方案

    需求来源 紧急视频回传云端:即拍即传.云端存储.紧急录像.云拍云录!这些需求现在可能对于我们来说比较远,大部分也是在行业中才会用到,但相信在不就的将来肯定会落地到每个人的手中,因为这是一个自我保护.自 ...