PAT A1034 Head of a Gang
admin
2024-02-03 01:15:15
0

1034 Head of a Gang

分数 30

作者 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 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

一个图的遍历,非要想着这么复杂,还想到了并查集。这个题在半年前看算法笔记的时候就看到过,当时还上手实践过,然而今天看到这个题,脑袋依旧是没开窍,这第一个代码就是今天写的,又长又臭。

第二个代码的字符串转换为数字,不是用进制来进行转换的,值得学习。当字符串的长度是七八九十个的时候,用进制转换为数字当作数组的下标,明显不现实,但借助 一个map容器,就可简单将字符串转换为数字的大小控制在字符串的数量以内。看代码实现,这第二个是算法笔记上的代码。

不管怎么说,只要从此掌握了这个转换方法,也不能说不是一种收获。

 * 输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,都需要
 * 用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还需要用到原来
 * 的名字,因此,此时的数字也能返过去访问原来的名字,所以要将名字转换为的数字与
 * 名字相映射,用一个map容器记录就行。
 * 由于是一个不超过三个字符的名字,所以可以直接用26进制进行英文转换为数字。
 *
 * 同一个团伙的成员可以用并查集进行合并,头领记录该团伙的成员数量,其他成员指向
 * 该头领。最后再用图的遍历确定每个团队的首领。
 *
 * 最后注意计算团队总时长 > 阈值k时,题目要求的总时长是总时长/2,即两个人之间的通话
 * 只能算一次,通话时长总计需要除以2。

/*** 输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,都需要* 用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还需要用到原来* 的名字,因此,此时的数字也能返过去访问原来的名字,所以要将名字转换为的数字与* 名字相映射,用一个map容器记录就行。* 由于是一个不超过三个字符的名字,所以可以直接用26进制进行英文转换为数字。* * 同一个团伙的成员可以用并查集进行合并,头领记录该团伙的成员数量,其他成员指向* 该头领。最后再用图的遍历确定每个团队的首领。* * 最后注意计算团队总时长 > 阈值k时,题目要求的总时长是总时长/2,即两个人之间的通话* 只能算一次,通话时长总计需要除以2。
*/#include 
#include 
#include 
#include 
#include 
#include using namespace std;typedef pair PSI;//dfs返回的类型,total记录该团队的成员之间通话的总时间,maxv记录该团伙中打电话时长
//最长的一个人的时长,h记录该成员的字符串映射的数字编号;
struct Node
{int total, maxv, h;
};const int N = 3e5;
int fath[N]; //并查集数组
bool hs[N]; //该成员是否被访问过
map int_nm; //数字映名字
int num[N]; //每个成员的通话时长
vector Adj[N]; //邻接表
set res; //记录结果
int k;//字符串转换为数字
int Stoi(string s)
{int n = 1;for(int i=0; i> n >> k;while(n--){string a, b;int t;cin >> a >> b >> t;Union(a, b, t);}
}Node dfs(int u)
{hs[u] = 1;int total = num[u], maxval = num[u];//cout << int_nm[u] << "->" << num[u] << endl;int h = u;for(int i=0; i" << int_nm[v] << endl;auto ans = dfs(v);//搜索每个团队的首领if(ans.maxv > maxval){h = ans.h;maxval = ans.maxv;}total += ans.total;}}return {total, maxval, h};
}void Deal()
{for(int i=0; i= -2) //s所在的团队成员数量不足2,不满足题意continue;Node ans = dfs(s);//注意此处的团队通话时长总计需要除以2,因为返回的是团队中每个//人的通话总时长,而题目要求的是总时长/2 > 阈值k;if(ans.total/2.0 <= k)   continue; res.insert({int_nm[ans.h], -fath[fau]});// cout << ans.total << "**" << ans.maxv << "**" << ans.h //   << "**" << int_nm[ans.h] << "**" << num[ans.h] << endl;}}}}
}void Print()
{cout << res.size() << endl;for(auto s : res)cout << s.first << ' ' << s.second << endl;
}int main()
{Read();Deal();Print();return 0;
}

2)《算法笔记》之代码:

思路:
输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,
都需要用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还
需要用到原来的名字,因此,此时的数字也能返过去访问原来的名字,那么定
义两个map容器便可。
剩下的便是图的遍历了。同时要注意,进行图的遍历时,是从0这个顶点开始进行遍历的。

