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

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

1034. Head of a Gang (30)

2019-11-14 10:01:34
字體:
供稿:網(wǎng)友

首先并查集模版要先寫上,hash肯定要用

設(shè)置times數(shù)組記錄每個(gè)人的通話總時(shí)長(zhǎng),并且把所有人合并集合

設(shè)置一個(gè)長(zhǎng)度為N結(jié)構(gòu)體,集合父親對(duì)應(yīng)位置存儲(chǔ)此集合的一些信息,比如集合中所有人的通話總時(shí)長(zhǎng),通話最長(zhǎng)的人,成員數(shù)

遍歷所有的人,把每個(gè)集合的信息存入結(jié)構(gòu)體對(duì)應(yīng)項(xiàng)

為了方便下次找到所有集合的父親,可以開一個(gè)集合(set)存儲(chǔ)遍歷出現(xiàn)的所有父親

遍歷所有父親,篩選所有符合條件的集合,保存每個(gè)集合中最大的人和成員數(shù);

輸出

#include<iostream>#include<algorithm>#include<vector>#include<map> #include<string>#include<set>using namespace std;const int N = 26*26*26 + 1;int fa[N];int times[N] = {0};set<int> all_fa;struct headset{	int name;	int sumt;	int maxt;	int memb;}allset[N];struct heads{	int name;	int memb;};vector<heads> gangs;bool comp(heads a, heads b){	if(a.name < b.name) return true;	else return false;}int name2n(string s){	return ( s[0] - 'A' ) * 26 * 26 + ( s[1] - 'A' ) * 26 + ( s[2] - 'A' );}void initfa(){	for(int i = 0; i < N; i++){		fa[i] = i;	} } int findfather(int x){	if(fa[x] == x) return x;	else{		int F = findfather(fa[x]);		fa[x] = F;		return F;	}}void Union(int a, int b){	int faA = findfather(a);	int faB = findfather(b);	if(faA != faB){		fa[faA] = faB;	}}int main(){	initfa();	int n, k;	cin>>n>>k;	for(int i = 0; i < n; i++){		string s1, s2;		int tempt;		cin>>s1>>s2>>tempt;		int a1 = name2n(s1);		int a2 = name2n(s2);		times[a1] += tempt;		times[a2] += tempt;		Union(a1,a2);//		all_names.insert(a1);//		all_names.insert(a2);	}//	for(set<int>::iterator it = all_names.begin(); it != all_names.end(); it++){//		int tempfa = findfather(*it);//		sumtimes[tempfa] += times[*it];//	}    for(int i = 0; i <= N; i++){    	if(times[i] != 0){    		int tempfa = findfather(i);//    		cout<<tempfa<<endl;    		all_fa.insert(tempfa);    		allset[tempfa].memb++;    		allset[tempfa].sumt += times[i];    		if(allset[tempfa].maxt < times[i]){    			allset[tempfa].maxt = times[i];    			allset[tempfa].name = i;			}		}	}	for(set<int>::iterator it = all_fa.begin(); it != all_fa.end(); it++){//		cout<<*it<<" "<<allset[*it].sumt<<" "<<allset[*it].memb<<" "<<allset[*it].name<<" "<<allset[*it].maxt<<endl;		if(allset[*it].sumt > 2 * k && allset[*it].memb > 2){			heads a;			a.name = allset[*it].name;			a.memb = allset[*it].memb;			gangs.push_back(a);		}	}	sort(gangs.begin(),gangs.end(),comp);	cout<<gangs.size()<<endl;	for(int i = 0; i < gangs.size(); i++){		int tname = gangs[i].name;		PRintf("%c%c%c",tname/26/26 + 'A',tname/26%26 + 'A',tname%26 + 'A');		printf(" %d/n",gangs[i].memb);	}	return 0;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 镇江市| 宜章县| 宝应县| 积石山| 彭水| 富裕县| 突泉县| 福贡县| 青冈县| 家居| 鹤庆县| 永济市| 黔东| 玛多县| 玉门市| 普安县| 武邑县| 怀仁县| 金门县| 拜城县| 友谊县| 民丰县| 邢台市| 江油市| 轮台县| 武义县| 宜州市| 攀枝花市| 涟水县| 宁都县| 东城区| 九台市| 五台县| 重庆市| 临澧县| 黑龙江省| 石渠县| 新乡市| 万安县| 丹江口市| 景洪市|