#include<iostream>
#include<map>
#include<string>
using namespace std;
const int maxv=2010;
const int inf=1e9;
int g[maxv][maxv];
int weight[maxv];
int vis[maxv];
map<string,int>str2int;
map<int,string>int2str;
map<string,int>res;
int n,k;
int numv=0;
int member,total,w,head;
int exchange(string str)
{
if(str2int.find(str)!=str2int.end())
return str2int[str];
else
{
str2int[str]=numv;
int2str[numv]=str;
return numv++;
}
}
void dfs(int u,int &member,int &total,int &w,int &head)
{
vis[u]=1;
member++;
total+=weight[u];
for(int v=0;v<numv;v++)
{
if(!vis[v]&&g[u][v]!=0)
{
if(weight[v]>w)
{
w=weight[v];
head=v;
}
dfs(v,member,total,w,head);
}
}
}
int main()
{
string name1,name2;
int timelen;
fill(g[0],g[0]+maxv*maxv,0);
fill(vis,vis+maxv,0);
fill(weight,weight+maxv,0);
//freopen("in.txt","r",stdin);
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>name1>>name2>>timelen;
int a=exchange(name1);
int b=exchange(name2);
g[a][b]+=timelen;
g[b][a]+=timelen;
weight[a]+=timelen;
weight[b]+=timelen;
}
int ans=0;
for(int i=0;i<numv;i++)
{
if(!vis[i])
{
total=0;
member=0;
w=weight[i];
head=i;
dfs(i,member,total,w,head);
if(total/2>k&&member>2)
{
ans++;
res[int2str[head]]=member;
}
}
}
cout<<ans<<endl;
map<string,int>::iterator it;
for(it=res.begin();it!=res.end();it++)
cout<<it->first<<" "<<it->second<<endl;
return 0;
}
PAT1034
最新推荐文章于 2022-03-11 19:25:23 发布