PATA1034题解
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624
参考:算法笔记(胡凡)10.3.1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int N=2*1e3+10;
int n,k,num=0,total=0;
int weight[N]={0},ishead[N],gar[N][N]={0};
bool vis[N]={0};
map<string,int> ma;
map<int,string> mb;
map<int,int> hea;
int stringtoint(string s)
{
if (ma.find(s)==ma.end())
{
ma[s]=num;
mb[num++]=s;
return num-1;
}
else
{
return ma[s];
}
}
void findmodal(int now,int& head,int& people,int& totwei)
{
vis[now]=1;
people++;
if (weight[now]>weight[head])
{
head=now;
}
for (int i=0;i<num;i++)
{
if (gar[now][i]>0)
{
totwei+=gar[now][i];
gar[i][now]=gar[now][i]=0;
if (!vis[i])
{
findmodal(i,head,people,totwei);
}
}
}
}
int main()
{
// freopen("in.txt","r",stdin);
cin>>n>>k;
string sa,sb;
int wei;
for (int i=0;i<n;i++)
{
cin>>sa>>sb>>wei;
int ida=stringtoint(sa);
int idb=stringtoint(sb);
weight[ida]+=wei;
weight[idb]+=wei;
gar[ida][idb]+=wei;//这里要用+=,不能单单=!
gar[idb][ida]+=wei;
}
for (int i=0;i<num;i++)
{
if (vis[i]==0)
{
int people=0,head=i,totwei=0;
findmodal(i,head,people,totwei);
if (people>2&&totwei>k)
{
total++;
ishead[head]=people;
}
}
}
cout<<total<<endl;
map<string,int>::iterator it;
for (it=ma.begin();it!=ma.end();it++)
{
if (ishead[it->second]>0)
{
cout<<it->first<<" "<<ishead[it->second]<<endl;
}
} return 0;
}
PATA1034题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- JavaMelody监控spring、struts
前言 前面讲过了Javamelody的基本配置,如何使用Javamelody来监控JDBC以及SQL. 这里继续讲解一下如何监控struts和spring. 手码不易,转载请注明:xingoo 由于s ...
- cobbler自动安装
基础环境:centos7.2 本地IP地址:192.168.56.12 网络环境:桥接模式 一.安装cobbler # rpm -ivh http://mirrors.aliyun.com/epel/ ...
- Android(java)学习笔记11:生产者和消费者之等待唤醒机制
1. 首先我们根据梳理我们之前Android(java)学习笔记70中,关于生产者和消费者程序思路: 2. 下面我们就要重点介绍这个等待唤醒机制: (1)第一步:还是先通过代码体现出等待唤醒机制 下面 ...
- Android(java)学习笔记37:String类型的面试题
1. String类型面试题: package cn.itcast_02; /* * String s = new String(“hello”)和String s = “hello”;的区别? * ...
- 初学MillerRabin素数测试
前言 \(MillerRabin\)素数测试是一种很实用的素数判定方法. 它只针对单个数字进行判定,因而可以对较大的乃至于\(long\ long\)范围内的数进行判定,而且速度也很快,是个十分优秀的 ...
- Codeforces Round #431 (Div. 2)
A. Odds and Ends Where do odds begin, and where do they end? Where does hope emerge, and will they e ...
- 【[COCI2011-2012#5] POPLOCAVANJE】
据说这道题卡空间? 不存在的,拿\(AC\)自动机去存\(5000\times5000\)的串肯定是要M的 我们可以考虑对长度为\(n\)的串建一个\(SAM\),这样空间就只需要两倍的\(3e5\) ...
- 【[SDOi2012]Longge的问题】
求\(\sum_{i=1}^ngcd(i,n)\) 考虑枚举\(gcd\),现在答案变成这样 \(\sum_{d|n}d*f(d)\) \(f(d)=\sum_{i=1}^n [gcd(i,n)==d ...
- 2018.11.29 Struts2中拦截器的学习&项目的实际运用
struts2官方架构 拦截器的创建 第一种方式 第二种方式 看源码AbstractInterceptor 底层已经帮我们写过这些方法了 第三种方式(推荐) 拦截器API学习 放行 前后处理 不放行, ...
- 【转】一个Android项目搞定所有主流架构-1.项目介绍和基本MVC架构示例
http://www.jianshu.com/p/798536fb91c5 项目启发来自谷歌的同类框架项目https://github.com/googlesamples/android-archit ...