【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 ...
随机推荐
- 如何更简单方便地执行SQL操作?
现在公司使用mybatis作为DAL层的框架. 使用起来比较简单,使用xml进行SQL的书写,java代码使用接口执行. 但在写一些简单SQL的时候会显得非常繁琐: xml和java分离(设计上为了解 ...
- 深入并发包 ConcurrentHashMap 源码解析
以前写过介绍HashMap的文章,文中提到过HashMap在put的时候,插入的元素超过了容量(由负载因子决定)的范围就会触发扩容操作,就是rehash,这个会重新将原数组的内容重新hash到新的扩容 ...
- windows部署MongoDB
打开MongoDb下载页面,分别下载Community Server和Compass,注意在安装Community Server时可以勾选同时安装Compass,但会比较慢,所以建议两个分开下载安装. ...
- 仓储模式Repository的选择与设计
首次接触仓储的概念来自Eric Evans 的经典著作<领域驱动设计-软件核心复杂性应对之道>,但书中没有具体实现.如何实现仓储模式,在我这几年的使用过程中也积累了一些具体的实施经验.根据 ...
- Python爬虫入门教程 32-100 B站博人传评论数据抓取 scrapy
1. B站博人传评论数据爬取简介 今天想了半天不知道抓啥,去B站看跳舞的小姐姐,忽然看到了评论,那就抓取一下B站的评论数据,视频动画那么多,也不知道抓取哪个,选了一个博人传跟火影相关的,抓取看看.网址 ...
- Python爬虫入门教程 24-100 微医挂号网医生数据抓取
1. 写在前面 今天要抓取的一个网站叫做微医网站,地址为 https://www.guahao.com ,我们将通过python3爬虫抓取这个网址,然后数据存储到CSV里面,为后面的一些分析类的教程做 ...
- jar文件和aar文件的区别
1. *.jar,JAR 文件就是 JavaArchive File,顾名思意,它的应用是与 Java 息息相关的,是 Java 的一种文档格式.只包含了class文件与清单文件 ,不包含资源文件 ...
- 翻译 异步I/O不会创建新的线程
异步I/O不会创建新的线程 本文翻译自 Stephen Cleary 的 [There is No Thread] 原文地址 https://blog.stephencleary.com/2013/1 ...
- 【java多线程】多线程的创建三种方式--笔记
申明:线程的概念以及进程的相关概念,可以参考网络上其他资料,这里只讨论多线程是怎么实现. 一.多线程的简单理解 明白什么是多线程,小生通俗一点的理解为:在一个程序里,我想同时让这个程序完成多个任务. ...
- spring框架应用系列二:component-scan自动扫描注册装配
component-scan自动扫描注册装配 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7717331.html ...