题目: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;
}

  

最新文章

  1. Redis(li)
  2. 转移博客到xinqiyang.freeflare.com了,这里会继续更新.
  3. 【bzoj1562】 NOI2009—变换序列
  4. 如何在Android应用程序中使用传感器模拟器SensorSimulator
  5. MYSQL 5.7 新增150多个新功能
  6. Hadoop 2.x从零基础到挑战百万年薪第一季
  7. A Game of Thrones(18) - Catelyn
  8. ASP.NET 读数据库绑定到 TreeView 递归方式
  9. Talk 3: Rob Pike on Upspin (Gopherfest 2017)
  10. hdu_2444The Accomodation of Students(二分图的判定和计算)
  11. 【JS】 Javascript与BOM的互动 寻路
  12. Final 个人最终作业。
  13. 20155332 2006-2007-2 《Java程序设计》第4周学习总结
  14. Java之从头开始编写简单课程信息管理系统
  15. logging模块知识点及应用小结
  16. python中的clear
  17. 【刷题】BZOJ 4945 [Noi2017]游戏
  18. CentOS 7 mini安装后安装图形界面及远程设置
  19. 基于MVC框架Aspose.Words打印到Word中写法
  20. LR、HMM、CRF和MaxEnt区别

热门文章

  1. NodeJs安装less(npm方式)
  2. June 12th 2017 Week 24th Monday
  3. OAuth 2.0协议在SAP产品中的应用
  4. python接口测试-项目实践(六) 实际结果与预期结果对比之 数据源与数据库对比
  5. C语言 字符串的声明与使用
  6. 关于c++对文件读写的封装
  7. win10的host设置
  8. Windows XP和Wndows7误删除了注册表下.exe文件夹之修复办法
  9. [转]这13个开源GIS软件,你了解几个?
  10. js控件设置只读属性和不可用属性