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