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:

  1. 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:

  1. 0

Keys:

Attention:

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

Code:

  1. /*
  2. Data: 2019-04-14 19:46:23
  3. Problem: PAT_A1034#Head of a Gang
  4. AC: 61:22
  5.  
  6. 题目大意:
  7. A和B有通话总时长表示他们的联系程度,
  8. 如果一撮人通话总时长超过某一阈值,则认定为“犯罪团伙”,其中电话打的最多的就是头目;
  9. 输入:
  10. 第一行给出通话数目N和阈值K(<=1e3)
  11. 接下来N行给出 v1 v2 w,其中V用三个大写字母表示,w<=1e3
  12. 输出:
  13. 第一行输出团伙数目
  14. 接下来按照字典序依次输出各个团伙的头目及其人数
  15.  
  16. 基本思路:
  17. 预处理各顶点边权之和,
  18. 遍历各个连通分量,统计结点数目,边权之和,犯罪头目
  19. map存储犯罪头目及其人数,达到字典序排序的目的
  20. */
  21. #include<cstdio>
  22. #include<iostream>
  23. #include<algorithm>
  24. #include<string>
  25. #include<map>
  26. using namespace std;
  27. const int M=2e3+;
  28. int grap[M][M],vis[M],cost[M],pt=;
  29. int K,k,sum,Max;
  30. map<string,int> mp,gang;
  31. string toS[M],head;
  32.  
  33. int ToN(string s)
  34. {
  35. if(mp[s]==)
  36. {
  37. mp[s]=pt;
  38. toS[pt]=s;
  39. return pt++;
  40. }
  41. else
  42. return mp[s];
  43. }
  44.  
  45. void DFS(int u)
  46. {
  47. vis[u]=;
  48. sum++;
  49. if(cost[u] > Max)
  50. {
  51. Max = cost[u];
  52. head = toS[u];
  53. }
  54. for(int v=; v<pt; v++)
  55. {
  56. k += grap[u][v];
  57. if(grap[u][v]!= && vis[v]==)
  58. DFS(v);
  59. /*若按规矩办事
  60. if(grap[u][v]!=0) //判断是否存在边
  61. {
  62. k += grap[u][v]; //各边数了两次
  63. if(vis[v]==0)
  64. k += grap[u][v]; //若有回路,少数一条边
  65. }
  66. */
  67. }
  68. }
  69.  
  70. int Travel()
  71. {
  72. fill(vis,vis+M,);
  73. int cnt=;
  74. for(int v=; v<pt; v++)
  75. {
  76. k=;sum=;Max=;
  77. if(vis[v]==)
  78. {
  79. DFS(v);
  80. if(k>K* && sum>)
  81. {
  82. cnt++;
  83. gang[head]=sum;
  84. }
  85. }
  86. }
  87. return cnt;
  88. }
  89.  
  90. int main()
  91. {
  92. #ifdef ONLINE_JUDGE
  93. #else
  94. freopen("Test.txt", "r", stdin);
  95. #endif // ONLINE_JUDGE
  96.  
  97. int n;
  98. scanf("%d%d", &n,&K);
  99. fill(grap[],grap[]+M*M,);
  100. fill(cost,cost+M,);
  101. for(int i=; i<n; i++)
  102. {
  103. string s1,s2;
  104. int v1,v2,w;
  105. cin >> s1 >> s2 >> w;
  106. v1 = ToN(s1);
  107. v2 = ToN(s2);
  108. grap[v1][v2]+=w;
  109. grap[v2][v1]+=w;
  110. cost[v1] += w;
  111. cost[v2] += w;
  112. }
  113. int cnt=Travel();
  114. printf("%d\n", cnt);
  115. for(auto it=gang.begin(); it!=gang.end(); it++)
  116. cout << it->first << " " << it->second << endl;
  117.  
  118. return ;
  119. }

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. spring boot.定时任务问题记录(TaskScheduler/ScheduledExecutorService异常)

    一.背景 spring boot的定时任务非常简单,只需要在启动类中加上@EnableScheduling注解,然后在对应的方法上配置@Scheduled就可以了,系统会自动处理并按照Schedule ...

  2. [转]supervisor 安装、配置、常用命令

    原文: http://www.cnblogs.com/xueweihan/p/6195824.html ------------------------------------------------ ...

  3. maven打包需要设置main-class的插件写法

    maven打包需要设置main-class的插件写法 <build> <plugins> <plugin> <groupId>org.apache.ma ...

  4. Oracle EBS LOV速度优化

    一.现象 本文地址:http://blog.csdn.net/sunansheng/article/details/50952758 当我们的EBS LOV的SQL写得比較复杂.或者数据量比較多时,L ...

  5. luogu1631 序列合并

    题目大意 有两个序列A,B,在A和B中各取一个数相加能得到$n^2$个和.求出这些和前n小的数字. 题解 首先这道题不可以用自己想的什么A序列B序列各两个指针的自己发明的模拟算法,用这样的算法只能是绝 ...

  6. 如何注释ascx中的代码

    https://forums.asp.net/t/1783252.aspx?Commented+out+ascx+code+not+treated+as+commented+out+ <%--  ...

  7. 【BZOJ 3620】 似乎在梦中见过的样子

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3620 [算法] KMP [代码] #include<bits/stdc++.h ...

  8. 【BZOJ 1398】 Necklace

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1398 [算法] 最小表示法 [代码] #include<bits/stdc++ ...

  9. iOS获取相册/相机图片-------自定义获取图片小控件

    一.功能简介 1.封装了一个按钮,点击按钮,会提示从何处获取图片:如果设备支持相机,可以从相机获取,同时还可以从手机相册获取图片. 2.选择图片后,有一个block回调,根据需求,将获得的图片拿来使用 ...

  10. aspectc中this可以获取的东西

    this->kind  操作类型 this->targetName 被调用函数名称 this->funcName 调用函数名称 this->argsCount 参数个数 thi ...