第二个代码的字符串转换为数字,不是用进制来进行转换的,值得学习。当字符串的长度是七八九十个的时候,用进制转换为数字当作数组的下标,明显不现实,但借助 一个map容器,就可简单将字符串转换为数字的大小控制在字符串的数量以内。看代码实现,这第二个是算法笔记上的代码。

/*
思路:
输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,
都需要用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还
需要用到原来的名字,因此,此时的数字也能返过去访问原来的名字,那么定
义两个map容器便可。
剩下的便是图的遍历了。同时要注意,进行图的遍历时,是从0这个顶点开始进行遍历的。
*/#include 
#include 
#include using namespace std;map str_int;
map int_str;
map gang;const int N = 2010;
int w[N];
vector Adj[N];
bool hs[N];int n, k;
int idx;int change(string s)
{if(str_int.find(s) != str_int.end())return str_int[s];elsereturn idx++;
}void Read()
{cin >> n >> k;while (n -- ){string a, b;int t;cin >> a >> b >> t;int u = change(a), v = change(b);str_int[a] = u, str_int[b] = v;int_str[u] = a, int_str[v] = b;w[u] += t, w[v] += t;Adj[u].push_back(v), Adj[v].push_back(u);}
}void dfs(int index, int &h, int &sum, int &num)
{hs[index] = 1;if(w[index] > w[h])h = index;++num;sum += w[index];for(int i=0; i k && num > 2)gang.insert({int_str[h], num});}}cout << gang.size() << endl;for(auto s : gang)cout << s.first << ' ' << s.second << endl;
}int main()
{Read();Traver();return 0;}

3)将自己用进制实现的字符串转换为数字的函数,变为算法笔记上的数字逐渐累加的函数。

