PAT-A1034

#include<stdio.h>
#include<iostream>
#include<string>
#include<map>
using namespace std;
int n, k, num_person=0;
bool visited[3002]={false};
int weight[3002]={0}, G[3002][3002]={0};
map<string,int> string_to_int;
map<int, string> int_to_string;
map<string, int> gang;

int change(string cc){
	if(string_to_int.find(cc)!=string_to_int.end())return string_to_int[cc];
	else{
		string_to_int[cc]=num_person;
		int_to_string[num_person]=cc;
		num_person++;
		return num_person-1;
	}
}
void DFS(int now, int &head, int &num, int &total_weight){
	num++;
	visited[now]=true;
	if(weight[now]>weight[head])head=now;
	for(int i=0;i<num_person;i++){
		if(G[now][i]>0){
			total_weight+=G[now][i];
			G[i][now]=G[now][i]=0;
			if(!visited[i])DFS(i,head,num,total_weight);
		}
	}
}
void DFSTrave(){
	for(int i=0;i<num_person;i++){
		if(!visited[i]){
			int head=i, num=0, total_weight=0;
				DFS(i,head,num,total_weight);
		    	if(num>2&&total_weight>k)gang[int_to_string[head]]=num;
		}
	}
}
int main(){
int i, w, id1, id2;
string aa, bb;
cin>>n>>k;
for(i=0;i<n;i++){
	cin>>aa>>bb>>w;
	id1=change(aa);
	id2=change(bb);
	G[id1][id2]+=w; G[id2][id1]+=w;
	weight[id1]+=w;    weight[id2]+=w;
}
DFSTrave();
printf("%d\n",gang.size());
for(map<string, int>::iterator it=gang.begin();it!=gang.end();it++){
	cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值