#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
using namespace std;
const int maxn = 2010;
int G[maxn][maxn] = {0},weight[maxn] = {0};
map<string,int> stringtoint;
map<int,string> inttostring;
map<string,int> gang;
bool vist[maxn]={0};
int n,k,numperson=0;
int getname(string a) //给每个人编号,字符串转化为数字
{
if(stringtoint.find(a) != stringtoint.end()) //如果没找到记录
{
return stringtoint[a]; //如果不是第一次,直接输出编号
}
else
{
numperson++; //人数++
stringtoint[a] = numperson; //给与他的编号
inttostring[numperson] = a;
return numperson;
}
}
void DFS(int now,int& head,int& groupper,int& total) //对一个团体DFS,now现在访问节点,head大哥节点,groupper目前团体人数,total整个团体的边权
{
vist[now] = true; //已经访问
groupper++;
if(weight[now] > weight[head]) //点权重更大的是大哥
head = now;
for(int i = 1;i<=numperson;i++) //对他的每一个相连的点
{
if(G[now][i] > 0) //如果相连
{
total += G[now][i];
G[i][now] = 0; //切断返回路径,防止成环的团体重复计算
G[now][i] = 0;
if(vist[i] == false)
DFS(i,head,groupper,total);
}
}
}
void DFSTra()
{
int head,grouper,total=0;
for(int i = 1;i<=numperson;i++)
{
if(vist[i] == false)
{
head = i;
grouper =0;
total = 0;
DFS(i,head,grouper,total);
if(total > k && grouper > 2)
{
gang[inttostring[head]] = grouper;
}
}
}
}
int main()
{
freopen("1.txt","r",stdin);
scanf("%d %d",&n,&k);
for(int i = 0;i<n;i++)
{
string a,b;
int a1,b1,time;
// scanf("%s %s %d",a.c_str(),b.c_str(),&time);
// printf("%s",a.c_str());
cin>>a>>b>>time;
a1 = getname(a);
b1 = getname(b);
G[a1][b1] += time;
G[b1][a1] += time;
weight[a1] += time;
weight[b1] += time;
}
DFSTra();
printf("%d\n",gang.size());
map<string,int>::iterator it;
for(it = gang.begin();it!=gang.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}
1034 Head of a Gang (30分) (DFS 图)
最新推荐文章于 2023-05-18 21:12:59 发布