#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;
}
PAT-A1034
最新推荐文章于 2021-02-01 18:46:05 发布