Source:

PAT A1034 Head of a Gang (30 分)

Description:

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 threthold 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 Nand K (both less than or equal to 1000), the number of phone calls and the weight threthold, 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

Keys:

Attention:

  • 给出N条边,最多可能有2*N个结点;
  • 边权累加的操作如果放在 if 里面的话,图中如果回路,会少数一条边;
  • 边权累加的操作如果放在 if 外面的话,相当于各个边数了两次,结果除以2就可以了;
  • 边权累加的操作正常应该放在 if(grap[u][v]!=INF)的里面,不过这题边权初始化为0,加上无效边不影响结果;
  • 输入的时候预处理各个结点的边权之和,是个小的tips,可以注意一下;

Code:

 /*
Data: 2019-04-14 19:46:23
Problem: PAT_A1034#Head of a Gang
AC: 61:22 题目大意:
A和B有通话总时长表示他们的联系程度,
如果一撮人通话总时长超过某一阈值,则认定为“犯罪团伙”,其中电话打的最多的就是头目;
输入:
第一行给出通话数目N和阈值K(<=1e3)
接下来N行给出 v1 v2 w,其中V用三个大写字母表示,w<=1e3
输出:
第一行输出团伙数目
接下来按照字典序依次输出各个团伙的头目及其人数 基本思路:
预处理各顶点边权之和,
遍历各个连通分量,统计结点数目,边权之和,犯罪头目
map存储犯罪头目及其人数,达到字典序排序的目的
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
const int M=2e3+;
int grap[M][M],vis[M],cost[M],pt=;
int K,k,sum,Max;
map<string,int> mp,gang;
string toS[M],head; int ToN(string s)
{
if(mp[s]==)
{
mp[s]=pt;
toS[pt]=s;
return pt++;
}
else
return mp[s];
} void DFS(int u)
{
vis[u]=;
sum++;
if(cost[u] > Max)
{
Max = cost[u];
head = toS[u];
}
for(int v=; v<pt; v++)
{
k += grap[u][v];
if(grap[u][v]!= && vis[v]==)
DFS(v);
/*若按规矩办事
if(grap[u][v]!=0) //判断是否存在边
{
k += grap[u][v]; //各边数了两次
if(vis[v]==0)
k += grap[u][v]; //若有回路,少数一条边
}
*/
}
} int Travel()
{
fill(vis,vis+M,);
int cnt=;
for(int v=; v<pt; v++)
{
k=;sum=;Max=;
if(vis[v]==)
{
DFS(v);
if(k>K* && sum>)
{
cnt++;
gang[head]=sum;
}
}
}
return cnt;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n;
scanf("%d%d", &n,&K);
fill(grap[],grap[]+M*M,);
fill(cost,cost+M,);
for(int i=; i<n; i++)
{
string s1,s2;
int v1,v2,w;
cin >> s1 >> s2 >> w;
v1 = ToN(s1);
v2 = ToN(s2);
grap[v1][v2]+=w;
grap[v2][v1]+=w;
cost[v1] += w;
cost[v2] += w;
}
int cnt=Travel();
printf("%d\n", cnt);
for(auto it=gang.begin(); it!=gang.end(); it++)
cout << it->first << " " << it->second << endl; return ;
}

PAT_A1034#Head of a Gang的更多相关文章

  1. 1034. Head of a Gang (30)

    分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...

  2. [BZOJ1370][Baltic2003]Gang团伙

    [BZOJ1370][Baltic2003]Gang团伙 试题描述 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: ...

  3. Head of a Gang (map+邻接表+DFS)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  4. 九度OJ 1446 Head of a Gang -- 并查集

    题目地址:http://ac.jobdu.com/problem.php?pid=1446 题目描述: One way that the police finds the head of a gang ...

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

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

  6. 1034. Head of a Gang

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  7. 1034. Head of a Gang (30) -string离散化 -map应用 -并查集

    题目如下: One way that the police finds the head of a gang is to check people's phone calls. If there is ...

  8. PAT1034;Head of a Gang

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

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

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

随机推荐

  1. android NDK开发在本地C/C++源码中设置断点单步调试具体教程

    近期在学android NDK开发,折腾了一天,最终可以成功在ADT中设置断点单步调试本地C/C++源码了.网上关于这方面的资料太少了,并且大都不全,并且调试过程中会出现各种各样的问题,真是非常磨人. ...

  2. 依据矩阵的二维相关系数进行OCR识别

    我想通过简单的模板匹配来进行图像识别. 把预处理好的字符图片,分别与A到Z的样本图片进行模板匹配. 结果最大的表明相关性最大,就能够识别字符图片了. 在实际应用中.我用了openCV的matchTem ...

  3. linux下oracle11G DG搭建(二):环绕主库搭建操作

    linux下oracle11G DG搭建(二):环绕主库搭建操作 环境 名称 主库 备库 主机名 bjsrv shsrv 软件版本号 RedHat Enterprise5.5.Oracle 11g 1 ...

  4. gitserver提交代码的总结

    将更新的代码提交到gitserver上所须要的步骤: 1.git pull 更新代码到最新 2,git add 文件名称 加入要提交的文件 3,git commit  -m  "关于改动的内 ...

  5. linux openssl 编程 Client端

    相关配置等请參看上一篇关于server端文章:http://blog.csdn.net/pingd/article/details/47805349 1.Client端源代码: openssl_cli ...

  6. PCB SLOT槽孔数量计算方法,同CAM350孔数一致 实现方法

    最近有好几个写脚本的朋友问我,SLOT槽孔孔的如何计算的,要求孔数与CAM350孔数保持一致. 前几年通过在CAM350里面不断测试,结果是:CAM 350中SLOT槽孔,孔与孔之间最高位,凸位高度值 ...

  7. PCB MS SQL 排序应用(row_number rank dense_rank NTILE PARTITION)

    一.排序前,准备数据 --表变量 ),流程数 int) insert into @table union all union all union all union all --查看一下 select ...

  8. LeetCode.5-最长回文子串(Longest Palindromic Substring)

    这是悦乐书的第342次更新,第366篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第3题(顺位题号是5).给定一个字符串s,找到s中最长的回文子字符串. 您可以假设s ...

  9. xml转换成数组array

    直接上代码,成功转换 if($data){ //返回来的是xml格式需要转换成数组再提取值,用来做更新 $startnum = strpos($data,"<xml>" ...

  10. Java中数组要点总结

    1.数组是基本数据类型和字符串类型的容器(引用数据类型),而集合是类数据类型的容器: 2.数组定义的格式: (1)一般格式: 元素类型[] 数组名 = new 元素类型[元素个数或者数组长度]: 其中 ...