#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
map<string,int> mp1;
map<int,string> mp2;
int N,K;
int father[2100];
int weight[2100];
struct gang
{
int num;
int leader;
int weight;
}G[2100];
int h[2100];
int findfather(int x)
{
int a=x;
while(x!=father[x])
{
x=father[x];
}
while(a!=father[a])
{
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
int Union(int a,int b)
{
int fa=findfather(a);
int fb=findfather(b);
if(fa!=fb)
{
father[fa]=fb;
}
}
bool cmp(gang a,gang b)
{
return mp2[a.leader]<mp2[b.leader];
}
int main()
{
for(int i=0;i<2100;i++)
{
father[i]=i;
}
memset(h,-1,sizeof(h));
scanf("%d%d",&N,&K);
char name1[10],name2[10];
int w;
int num=0;
for(int i=0;i<N;i++)
{
scanf("%s %s %d",&name1,&name2,&w);
if(mp1.find(name1)==mp1.end())
{
mp1[name1]=num;
mp2[num]=name1;
num++;
}
if(mp1.find(name2)==mp1.end())
{
mp1[name2]=num;
mp2[num]=name2;
num++;
}
int i1=mp1[name1];
int i2=mp1[name2];
weight[i1]+=w;
weight[i2]+=w;
Union(i1,i2);
}
int ng=0;
for(int i=0;i<num;i++)
{
int fa=findfather(i);
if(h[fa]==-1)
{
G[ng].num=0;
G[ng].leader=i;
G[ng].weight=0;
h[fa]=ng;
ng++;
}
int gi=h[fa];
G[gi].num++;
G[gi].weight+=weight[i]/2;
if(weight[i]>weight[G[gi].leader])
{
G[gi].leader=i;
}
}
int resultn=0;
for(int i=0;i<ng;i++)
{
if(G[i].weight>K&&G[i].num>2)
{
resultn++;
}
}
sort(G,G+ng,cmp);
printf("%d\n",resultn);
for(int i=0;i<ng;i++)
{
if(G[i].weight>K&&G[i].num>2)
{
cout<<mp2[G[i].leader]<<" "<<G[i].num<<endl;
}
}
return 0;
}
PAT甲1034 Head of a Gang (30)(30 分)
最新推荐文章于 2019-08-30 17:36:15 发布