题目: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题解的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. ios端微信浏览器禁止上下滑动

    在body里的第一个div容器中添加 position:fixed

  2. WAKE-WIN10-SOFT-软件-Matlab配置及工具箱

    1Matlab 1,1Matlab下载,安装,配置,,, 1,2 2工具箱 2,1LibSVM 必应:https://www.bing.com/search?q=libsvm&qs=n& ...

  3. 部署Smokeping

    1.环境安装 # yum -y install gcc* perl* httpd* libxml2 libxml2-devel libpng libpng-devel glib2 glib2-deve ...

  4. javascript DOM 操作基础知识小结

    经常用到javascript对dom,喜欢这方便的朋友也很多,要想更好的对dom进行操作,这些基础一定要知道的.   DOM添加元素,使用节点属性  <!DOCTYPE html PUBLIC ...

  5. Android学习笔记_50_(转 四种加载方式详解(standard singleTop singleTask singleInstance)

    Android之四种加载方式 (http://marshal.easymorse.com/archives/2950 图片) 在多Activity开发中,有可能是自己应用之间的Activity跳转,或 ...

  6. 消息中间件JMS(二)

    之前介绍了ActiveMQ下载与安装,并且启动了.下面进行ActiveMQ的Demo 1. JMS入门Demo 1.1 点对点模式 点对点模式主要建立在一个队列上面,当连接一个队列的时候,发送端不需要 ...

  7. code First 四

    先从现有数据库获取代码:  我们创建模型的时候选择Code First就可以了 public ModelStudent() : base("name=ModelStudent") ...

  8. 关于SQLNET.AUTHENTICATION_SERVICES= (NTS) 的解释

    原文转自:http://www.360doc.com/content/12/0207/12/3446769_184740592.shtml       标题所代表的意思为 使用操作系统本地验证,一般不 ...

  9. 概括iOS知识点思维导图

  10. iOS之性能的优化

    本文转自:http://m.blog.csdn.net/article/details?id=51638925 写在前面 本文来自iOS Tutorial Team 的 Marcelo Fabri,他 ...