昨天准备学完图相关的知识,但是学起来挺懵的,理解起来不难,但自己一回想,又什么都想不起来。

翻来覆去看图的遍历,还是觉得有点没到位。

所以做题来检测一下,果然学和自己做是两码事。

先看的书,又看的柳婼的代码。思路一样。

自己照着打了一遍,又自己实现了一遍,总体并不难,关键就是三十分的题,要花多点时间读懂题意。

只发现一个对于我来说的注意事项:

两个人打电话,可能不止打一次。

#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的更多相关文章

  1. 【PAT】B1075 链表元素分类(25 分)

    这道题算有点难,心目中理想的难度. 不能前怕狼后怕虎,一会担心超时,一会又担心内存过大,直接撸 将三部分分别保存到vector 有意思的在于输出 分别输出第一个的add和num 中间输出nextadd ...

  2. 【PAT】1091 Acute Stroke(30 分)

    1091 Acute Stroke(30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the s ...

  3. 【持续更新】【pat】pat刷题技巧记录

    修改code completion快捷键位CTRL+ENTER,帮助提示函数名称 修改命令行提示符的属性,开启快速编辑模式,方便调试 添加c++11语言标准支持 开启代码调试功能 对输入的字符串进行切 ...

  4. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  5. 【PAT】1025. PAT Ranking (25)

    题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1025 题目描述: Programming Ability Test (PAT) is orga ...

  6. 【PAT】1012. The Best Rank (25)

    题目链接: http://pat.zju.edu.cn/contests/pat-a-practise/1012 题目描述: To evaluate the performance of our fi ...

  7. 【PAT】1009. Product of Polynomials (25)

    题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...

  8. 【PAT】1041. Be Unique (20)

    题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1041 题目描述: Being unique is so important to people ...

  9. 【PAT】1035. Password (20)

    题目:http://pat.zju.edu.cn/contests/pat-a-practise/1035 分析:简单题.直接搜索,然后替换,不会超时,但是应该有更好的办法. 题目描述: To pre ...

随机推荐

  1. python之读取配置文件模块configparser(三)高级使用---非标准配置文件解析

    非标准配置文件也是经常使用的,如何使用configparser来解析? 这要从configparser本身解析结构来说,configparser包含section和option,非标准配置文件只有op ...

  2. nfs服务启动失败:Failed to start NFS status monitor for NFSv2/3 locking..

    今天碰到个问题,服务器重启后,nfs服务就启动不了了,关闭都关不了.查看系统日志报下面的错: Aug 10 17:08:53 prod-r3-slt-s-01 systemd: Started Pre ...

  3. Linux查看系统、核数、CPU、位数

    查看系统: cat /etc/os-release 结果为 centOS Linux 7 查看核数和CPU: lscpu 40 个核,处理器为 Intel(R) Xeon(R) CPU E7-8891 ...

  4. [Python Web]部署完网站需要做的基本后续工作

    简述 今天自己上线了一个简单的 Page,没有什么功能就是一个展示页. 但是,我发现部署完,上线后,还要弄不少东西.下面就是我记录.整理的一些上线网站基本都会用到的网站和配置. 加入统计代码 这个是必 ...

  5. Babel presets stage

    在一些新框架的代码中,常基于es6/7标准来书写代码.鉴于这些标准被没有被浏览器广泛支持,我们一般使用babel来将使用e6/7标准书写的代码降级编译(或者说转译)为浏览器可解析的es3/5代码. 以 ...

  6. 【SpringCloud】Zuul在何种情况下使用Hystrix

    首先,引入spring-cloud-starter-zuul之后会间接引入: hystrix依赖已经引入,那么何种情况下使用hystrix呢? 在Zuul的自动配置类ZuulServerAutoCon ...

  7. TFS2012服务端环境搭建

    微软提供的TFS(Team Foundation Server)是为开发者团队提供的一个集 代码仓库.任务管理.文档管理等众多强大功能的服务平台. 1.TFS依赖项 目前TFS已经出到了2018版本, ...

  8. Perl List::Util模块用法详解

    本文介绍Perl标准库List::Utils中的列表工具,有时候它们非常好用.比如Perl中测试列表中是否包含某个元素(某个元素是否存在于列表中)没有比较直接比较方便的功能,但使用List::Util ...

  9. web中的——作者也不知道这里面写的啥

    1.使用超链接<a href="/Default/Index">添加</a><a href="#">返回显示信息</a ...

  10. [MySQL] mysql 的读写锁与并发控制

    1.无论何时只要有多个查询在同一时刻修改数据,都会产生并发控制的问题 2.讨论mysql在两个层面,服务器层和存储引擎层,如何并发控制读写 3.举了个mbox邮箱文件的例子,说如果有多个进程同时对mb ...