【PAT 1034】 Head of a Gang 图论DFS

2024-04-05 06:18
文章标签 图论 dfs head pat 1034 gang

本文主要是介绍【PAT 1034】 Head of a Gang 图论DFS,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1034. Head of a Gang (30)

时间限制
100 ms
内存限制
32000 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 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 N and 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
题意:

『gang』翻译过来是『一伙人』。gang 的定义是一群人,至少有 3 个人,这群人中每个人之间都通过通话相连,且整个群体的通话时长超过一个阈值。整个 gang 的团体中,拥有的电话时长最长的人就是头目了。

分析:

使用 vector<int> arr[LEN];存储该有向图,并求得每个节点的度数(入度+出度);
采用DFS搜索所有连通子图;
判断连通子图中节点个数及总度数是否满足Gang条件;
若满足则记录下度数最大的节点及节点个数;
对结果排序后输出。

代码:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>using namespace std;//此代码使用前,需删除下面两行+后面的system("PAUSE")
ifstream fin("in.txt");
#define cin finstruct HeadNum{int name;int num;HeadNum(int nam,int n){name = nam;num=n;}
};const int LEN = 26*26*26+1;
vector<int> arr[LEN];
int sum[LEN]={0};
bool isVisited[LEN]={false};
vector<int> linkNode;int cmp(const HeadNum& aa,const HeadNum &bb)
{return aa.name < bb.name;
}int hashName(const char name[]){		//将字符数组hash成整型return (name[0]-'A')*26*26+(name[1]-'A')*26+name[2]-'A';
}void int_to_name(int n,char *s){		//整型转换回 字符数组s[3]='\0';  s[2]=n%26+'A';  s[1]=(n/26)%26+'A';  s[0]=n/(26*26)+'A';  
} void dfs(int head){					//深度优先搜索if(isVisited[head])return;isVisited[head] = true;int size = arr[head].size();for(int i=0;i<size;i++){dfs(arr[head][i]);}	linkNode.push_back(head);return;
}HeadNum* isGang(const vector<int> &v,int k){		//判断是否算的上一个Gang团伙int s = v.size();if(s<3)return NULL;			//人数小于3人不构成团伙int total = 0;int max = 0;int head = -1;for(int i=0;i<s;i++){total += sum[v[i]];			//计算一个团伙的总时长(每段时长都被计算2次)if(max < sum[v[i]]){max = sum[v[i]];head = v[i];}}if(total/2 <= k){		//这里要除以2return NULL;}else{return new HeadNum(head,s);}
}
int main()
{int n,k;cin>>n>>k;queue<int> node;vector<HeadNum> res;int i;char name1[4],name2[4];int intName1,intName2,value;for(i=0;i<n;i++){cin>>name1>>name2;intName1 = hashName(name1);intName2 = hashName(name2);arr[intName1].push_back(intName2);cin>>value;sum[intName1] += value;sum[intName2] += value;node.push(intName1);}HeadNum* head = NULL;while(!node.empty()){linkNode.clear();		//linkNode存储每次联通子图dfs(node.front());node.pop();head = isGang(linkNode,k);		//判断联通子图是否满足团伙条件,若满足则返回{头目姓名,团伙人数}if(head!=NULL){res.push_back(*head);}}sort(res.begin(),res.end(),cmp);	//将结果按照 head姓名进行排序cout<<res.size()<<endl;char name[4];for(i=0;i<res.size();i++){int_to_name(res[i].name,name);		//整型转换回字符数组cout<<name<<' '<<res[i].num<<endl;}system("PAUSE");return 0;
}


这篇关于【PAT 1034】 Head of a Gang 图论DFS的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/877823

相关文章

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

poj 3050 dfs + set的妙用

题意: 给一个5x5的矩阵,求由多少个由连续6个元素组成的不一样的字符的个数。 解析: dfs + set去重搞定。 代码: #include <iostream>#include <cstdio>#include <set>#include <cstdlib>#include <algorithm>#include <cstring>#include <cm

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

深度优先(DFS)和广度优先(BFS)——算法

深度优先 深度优先搜索算法(英语:Depth-First-Search,DFS)是一种用于遍历或搜索树或图的算法。 沿着树的深度遍历树的节点,尽可能深的搜索树的分支,当节点v的所在边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点。这一过程一直进行到已发现从源节点可达的所有节点为止。如果还存在未被发现的节点,则选择其中一个作为源节点并重复以上过程,整个进程反复进行直到所有节点都被访

跟我一起玩《linux内核设计的艺术》第1章(四)——from setup.s to head.s,这回一定让main滚出来!(已解封)

看到书上1.3的大标题,以为马上就要见着main了,其实啊,还早着呢,光看setup.s和head.s的代码量就知道,跟bootsect.s没有可比性,真多……这确实需要包括我在内的大家多一些耐心,相信见着main后,大家的信心和干劲会上一个台阶,加油! 既然上篇已经玩转gdb,接下来的讲解肯定是边调试边分析书上的内容,纯理论讲解其实我并不在行。 setup.s: 目标:争取把setup.

ElasticSearch 6.1.1 通过Head插件,新建索引,添加文档,及其查询数据

ElasticSearch 6.1.1 通过Head插件,新建索引,添加文档,及其查询; 一、首先启动相关服务: 二、新建一个film索引: 三、建立映射: 1、通过Head插件: POST http://192.168.1.111:9200/film/_mapping/dongzuo/ {"properties": {"title": {"type":

Windows环境下ElasticSearch6.1.1版本安装Head插件

安装Head插件步骤如下: 1、下载node.js ,网址:https://nodejs.org/en/ 安装node到D盘。如D:\nodejs。 把NODE_HOME设置到环境变量里(安装包也可以自动加入PATH环境变量)。测试一下node是否生效: 2、安装grunt grunt是一个很方便的构建工具,可以进行打包压缩、测试、执行等等的工作,5.0里的head插件就是通过grunt

nyoj99(并查集+欧拉路+dfs)

单词拼接 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 5 描述 给你一些单词,请你判断能否把它们首尾串起来串成一串。 前一个单词的结尾应该与下一个单词的道字母相同。 如 aloha dog arachnid gopher tiger rat   可以拼接成:aloha.arachnid.dog.gopher.rat.tiger 输入 第一行是一个整

【代码随想录训练营第42期 续Day52打卡 - 图论Part3 - 卡码网 103. 水流问题 104. 建造最大岛屿

目录 一、做题心得 二、题目与题解 题目一:卡码网 103. 水流问题 题目链接 题解:DFS 题目二:卡码网 104. 建造最大岛屿 题目链接 题解:DFS  三、小结 一、做题心得 也是成功补上昨天的打卡了。 这里继续图论章节,还是选择使用 DFS 来解决这类搜索问题(单纯因为我更熟悉 DFS 一点),今天补卡的是水流问题和岛屿问题。个人感觉这一章节题对于刚