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

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

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

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

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

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

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

#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. scala中spark运行内存不足

    用 bash spark-submit 在spark上跑代码的时候出现错误: ERROR executor.Executor: Exception in task 9.0 in stage 416.0 ...

  2. 『左偏树 Leftist Tree』

    新增一道例题 左偏树 Leftist Tree 这是一个由堆(优先队列)推广而来的神奇数据结构,我们先来了解一下它. 简单的来说,左偏树可以实现一般堆的所有功能,如查询最值,删除堆顶元素,加入新元素等 ...

  3. Dubbo(二) —— dubbo配置

      一.配置原则 JVM 启动 -D 参数优先,这样可以使用户在部署和启动时进行参数重写,比如在启动时需改变协议的端口. XML 次之,如果在 XML 中有配置,则 dubbo.properties ...

  4. SpringBoot入门教程(十二)DevTools热部署

    devtools模块,是为开发者服务的一个模块.主要的功能就是代码修改后一般在5秒之内就会自动重新加载至服务器,相当于restart成功.与JRebel不同的是,JRebel是一款商业插件,devto ...

  5. Mongodb~Linux环境下的部署

    < mongodb服务脚本的制作> Mongodb这个文档型非关系型数据库,可以说它是最像关系型的了,之前大叔主要讲如何使用mongodb,而没有说过如何去部署和安装它,而今天大叔有必要讲 ...

  6. Numpy 基础学习

    numpy.array() 功能:创建一个数据 vector = numpy.array([1,2,3,4]) matrix = numpy.array([1,2,3,4],[11,12,13,14] ...

  7. 学习编写Dockerfile

    前言 我们学习docker的话,其主要目的还是要用来改变我们部署应用程序的传统习惯,达到解放生产力,解放人力的目的.这篇则自己来熟悉一下dockerfile常用命令.并且尝试改变下生产环境手动部署应用 ...

  8. VS Code调试.Net Core版Hello World

    安装C#插件 下载安装插件,地址:https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp 安装插件之后重新启动VS C ...

  9. JavaScript 条件语句

    if语句     有些代码块只能在一定条件下运行,通过if.if else.else代码块,可以让你的代码按条件执行. // 控制流 var foo = true; var bar = false; ...

  10. Mysql 连接数,最大并发数设置

    项目中可能会遇到MySQL: ERROR 1040: Too many connections”的异常情况,造成这种情况的一种原因是访问量过高,MySQL服务器抗不住,这个时候就要考虑增加从服务器分散 ...