/*** 输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,都需要* 用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还需要用到原来* 的名字,因此,此时的数字也能返过去访问原来的名字,所以要将名字转换为的数字与* 名字相映射,用一个map容器记录就行。* 由于是一个不超过三个字符的名字,所以可以直接用26进制进行英文转换为数字。* * 同一个团伙的成员可以用并查集进行合并,头领记录该团伙的成员数量,其他成员指向* 该头领。最后再用图的遍历确定每个团队的首领。* * 最后注意计算团队总时长 > 阈值k时,题目要求的总时长是总时长/2,即两个人之间的通话* 只能算一次,通话时长总计需要除以2。
*/#include 
#include 
#include 
#include 
#include 
#include using namespace std;typedef pair PSI;//dfs返回的类型,total记录该团队的成员之间通话的总时间,maxv记录该团伙中打电话时长
//最长的一个人的时长,h记录该成员的字符串映射的数字编号;
struct Node
{int total, maxv, h;
};const int N = 3e5;
int fath[N]; //并查集数组
bool hs[N]; //该成员是否被访问过
map int_nm; //数字映名字
map str_int;
int num[N]; //每个成员的通话时长
vector Adj[N]; //邻接表
set res; //记录结果
int k;
int idx;//字符串转换为数字
int change(string s)
{if(str_int.find(s) != str_int.end())return str_int[s];elsereturn idx++;
}//并查集的查找祖宗结点编号
int find(int u)
{if(fath[u] < 0)return u;else return fath[u] = find(fath[u]);//此处需要trturn,否则会出现段错误,想想也是
}//并查集合并函数
void Union(string a, string b, int t)
{int u = change(a), v = change(b);int_nm[u] = a, int_nm[v] = b;str_int[a] = u, str_int[b] = v;Adj[u].push_back(v), Adj[v].push_back(u);num[u] += t, num[v] += t;int fau = find(u), fav = find(v);if(fau != fav){fath[fau] += fath[fav]; //注意此处是加,不是相减fath[fav] = fau;}
}void Read()
{int n;memset(fath, -1, sizeof(fath)); //注意初始化并查集数组cin >> n >> k;while(n--){string a, b;int t;cin >> a >> b >> t;Union(a, b, t);}
}Node dfs(int u)
{hs[u] = 1;int total = num[u], maxval = num[u];//cout << int_nm[u] << "->" << num[u] << endl;int h = u;for(int i=0; i" << int_nm[v] << endl;auto ans = dfs(v);//搜索每个团队的首领if(ans.maxv > maxval){h = ans.h;maxval = ans.maxv;}total += ans.total;}}return {total, maxval, h};
}void Deal()
{for(int i=0; i= -2) //s所在的团队成员数量不足2,不满足题意continue;Node ans = dfs(s);//注意此处的团队通话时长总计需要除以2,因为返回的是团队中每个//人的通话总时长,而题目要求的是总时长/2 > 阈值k;if(ans.total/2.0 <= k)   continue; res.insert({int_nm[ans.h], -fath[fau]});// cout << ans.total << "**" << ans.maxv << "**" << ans.h //   << "**" << int_nm[ans.h] << "**" << num[ans.h] << endl;}}}}
}void Print()
{cout << res.size() << endl;for(auto s : res)cout << s.first << ' ' << s.second << endl;
}int main()
{Read();Deal();Print();return 0;
}

相关内容

热门资讯

智慧吴江app安卓系统,安卓系... 你知道吗?最近吴江地区掀起了一股智慧风潮,一款名为“智慧吴江app”的应用在安卓系统上大受欢迎。这款...
苹果系统听歌app安卓,跨平台... 你有没有发现,无论是走在街头还是坐在家里,音乐总是能瞬间点燃我们的心情?而在这个音乐无处不在的时代,...
安卓系统卡顿根源,性能瓶颈与优... 手机用久了是不是感觉越来越卡?是不是每次打开应用都要等半天,甚至有时候直接卡死?别急,今天就来跟你聊...
电脑系统怎么装安卓系统,电脑系... 你有没有想过,把安卓系统装在你的电脑上,是不是就像给电脑穿上了时尚的新衣呢?想象你可以在电脑上直接使...
安卓系统华为手环app,健康管... 你有没有发现,现在的生活越来越离不开智能设备了?手机、平板、手表……这些小玩意儿不仅让我们的生活变得...
switch lite刷安卓系... 你有没有想过,你的Switch Lite除了玩那些可爱的任天堂游戏,还能干些什么呢?没错,今天我要给...
想买华为但是安卓系统,尽享安卓... 最近是不是也被华为的新款手机给迷住了?看着那流畅的线条和强大的性能,是不是心动了呢?但是,一想到安卓...
怎么拷安卓系统文件,安卓系统文... 你有没有想过,手机里的那些安卓系统文件,其实就像是一扇通往手机世界的秘密通道呢?想要深入了解你的安卓...
安卓系统移植按键失灵,安卓系统... 最近你的安卓手机是不是也遇到了按键失灵的尴尬情况呢?这可真是让人头疼啊!别急,今天就来给你详细解析一...
安卓系统更新管理在哪,全面解析... 你有没有发现,你的安卓手机最近是不是总在提醒你更新系统呢?别急,别急,今天就来手把手教你,安卓系统更...
安卓系统哪里出的,从诞生地到全... 你有没有想过,我们每天离不开的安卓系统,它究竟是从哪里冒出来的呢?是不是觉得这个问题有点儿像是在问星...
最好的电脑安卓系统,最佳电脑安... 亲爱的电脑迷们,你是否在寻找一款既能满足你工作需求,又能让你畅享娱乐的电脑操作系统呢?今天,我要给你...
安卓系统保密性,守护隐私的坚实... 你知道吗?在这个信息爆炸的时代,保护个人隐私变得比以往任何时候都重要。尤其是对于安卓系统用户来说,了...
苹果系统下载安卓版本,安卓版本... 你有没有想过,为什么苹果系统的手机那么受欢迎,却还有人想要下载安卓版本呢?这背后可是有着不少故事呢!...
安卓系统如何下载carplay... 你是不是也和我一样,对安卓系统上的CarPlay功能充满了好奇?想象在安卓手机上就能享受到苹果Car...
退回安卓系统的理由,揭秘安卓系... 你有没有想过,为什么有些人会选择退回到安卓系统呢?这可不是一件简单的事情,背后可是有着不少原因哦!让...
安卓机系统互通吗,共创智能生态 你有没有想过,你的安卓手机里的应用和电脑上的安卓应用是不是可以无缝对接呢?是不是有时候觉得手机上的某...
安卓源码 添加系统应用,系统应... 你有没有想过,手机里的那些系统应用是怎么来的?是不是觉得它们就像天外来物,神秘又神奇?其实,只要你愿...
安卓系统能否播放flv,全面解... 你有没有想过,你的安卓手机里那些珍贵的FLV视频文件,到底能不能顺利播放呢?这可是个让人挠头的问题,...
奔驰c系安卓系统,智能驾驶体验... 你有没有发现,最近开奔驰C系的小伙伴们都在悄悄地谈论一个新玩意儿——安卓系统!没错,就是那个我们手机...