简单DFS。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<vector>
  5. #include<map>
  6. #include<string>
  7. #include<queue>
  8. #include<stack>
  9. #include<algorithm>
  10. #include<iostream>
  11. using namespace std;
  12.  
  13. const int maxn=+;
  14. map<string,int>m1;
  15. map<int,string>m2;
  16. int n,k,tot;
  17.  
  18. struct Edge
  19. {
  20. int u,v,val;
  21. }e[maxn];
  22.  
  23. struct Ans
  24. {
  25. string name;
  26. int cnt;
  27. }ans[maxn];
  28. int num;
  29.  
  30. vector<int>g[maxn];
  31. int flag[maxn];
  32. int Block;
  33. int cnt[maxn];
  34.  
  35. void dfs(int x)
  36. {
  37. num++; flag[x]=Block;
  38. for(int i=;i<g[x].size();i++)
  39. {
  40. if(flag[g[x][i]]!=) continue;
  41. dfs(g[x][i]);
  42. }
  43. }
  44.  
  45. bool cmp(const Ans&a,const Ans&b){return a.name<b.name;}
  46.  
  47. int main()
  48. {
  49. scanf("%d%d",&n,&k); tot=;
  50. memset(flag,,sizeof flag);
  51. memset(cnt,,sizeof cnt);
  52. for(int i=;i<=n;i++)
  53. {
  54. string a,b; cin>>a>>b>>e[i].val;
  55. if(m1[a]==)
  56. {
  57. m1[a]=++tot;
  58. m2[tot]=a;
  59. }
  60. if(m1[b]==)
  61. {
  62. m1[b]=++tot;
  63. m2[tot]=b;
  64. }
  65. e[i].u=m1[a];
  66. e[i].v=m1[b];
  67. cnt[e[i].v]+=e[i].val;
  68. cnt[e[i].u]+=e[i].val;
  69. g[e[i].u].push_back(e[i].v);
  70. g[e[i].v].push_back(e[i].u);
  71. }
  72.  
  73. num=;
  74. int t=;
  75. for(int i=;i<=tot;i++)
  76. {
  77. if(flag[i]!=) continue;
  78. Block++; num=; dfs(i);
  79. if(num<=) continue;
  80. int sum=;
  81. for(int j=;j<=n;j++)
  82. if(flag[e[j].u]==Block&&flag[e[j].v]==Block)
  83. sum=sum+e[j].val;
  84. if(sum<=k) continue;
  85.  
  86. int id,Max=-;
  87. for(int j=;j<=tot;j++)
  88. if(flag[j]==Block&&cnt[j]>Max)
  89. Max=cnt[j],id=j;
  90.  
  91. ans[t].name=m2[id];
  92. ans[t].cnt=num;
  93. t++;
  94. }
  95.  
  96. sort(ans,ans+t,cmp);
  97.  
  98. printf("%d\n",t);
  99. for(int i=;i<t;i++)
  100. cout<<ans[i].name<<" "<<ans[i].cnt<<endl;
  101.  
  102. return ;
  103. }

PAT (Advanced Level) 1034. Head of a Gang (30)的更多相关文章

  1. PAT (Advanced Level) Practise - 1095. Cars on Campus (30)

    http://www.patest.cn/contests/pat-a-practise/1095 Zhejiang University has 6 campuses and a lot of ga ...

  2. PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

    因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...

  3. PAT (Advanced Level) 1053. Path of Equal Weight (30)

    简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  4. 【PAT Advanced Level】1014. Waiting in Line (30)

    简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...

  5. PAT (Advanced Level) 1038. Recover the Smallest Number (30)

    注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...

  6. 【PAT甲级】1034 Head of a Gang (30 分)

    题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...

  7. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  8. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  9. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

随机推荐

  1. linux php扩展安装gettext

    php解压后的文件路径为/usr/local/src/php-5.2.6 php 的安装路径为/usr/local/php [root@localhost# cd  /usr/local/src/ph ...

  2. 在Oracle用SQL处理以 System.currentTimeMillis

    有時為了系統的需求會紀錄到毫秒(Millisecond),我們會接將得到的值寫入db,但是如果要用SQL 做時間範圍的搜尋,有以下做法( systemdate欄位存放System.currentTim ...

  3. c语言 错误记录

    1.预处理错误 #include <>   //系统内部的 #include ""   // 自定义的 遇到 not find------解决方案:gcc -I 跟查找 ...

  4. 1-3 编程基础 makefile工程管理

    GNU make Linux程序员必须学会使用GNU make来构建和管理自己的软件工程.GNU的make能够使整个工程的编译.链接只需要一个命令就可以完成. makefile make在执行时,需要 ...

  5. npm的替代品

    npm安装依赖包太慢,cnpm也快不到哪里去,偶然发现了yarn,特快特好用! 安装yarn:npm install -g yarn 查看版本号:yarn -v 安装依赖项:yarn install

  6. 导出csv文件(php实现)

    <?php namespace App\Library\lib; class CsvLib { /** * [构造函数] * */ public function __construct() { ...

  7. 如何使用GoEasy实现PHP与Websocket实时通信

    最近搞了搞websocket 做了个简答的聊天demo 1.      从GoEasy获取appkey appkey是验证用户的有效性的唯一标识. Ø  注册账号. GoEasy官网:https:// ...

  8. STM32——输入捕获实验原理及配置步骤

    输入捕获实验原理及配置步骤 一.输入捕获概念 STM32的输入捕获,简单的说就是通过检测 TIMx_CHx (定时器X的通道X)上的 边沿信号,在边沿信号发生跳变(比如上升沿/下降沿)的时候,将当前定 ...

  9. zzuli 1905 小火山的跳子游戏

    Description   小火山和火山火山在一块玩跳子游戏.规则如下:   1:跳子的起始位置为0,棋盘大小从1到N   2:每次跳子跳k步. 例如当前位置为i, 那么下一步为i + k   3:跳 ...

  10. hihocoder 1584 Bounce (数学 && 规律) ACM-ICPC北京赛区2017网络赛

    题意: 给定一副n*m的格子图, 问从左上角的点开始往右下角滑,碰到墙壁就反弹, 碰到角落就停止, 问恰好经过一次的格子有多少个. 如图,恰好经过一次的格子有39个. 分析: 首先要引入两个概念, “ ...