1035 head of a gang
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1035 head of a gang相关的知识,希望对你有一定的参考价值。
简单并查集
AC代码
#include <map> #include <string> #include <iostream> #include <cstdio> #include <vector> using namespace std; string find(map<string,string>& r,string n){ while(n != r[n]){ n = r[n]; } return n; } void unit(map<string,string>& r,map<string,int>& t,string n1,string n2){ string s1(find(r,n1)),s2(find(r,n2)); if(t[s1] < t[s2]) r[s1] = s2; else if(t[s2] < t[s1]) r[s2] = s1; else if(s2 < s1) r[s1] = r[s2]; else r[s2] = r[s1]; } int main(){ int n,k; scanf("%d %d",&n,&k); map<string,string> r; map<string,int> t; map<string,bool> vs; vector<pair<string,string>> rini; for(int i = 0;i < n;i++){ string n1,n2; int tc; cin >> n1 >> n2 >> tc; vs[n1] = vs[n2] = false; pair<string,string> p; p.first = n1; p.second = n2; rini.push_back(p); t[n1] += tc; t[n2] += tc; r[n1] = n1; r[n2] = n2; } for(int i = 0;i < n;i++){ unit(r,t,rini[i].first,rini[i].second); } int count(0); map<string,int> gt; map<string,int> gn; for(map<string,string>::iterator ite = r.begin();ite != r.end();ite++){ gn[find(r,ite->first)]++; gt[find(r,ite->first)] += t[ite->first]; } for(map<string,int>::iterator ite = gt.begin();ite != gt.end();ite++){ if(ite->second <= 2 * k || gn[ite->first] <= 2) gn.erase(ite->first); } printf("%d\n",gn.size()); for(map<string,int>::iterator ite = gn.begin();ite != gn.end();ite++){ printf("%s %d\n",ite->first.c_str(),ite->second); } return 0; }
以上是关于1035 head of a gang的主要内容,如果未能解决你的问题,请参考以下文章