• PAT A1034 Head of a Gang


    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:

    1. 8 59
    2. AAA BBB 10
    3. BBB AAA 20
    4. AAA CCC 40
    5. DDD EEE 5
    6. EEE DDD 70
    7. FFF GGG 30
    8. GGG HHH 20
    9. HHH FFF 10

    Sample Output 1:

    1. 2
    2. AAA 3
    3. GGG 3

    Sample Input 2:

    1. 8 70
    2. AAA BBB 10
    3. BBB AAA 20
    4. AAA CCC 40
    5. DDD EEE 5
    6. EEE DDD 70
    7. FFF GGG 30
    8. GGG HHH 20
    9. HHH FFF 10

    Sample Output 2:

    0

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

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

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

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

    1. /**
    2. * 输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,都需要
    3. * 用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还需要用到原来
    4. * 的名字,因此,此时的数字也能返过去访问原来的名字,所以要将名字转换为的数字与
    5. * 名字相映射,用一个map容器记录就行。
    6. * 由于是一个不超过三个字符的名字,所以可以直接用26进制进行英文转换为数字。
    7. *
    8. * 同一个团伙的成员可以用并查集进行合并,头领记录该团伙的成员数量,其他成员指向
    9. * 该头领。最后再用图的遍历确定每个团队的首领。
    10. *
    11. * 最后注意计算团队总时长 > 阈值k时,题目要求的总时长是总时长/2,即两个人之间的通话
    12. * 只能算一次,通话时长总计需要除以2。
    13. */
    14. #include <iostream>
    15. #include <cstring>
    16. #include <algorithm>
    17. #include <map>
    18. #include <set>
    19. #include <vector>
    20. using namespace std;
    21. typedef pair<string,int> PSI;
    22. //dfs返回的类型,total记录该团队的成员之间通话的总时间,maxv记录该团伙中打电话时长
    23. //最长的一个人的时长,h记录该成员的字符串映射的数字编号;
    24. struct Node
    25. {
    26. int total, maxv, h;
    27. };
    28. const int N = 3e5;
    29. int fath[N]; //并查集数组
    30. bool hs[N]; //该成员是否被访问过
    31. map<int,string> int_nm; //数字映名字
    32. int num[N]; //每个成员的通话时长
    33. vector<int> Adj[N]; //邻接表
    34. set<PSI> res; //记录结果
    35. int k;
    36. //字符串转换为数字
    37. int Stoi(string s)
    38. {
    39. int n = 1;
    40. for(int i=0; i<s.size(); ++i)
    41. n = n*27 + s[i] - 'A' + 1;
    42. return n;
    43. }
    44. //并查集的查找祖宗结点编号
    45. int find(int u)
    46. {
    47. if(fath[u] < 0)
    48. return u;
    49. else
    50. return fath[u] = find(fath[u]);
    51. //此处需要trturn,否则会出现段错误,想想也是
    52. }
    53. //并查集合并函数
    54. void Union(string a, string b, int t)
    55. {
    56. int u = Stoi(a), v = Stoi(b);
    57. int_nm[u] = a, int_nm[v] = b;
    58. Adj[u].push_back(v), Adj[v].push_back(u);
    59. num[u] += t, num[v] += t;
    60. int fau = find(u), fav = find(v);
    61. if(fau != fav)
    62. {
    63. fath[fau] += fath[fav]; //注意此处是加,不是相减
    64. fath[fav] = fau;
    65. }
    66. }
    67. void Read()
    68. {
    69. int n;
    70. memset(fath, -1, sizeof(fath)); //注意初始化并查集数组
    71. cin >> n >> k;
    72. while(n--)
    73. {
    74. string a, b;
    75. int t;
    76. cin >> a >> b >> t;
    77. Union(a, b, t);
    78. }
    79. }
    80. Node dfs(int u)
    81. {
    82. hs[u] = 1;
    83. int total = num[u], maxval = num[u];
    84. //cout << int_nm[u] << "->" << num[u] << endl;
    85. int h = u;
    86. for(int i=0; i<Adj[u].size(); ++i)
    87. {
    88. int v = Adj[u][i];
    89. if(hs[v] == 0)
    90. {
    91. //cout << int_nm[u] << "->" << int_nm[v] << endl;
    92. auto ans = dfs(v);
    93. //搜索每个团队的首领
    94. if(ans.maxv > maxval)
    95. {
    96. h = ans.h;
    97. maxval = ans.maxv;
    98. }
    99. total += ans.total;
    100. }
    101. }
    102. return {total, maxval, h};
    103. }
    104. void Deal()
    105. {
    106. for(int i=0; i<N; ++i)
    107. {
    108. if(Adj[i].size()) //如果i有团队,
    109. {
    110. for(auto s : Adj[i])
    111. {
    112. if(hs[s] == 0) //如果此时s还未被访问过
    113. {
    114. int fau = find(s);
    115. if(fath[fau] >= -2) //s所在的团队成员数量不足2,不满足题意
    116. continue;
    117. Node ans = dfs(s);
    118. //注意此处的团队通话时长总计需要除以2,因为返回的是团队中每个
    119. //人的通话总时长,而题目要求的是总时长/2 > 阈值k;
    120. if(ans.total/2.0 <= k) continue;
    121. res.insert({int_nm[ans.h], -fath[fau]});
    122. // cout << ans.total << "**" << ans.maxv << "**" << ans.h
    123. // << "**" << int_nm[ans.h] << "**" << num[ans.h] << endl;
    124. }
    125. }
    126. }
    127. }
    128. }
    129. void Print()
    130. {
    131. cout << res.size() << endl;
    132. for(auto s : res)
    133. cout << s.first << ' ' << s.second << endl;
    134. }
    135. int main()
    136. {
    137. Read();
    138. Deal();
    139. Print();
    140. return 0;
    141. }

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

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

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

    1. /*
    2. 思路:
    3. 输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,
    4. 都需要用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还
    5. 需要用到原来的名字,因此,此时的数字也能返过去访问原来的名字,那么定
    6. 义两个map容器便可。
    7. 剩下的便是图的遍历了。同时要注意,进行图的遍历时,是从0这个顶点开始进行遍历的。
    8. */
    9. #include <iostream>
    10. #include <map>
    11. #include <vector>
    12. using namespace std;
    13. map<string, int> str_int;
    14. map<int, string> int_str;
    15. map<string, int> gang;
    16. const int N = 2010;
    17. int w[N];
    18. vector<int> Adj[N];
    19. bool hs[N];
    20. int n, k;
    21. int idx;
    22. int change(string s)
    23. {
    24. if(str_int.find(s) != str_int.end())
    25. return str_int[s];
    26. else
    27. return idx++;
    28. }
    29. void Read()
    30. {
    31. cin >> n >> k;
    32. while (n -- )
    33. {
    34. string a, b;
    35. int t;
    36. cin >> a >> b >> t;
    37. int u = change(a), v = change(b);
    38. str_int[a] = u, str_int[b] = v;
    39. int_str[u] = a, int_str[v] = b;
    40. w[u] += t, w[v] += t;
    41. Adj[u].push_back(v), Adj[v].push_back(u);
    42. }
    43. }
    44. void dfs(int index, int &h, int &sum, int &num)
    45. {
    46. hs[index] = 1;
    47. if(w[index] > w[h])
    48. h = index;
    49. ++num;
    50. sum += w[index];
    51. for(int i=0; i<Adj[index].size(); ++i)
    52. {
    53. int v = Adj[index][i];
    54. if(hs[v] == 0)
    55. dfs(v, h, sum, num);
    56. }
    57. }
    58. void Traver()
    59. {
    60. for(int i=0; i<idx; ++i)
    61. {
    62. if(hs[i] == 0)
    63. {
    64. int h = i, sum = 0, num = 0;
    65. dfs(i, h, sum, num);
    66. if(sum/2.0 > k && num > 2)
    67. gang.insert({int_str[h], num});
    68. }
    69. }
    70. cout << gang.size() << endl;
    71. for(auto s : gang)
    72. cout << s.first << ' ' << s.second << endl;
    73. }
    74. int main()
    75. {
    76. Read();
    77. Traver();
    78. return 0;
    79. }

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

    1. /**
    2. * 输入的成员名字是英文,然而,用图遍历时,不管是邻接矩阵,还是邻接表,都需要
    3. * 用数组访问两个顶点是否连通,那么就需要将英文变为数字,同时,还需要用到原来
    4. * 的名字,因此,此时的数字也能返过去访问原来的名字,所以要将名字转换为的数字与
    5. * 名字相映射,用一个map容器记录就行。
    6. * 由于是一个不超过三个字符的名字,所以可以直接用26进制进行英文转换为数字。
    7. *
    8. * 同一个团伙的成员可以用并查集进行合并,头领记录该团伙的成员数量,其他成员指向
    9. * 该头领。最后再用图的遍历确定每个团队的首领。
    10. *
    11. * 最后注意计算团队总时长 > 阈值k时,题目要求的总时长是总时长/2,即两个人之间的通话
    12. * 只能算一次,通话时长总计需要除以2。
    13. */
    14. #include <iostream>
    15. #include <cstring>
    16. #include <algorithm>
    17. #include <map>
    18. #include <set>
    19. #include <vector>
    20. using namespace std;
    21. typedef pair<string,int> PSI;
    22. //dfs返回的类型,total记录该团队的成员之间通话的总时间,maxv记录该团伙中打电话时长
    23. //最长的一个人的时长,h记录该成员的字符串映射的数字编号;
    24. struct Node
    25. {
    26. int total, maxv, h;
    27. };
    28. const int N = 3e5;
    29. int fath[N]; //并查集数组
    30. bool hs[N]; //该成员是否被访问过
    31. map<int,string> int_nm; //数字映名字
    32. map<string, int> str_int;
    33. int num[N]; //每个成员的通话时长
    34. vector<int> Adj[N]; //邻接表
    35. set<PSI> res; //记录结果
    36. int k;
    37. int idx;
    38. //字符串转换为数字
    39. int change(string s)
    40. {
    41. if(str_int.find(s) != str_int.end())
    42. return str_int[s];
    43. else
    44. return idx++;
    45. }
    46. //并查集的查找祖宗结点编号
    47. int find(int u)
    48. {
    49. if(fath[u] < 0)
    50. return u;
    51. else
    52. return fath[u] = find(fath[u]);
    53. //此处需要trturn,否则会出现段错误,想想也是
    54. }
    55. //并查集合并函数
    56. void Union(string a, string b, int t)
    57. {
    58. int u = change(a), v = change(b);
    59. int_nm[u] = a, int_nm[v] = b;
    60. str_int[a] = u, str_int[b] = v;
    61. Adj[u].push_back(v), Adj[v].push_back(u);
    62. num[u] += t, num[v] += t;
    63. int fau = find(u), fav = find(v);
    64. if(fau != fav)
    65. {
    66. fath[fau] += fath[fav]; //注意此处是加,不是相减
    67. fath[fav] = fau;
    68. }
    69. }
    70. void Read()
    71. {
    72. int n;
    73. memset(fath, -1, sizeof(fath)); //注意初始化并查集数组
    74. cin >> n >> k;
    75. while(n--)
    76. {
    77. string a, b;
    78. int t;
    79. cin >> a >> b >> t;
    80. Union(a, b, t);
    81. }
    82. }
    83. Node dfs(int u)
    84. {
    85. hs[u] = 1;
    86. int total = num[u], maxval = num[u];
    87. //cout << int_nm[u] << "->" << num[u] << endl;
    88. int h = u;
    89. for(int i=0; i<Adj[u].size(); ++i)
    90. {
    91. int v = Adj[u][i];
    92. if(hs[v] == 0)
    93. {
    94. //cout << int_nm[u] << "->" << int_nm[v] << endl;
    95. auto ans = dfs(v);
    96. //搜索每个团队的首领
    97. if(ans.maxv > maxval)
    98. {
    99. h = ans.h;
    100. maxval = ans.maxv;
    101. }
    102. total += ans.total;
    103. }
    104. }
    105. return {total, maxval, h};
    106. }
    107. void Deal()
    108. {
    109. for(int i=0; i<idx; ++i)
    110. {
    111. if(Adj[i].size()) //如果i有团队,
    112. {
    113. for(auto s : Adj[i])
    114. {
    115. if(hs[s] == 0) //如果此时s还未被访问过
    116. {
    117. int fau = find(s);
    118. if(fath[fau] >= -2) //s所在的团队成员数量不足2,不满足题意
    119. continue;
    120. Node ans = dfs(s);
    121. //注意此处的团队通话时长总计需要除以2,因为返回的是团队中每个
    122. //人的通话总时长,而题目要求的是总时长/2 > 阈值k;
    123. if(ans.total/2.0 <= k) continue;
    124. res.insert({int_nm[ans.h], -fath[fau]});
    125. // cout << ans.total << "**" << ans.maxv << "**" << ans.h
    126. // << "**" << int_nm[ans.h] << "**" << num[ans.h] << endl;
    127. }
    128. }
    129. }
    130. }
    131. }
    132. void Print()
    133. {
    134. cout << res.size() << endl;
    135. for(auto s : res)
    136. cout << s.first << ' ' << s.second << endl;
    137. }
    138. int main()
    139. {
    140. Read();
    141. Deal();
    142. Print();
    143. return 0;
    144. }

  • 相关阅读:
    node.js --- MVC
    C认证笔记 - 计算机通识 - 信息单位
    【六祎 - Mac】Mac安装Nginx;Mac编译安装Nginx;
    C++ 重载运算符和重载函数
    ubuntu创建账号和samba共享目录
    电子科技大学编译原理复习笔记(四):程序语言的设计
    Linux(CentOS7)搭建LAMP服务环境
    C语言基础知识
    【USRP】报错:Error: RuntimeError: [ad9361_device_t] BBPLL not locked
    【Leetcode】189. 轮转数组
  • 原文地址:https://blog.csdn.net/qq_51825761/article/details/127947147