【PAT】A1034Head of a Gang
昨天准备学完图相关的知识,但是学起来挺懵的,理解起来不难,但自己一回想,又什么都想不起来。
翻来覆去看图的遍历,还是觉得有点没到位。
所以做题来检测一下,果然学和自己做是两码事。
先看的书,又看的柳婼的代码。思路一样。
自己照着打了一遍,又自己实现了一遍,总体并不难,关键就是三十分的题,要花多点时间读懂题意。
只发现一个对于我来说的注意事项:
两个人打电话,可能不止打一次。
#include<string>
#include<iostream>
#include<map>
using namespace std;
map<string,int> sti;
map<int,string> its;
int id=1,K;
int G[2010][2010],weight[2010];
bool vis[2010];
int getid(string str){
if(sti[str]==0){
sti[str]=id;
its[id]=str;
return id++;
}else
return sti[str];
}
void DFS(int u,int &head,int &numMember,int &totalweight){
numMember++;
vis[u]=true;
if(weight[u]>weight[head])
head=u;
for(int j=1;j<id;j++){
if(G[j][u]>0){
totalweight+=G[j][u];
G[j][u]=G[u][j]=0;
if(vis[j]==false)
DFS(j,head,numMember,totalweight);
}
}
}
map<string,int> ans;
void DFSTrave(){
for(int i=1;i<id;i++){
if(vis[i]==false){
int head=i,numMember=0,totalweight=0;
DFS(i,head,numMember,totalweight);
if(numMember>2&&totalweight>K)
ans[its[head]]=numMember;
}
}
}
int main(){
int N;
cin >> N >> K;
for(int i=0;i<N;i++){
string str1,str2;
int temp;
cin >>str1 >> str2 >>temp;
int id1=getid(str1);
int id2=getid(str2);
weight[id1]+=temp;
weight[id2]+=temp;
G[id1][id2]+=temp;//两个人不一定只通一次电话
G[id2][id1]+=temp;
}
DFSTrave();
cout << ans.size() << endl;
for(auto it=ans.begin();it!=ans.end();it++)
cout << it->first << " " << it->second <<endl;
return 0;
}
【PAT】A1034Head of a Gang的更多相关文章
- 【PAT】B1075 链表元素分类(25 分)
这道题算有点难,心目中理想的难度. 不能前怕狼后怕虎,一会担心超时,一会又担心内存过大,直接撸 将三部分分别保存到vector 有意思的在于输出 分别输出第一个的add和num 中间输出nextadd ...
- 【PAT】1091 Acute Stroke(30 分)
1091 Acute Stroke(30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the s ...
- 【持续更新】【pat】pat刷题技巧记录
修改code completion快捷键位CTRL+ENTER,帮助提示函数名称 修改命令行提示符的属性,开启快速编辑模式,方便调试 添加c++11语言标准支持 开启代码调试功能 对输入的字符串进行切 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- 【PAT】1025. PAT Ranking (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1025 题目描述: Programming Ability Test (PAT) is orga ...
- 【PAT】1012. The Best Rank (25)
题目链接: http://pat.zju.edu.cn/contests/pat-a-practise/1012 题目描述: To evaluate the performance of our fi ...
- 【PAT】1009. Product of Polynomials (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...
- 【PAT】1041. Be Unique (20)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1041 题目描述: Being unique is so important to people ...
- 【PAT】1035. Password (20)
题目:http://pat.zju.edu.cn/contests/pat-a-practise/1035 分析:简单题.直接搜索,然后替换,不会超时,但是应该有更好的办法. 题目描述: To pre ...
随机推荐
- Python内置函数(43)——min
英文文档: min(iterable, *[, key, default]) min(arg1, arg2, *args[, key]) Return the smallest item in an ...
- Python内置函数(19)——eval
英文文档: eval(expression, globals=None, locals=None) The arguments are a string and optional globals an ...
- Java String:重要到别人只能当老二的字符串类
字符串,是Java中最重要的类.这句肯定的推断不是Java之父詹姆斯·高斯林说的,而是沉默王二说的,因此你不必怀疑它的准确性. 关于字符串,有很多的面试题,但我总觉得理论知识绕来绕去没多大意思.你比如 ...
- 【java 多线程】多线程并发同步问题及解决方法
一.线程并发同步概念 线程同步其核心就在于一个“同”.所谓“同”就是协同.协助.配合,“同步”就是协同步调昨,也就是按照预定的先后顺序进行运行,即“你先,我等, 你做完,我再做”. 线程同步,就是当线 ...
- OCR识别
最近作者项目中用到了身份证识别跟营业执照的OCR识别,就研究了一下百度云跟腾讯云的OCR产品接口. 1.腾讯云OCR 收费:身份证OCR和营业执照OCR接口,每个接口每个月各有1000次的免费调用 接 ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- jumpserver篇--安装
参考:https://github.com/jumpserver/jumpserver/wiki/%E5%AE%89%E8%A3%85%E5%9B%BE%E8%A7%A3 服务器环境: ip:192. ...
- [十五]javaIO之SequenceInputStream
功能简介 SequenceInputStream 合并流 顾名思义,就是可以吧两个流合并起来 他并没有很复杂,单纯的很,仅仅实现了InputStream 他拥有两个构造方法把两个InputS ...
- Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)
通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...
- js中如何在不影响既有事件监听的前提下新增监听器
一. 需求澄清 比如某个按钮已经绑定了2-3个对Window对象的load事件的监听,现在需要添加一个新的对click事件的监听器,但在一定条件下才会同时触发原有的2-3个load监听器,否则只触发新 ...