PAT (Advanced Level) 1034. Head of a Gang (30)
简单DFS。
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<vector>
- #include<map>
- #include<string>
- #include<queue>
- #include<stack>
- #include<algorithm>
- #include<iostream>
- using namespace std;
- const int maxn=+;
- map<string,int>m1;
- map<int,string>m2;
- int n,k,tot;
- struct Edge
- {
- int u,v,val;
- }e[maxn];
- struct Ans
- {
- string name;
- int cnt;
- }ans[maxn];
- int num;
- vector<int>g[maxn];
- int flag[maxn];
- int Block;
- int cnt[maxn];
- void dfs(int x)
- {
- num++; flag[x]=Block;
- for(int i=;i<g[x].size();i++)
- {
- if(flag[g[x][i]]!=) continue;
- dfs(g[x][i]);
- }
- }
- bool cmp(const Ans&a,const Ans&b){return a.name<b.name;}
- int main()
- {
- scanf("%d%d",&n,&k); tot=;
- memset(flag,,sizeof flag);
- memset(cnt,,sizeof cnt);
- for(int i=;i<=n;i++)
- {
- string a,b; cin>>a>>b>>e[i].val;
- if(m1[a]==)
- {
- m1[a]=++tot;
- m2[tot]=a;
- }
- if(m1[b]==)
- {
- m1[b]=++tot;
- m2[tot]=b;
- }
- e[i].u=m1[a];
- e[i].v=m1[b];
- cnt[e[i].v]+=e[i].val;
- cnt[e[i].u]+=e[i].val;
- g[e[i].u].push_back(e[i].v);
- g[e[i].v].push_back(e[i].u);
- }
- num=;
- int t=;
- for(int i=;i<=tot;i++)
- {
- if(flag[i]!=) continue;
- Block++; num=; dfs(i);
- if(num<=) continue;
- int sum=;
- for(int j=;j<=n;j++)
- if(flag[e[j].u]==Block&&flag[e[j].v]==Block)
- sum=sum+e[j].val;
- if(sum<=k) continue;
- int id,Max=-;
- for(int j=;j<=tot;j++)
- if(flag[j]==Block&&cnt[j]>Max)
- Max=cnt[j],id=j;
- ans[t].name=m2[id];
- ans[t].cnt=num;
- t++;
- }
- sort(ans,ans+t,cmp);
- printf("%d\n",t);
- for(int i=;i<t;i++)
- cout<<ans[i].name<<" "<<ans[i].cnt<<endl;
- return ;
- }
PAT (Advanced Level) 1034. Head of a Gang (30)的更多相关文章
- 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 ...
- PAT (Advanced Level) 1064. Complete Binary Search Tree (30)
因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...
- PAT (Advanced Level) 1053. Path of Equal Weight (30)
简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- 【PAT Advanced Level】1014. Waiting in Line (30)
简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...
- PAT (Advanced Level) 1038. Recover the Smallest Number (30)
注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...
- 【PAT甲级】1034 Head of a Gang (30 分)
题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT (Advanced Level) Practice 1001-1005
PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
随机推荐
- linux php扩展安装gettext
php解压后的文件路径为/usr/local/src/php-5.2.6 php 的安装路径为/usr/local/php [root@localhost# cd /usr/local/src/ph ...
- 在Oracle用SQL处理以 System.currentTimeMillis
有時為了系統的需求會紀錄到毫秒(Millisecond),我們會接將得到的值寫入db,但是如果要用SQL 做時間範圍的搜尋,有以下做法( systemdate欄位存放System.currentTim ...
- c语言 错误记录
1.预处理错误 #include <> //系统内部的 #include "" // 自定义的 遇到 not find------解决方案:gcc -I 跟查找 ...
- 1-3 编程基础 makefile工程管理
GNU make Linux程序员必须学会使用GNU make来构建和管理自己的软件工程.GNU的make能够使整个工程的编译.链接只需要一个命令就可以完成. makefile make在执行时,需要 ...
- npm的替代品
npm安装依赖包太慢,cnpm也快不到哪里去,偶然发现了yarn,特快特好用! 安装yarn:npm install -g yarn 查看版本号:yarn -v 安装依赖项:yarn install
- 导出csv文件(php实现)
<?php namespace App\Library\lib; class CsvLib { /** * [构造函数] * */ public function __construct() { ...
- 如何使用GoEasy实现PHP与Websocket实时通信
最近搞了搞websocket 做了个简答的聊天demo 1. 从GoEasy获取appkey appkey是验证用户的有效性的唯一标识. Ø 注册账号. GoEasy官网:https:// ...
- STM32——输入捕获实验原理及配置步骤
输入捕获实验原理及配置步骤 一.输入捕获概念 STM32的输入捕获,简单的说就是通过检测 TIMx_CHx (定时器X的通道X)上的 边沿信号,在边沿信号发生跳变(比如上升沿/下降沿)的时候,将当前定 ...
- zzuli 1905 小火山的跳子游戏
Description 小火山和火山火山在一块玩跳子游戏.规则如下: 1:跳子的起始位置为0,棋盘大小从1到N 2:每次跳子跳k步. 例如当前位置为i, 那么下一步为i + k 3:跳 ...
- hihocoder 1584 Bounce (数学 && 规律) ACM-ICPC北京赛区2017网络赛
题意: 给定一副n*m的格子图, 问从左上角的点开始往右下角滑,碰到墙壁就反弹, 碰到角落就停止, 问恰好经过一次的格子有多少个. 如图,恰好经过一次的格子有39个. 分析: 首先要引入两个概念, “ ...