PAT 1034. Head of a Gang (30) DFS,删边
来源:互联网 发布:什么是数据api接口 编辑:程序博客网 时间:2024/05/20 11:33
一开始以为是用并查集,后来发现是用DFS的。亮点在于DFS只是载体,删掉遍历过的边才是重点
#include <iostream>#include<map>#include<string>#include<algorithm>using namespace std;int sum=0;int edge[1001][1001]={0};int cnt;int head=-1;int p;int tiime[2002]={0};int mark[2002]={0};int max(int z,int c) {return z>c?z:c;}int toutou;struct ZZ{ int id; int cnt; string name; bool operator < (const ZZ &A) const{ return name<A.name; }}tutu[1000]; void DFS(int x) { if(mark[x]==0)cnt++; mark[x]=1; for(int i=0;i<p;i++) { if(edge[x][i]>0) { tiime[x]+=edge[x][i]; tiime[i]+=edge[x][i]; sum+=edge[x][i]; edge[x][i]=edge[i][x]=0; DFS(i); } head=max(head,tiime[x]); if(head==tiime[x]) toutou=x; } }int main(){ int n,k; cin>>n>>k; p=0; map<string,int> M; map<int,string> SM; for(int i=0;i<n;i++) { int c; int t1,t2; string a,b; cin>>a>>b>>c; if(M.find(a)==M.end()) {M[a]=p++;t1=p-1;} else t1=M[a]; if(M.find(b)==M.end()) {M[b]=p++;t2=p-1;} else t2=M[b]; edge[t1][t2]+=c; edge[t2][t1]+=c; SM[t1]=a; SM[t2]=b; } int p2=0; for(int i=0;i<p;i++) { cnt=0; head=-1; toutou=-1; sum=0; if(mark[i]==0) DFS(i); if(cnt>2&&sum>k) { tutu[p2].id=toutou; tutu[p2].cnt=cnt; tutu[p2].name=SM[toutou]; p2++; } } sort(tutu,tutu+p2); cout<<p2<<endl; for(int i=0;i<p2;i++) cout<<SM[tutu[i].id]<<' '<<tutu[i].cnt<<endl; return 0;}
阅读全文
0 0
- PAT 1034. Head of a Gang (30) DFS,删边
- 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)
- PAT-A 1034. Head of a Gang (dfs)
- 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
- PAT 1034. Head of a Gang
- PAT 1034. Head of a Gang
- 【PAT】1034. Head of a Gang
- PAT A1034. Head of a Gang (30)
- pat-a1034. Head of a Gang (30)
- 算法题之-二维数组元素查找
- [Android6.0][RK3399] 实现耳机和喇叭自动切换功能
- SOCKET用户接口与系统调用关系
- Eclipse安装
- UVA
- PAT 1034. Head of a Gang (30) DFS,删边
- 【HDU
- linux的Anaconda环境下安装TensorFlow
- HDU 6166 2017多校 Team09 1006:Dijkstra顶点子集最短路径
- 深入了解HDMI接口
- JDBC概述和初体验
- 使用Rancher创建负载均衡的容器应用
- java提高篇(七)-----关键字static
- 【tensorflow 学习】seq2seq模型代码解读