1034. Head of a Gang (30) PAT 甲级
来源:互联网 发布:程序员开发手册 编辑:程序博客网 时间:2024/05/22 06:07
传送门
坑点
1000组通话,因此用户可能有2000个。
所以数组应该开2000+
#include<stdio.h>#include<map>#include<string.h>#include<iostream>using namespace std;const int MAX_N=2100;int personNum=0;map<string,int> NametoId;map<int,string> IdtoName;map<string,int> Gang;int weight[MAX_N];int G[MAX_N][MAX_N];bool visited[MAX_N];int change(string s){ if(NametoId.find(s)!=NametoId.end()){ return NametoId[s]; } else{ NametoId[s]=personNum; IdtoName[personNum]=s; return personNum++; }}void dfs(int now,int &head,int &memberNum,int &totalweight){ memberNum++; visited[now]=true; if(weight[now]>weight[head]){ head=now; } for(int i=0;i<personNum;i++){ if(G[now][i]>0){ totalweight+=G[now][i]; G[now][i]=G[i][now]=0; if(!visited[i]){ dfs(i,head,memberNum,totalweight); } } }}int main(){ int n,K; string a,b; int w; cin>>n>>K; for(int i=0;i<n;i++){ cin>>a>>b>>w; int id1=change(a); int id2=change(b); weight[id1]+=w; weight[id2]+=w; G[id1][id2]+=w; G[id2][id1]+=w; } for(int i=0;i<personNum;i++){ if(!visited[i]){ int head=i,memberNum=0,totalweight=0; dfs(i,head,memberNum,totalweight); if(totalweight>K&&memberNum>2){ Gang[IdtoName[head]]=memberNum; } } } cout<<Gang.size()<<endl; for(map<string,int>::iterator it=Gang.begin();it!=Gang.end();it++){ cout<<it->first<<" "<<it->second<<endl; }}
0 0
- 【PAT甲级】1034. Head of a Gang (30)
- 1034. Head of a Gang (30) PAT 甲级
- PAT甲级练习1034. Head of a Gang (30)
- PAT甲级1034. Head of a Gang (30)
- PAT 甲级 1034. Head of a Gang (30)
- PAT甲级 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)-PAT甲级真题
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- pat 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- 【PAT】1034. Head of a Gang (30)
- 1034. Head of a Gang (30)-PAT甲级真题(图的遍历dfs)
- PAT A 1034. Head of a Gang (30)
- PAT-A-1034. Head of a Gang (30)
- PAT 1034. Head of a Gang
- PAT 1034. Head of a Gang
- angular生命周期
- solr6高亮solrj設置
- Tomcat:IOException while loading persisted sessions: java.io.EOFException解决
- art 解决grub2-install安装报错 grub2-install: error: disk ‘hostdisk//dev/sda1’ not found.
- HDU 6011
- 1034. Head of a Gang (30) PAT 甲级
- C-F1. 忘记密码怎么办 ❀ C3750-E ❀ 思科 (CISCO) 交换机
- 78. Subsets
- Java 流(Stream)、文件(File)和IO
- Kubernetes1.4新特性前瞻:设置JOB执行计划
- 自定义Toast悬浮停靠问题
- http://36kr.com/p/5064568.html?ktm_source=feed
- 这套公式让你了解javascript的作用域
- 秒杀系统的架构解决之道