国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

1034. Head of a Gang (30)

2019-11-14 11:17:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1034. Head of a Gang (30)

考察搜索算法; 利用dfs或者bfs判斷連通性,注意自己構(gòu)造name和頂點(diǎn)的對(duì)應(yīng),本代碼采用了map,通過(guò)name字符串快速查找對(duì)應(yīng)頂點(diǎn)

#include <iostream>#include <string>#include <vector>#include <algorithm>#include <map>using namespace std;struct answer{ string name; int num;};int visited[10000]={0};int cntp=0,maxt,head,sumtime;map<string,int> FName;vector<string> FIndex(10000);vector<vector<int>> v(10000);vector<int> rec;int timecnt[10000]={0};bool comp(const answer &a,const answer &b){ return a.name<b.name;}void DFS(int u){ if(visited[u]) return; visited[u]=1;cntp++;sumtime+=timecnt[u]; if(timecnt[u]>maxt) { maxt=timecnt[u]; head=u; } for(auto it=v[u].begin();it!=v[u].end();++it) DFS(*it);}int main(){ int n,limit,index=1; cin>>n>>limit; for(int i=0;i<n;++i) { string strl,strr; int time; cin>>strl>>strr>>time; if(!FName[strl]) { FName[strl]=index; FIndex[index]=strl; ++index; } if(!FName[strr]) { FName[strr]=index; FIndex[index]=strr; ++index; } int u=FName[strl],w=FName[strr]; v[u].push_back(w); v[w].push_back(u); rec.push_back(u); timecnt[u]+=time; timecnt[w]+=time; } vector<answer> ans; for(auto it=rec.begin();it!=rec.end();++it) { cntp=0;maxt=-1;head=-1;sumtime=0; DFS(*it); sumtime/=2; answer temp; if(maxt!=-1&&cntp>2&&sumtime>limit) { temp.name=FIndex[head]; temp.num=cntp; ans.push_back(temp); } } sort(ans.begin(),ans.end(),comp); cout<<ans.size()<<endl; for(auto it=ans.begin();it!=ans.end();++it) cout<<it->name<<" "<<it->num<<endl; return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 望奎县| 沈阳市| 延庆县| 鲁山县| 阿拉善右旗| 苗栗市| 普定县| 岑巩县| 昌乐县| 巴南区| 陇西县| 财经| 海盐县| 富裕县| 兴化市| 昌图县| 凤山市| 石台县| 巴林右旗| 伊春市| 奇台县| 改则县| 太和县| 囊谦县| 塔河县| 上栗县| 三门峡市| 盘山县| 杭州市| 扎鲁特旗| 淮北市| 东明县| 罗江县| 湛江市| 广水市| 尚义县| 孟州市| 马尔康县| 辉南县| 弋阳县| 偃师市|