• 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 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

     dfs每个连通分量

    1. #include <iostream>
    2. #include <vector>
    3. #include <string>
    4. #include <algorithm>
    5. #include <cstring>
    6. using namespace std;
    7. struct edge {
    8. int next, val;
    9. bool flag;//记录访问
    10. };
    11. struct Peo {
    12. string name;//头目
    13. int num;//人数
    14. } a[2010];
    15. int n, k, cnt, ans;
    16. vector<edge>g[20000];
    17. bool vis[20000];
    18. int num[20000];//记录人数
    19. int time1[20000];//记录每人通话时间
    20. int sum;//记录每个连通分量的总时间
    21. int p;//记录每个连通分量的总人数
    22. int maxx;//记录每组最大通话时间
    23. int mName;//记录每组头目
    24. int stoi1(string s) {
    25. return (s[0] - 'A') * 26 * 26 + (s[1] - 'A') * 26 + s[2] - 'A';
    26. }
    27. string itos(int x) {
    28. string s = "";
    29. s += (char)(x % 26 + 'A');
    30. x /= 26;
    31. s += (char)(x % 26 + 'A');
    32. x /= 26;
    33. s += (char)(x + 'A');
    34. reverse(s.begin(), s.end());
    35. return s;
    36. }
    37. void dfs(int x) {
    38. for (int i = 0; i < g[x].size(); i++) {
    39. if (g[x][i].flag) {
    40. continue;
    41. }
    42. sum += g[x][i].val;
    43. g[x][i].flag = 1;
    44. if (vis[g[x][i].next]) {
    45. continue;
    46. }
    47. vis[g[x][i].next] = 1;
    48. p++;
    49. if (time1[g[x][i].next] > maxx || (time1[g[x][i].next] == maxx && g[x][i].next < mName)) {
    50. maxx = time1[g[x][i].next];
    51. mName = g[x][i].next;
    52. }
    53. dfs(g[x][i].next);
    54. }
    55. }
    56. bool cmp(Peo p1, Peo p2) {
    57. return p1.name < p2.name;
    58. }
    59. int main() {
    60. cin >> n >> k;
    61. string s1, s2;
    62. int u, v, t;
    63. for (int i = 0; i < n; i++) {
    64. cin >> s1 >> s2 >> t;
    65. u = stoi1(s1);
    66. v = stoi1(s2);
    67. if (!vis[u]) {
    68. vis[u] = 1;
    69. cnt++;
    70. num[cnt] = u;
    71. }
    72. if (!vis[v]) {
    73. vis[v] = 1;
    74. cnt++;
    75. num[cnt] = v;
    76. }
    77. time1[u] += t;
    78. time1[v] += t;
    79. edge e;
    80. e.flag = 0;
    81. e.next = v;
    82. e.val = t;
    83. g[u].push_back(e);
    84. e.next = u;
    85. e.val = 0; //防止重复计算边权
    86. g[v].push_back(e);
    87. }
    88. memset(vis, 0, sizeof(vis));
    89. for (int i = 1; i <= cnt; i++) {
    90. if (vis[num[i]]) {
    91. continue;
    92. }
    93. vis[num[i]] = 1;
    94. p = 1;
    95. sum = 0;
    96. mName = num[i];
    97. maxx = time1[num[i]];
    98. dfs(num[i]);
    99. if (p > 2 && sum > k) {
    100. ans++;
    101. a[ans].name = itos(mName);
    102. a[ans].num = p;
    103. }
    104. }
    105. sort(a + 1, a + 1 + ans, cmp);
    106. cout << ans << endl;
    107. for (int i = 1; i <= ans; i++) {
    108. cout << a[i].name << ' ' << a[i].num << endl;
    109. }
    110. return 0;
    111. }

     

  • 相关阅读:
    【python入门篇】列表简介及操作(2)
    百战RHCE(第五十一战:运维工程师必会技-Ansible学习6-编写和执行Playbook)
    从零开始搭建一个GIS开发小框架(五)——GMap.Net组件WPF版本使用体验
    APP上架需要的准备和流程
    RocketMQ - Java 20220917
    图技术赋能民生银行创新转型
    TDengine Restful Authorization 自定义Token
    【ICPC2022济南站】【树形dp】【删物品背包dp】C.DFS Order 2
    2008-2020年数据上市公司高管团队异质性数据包含Stata代码
    MapBox入门教程一:token申请与环境搭建
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/125545187