solution
#include <iostream>
#include <map>
using namespace std;
int G[2010][2010], wight[2010];
bool vis[2010];
map<string, int> stringToInt;
map<int, string> intToString;
map<string, int> ans;
int idNumber = 1, k;
int stoifunc(string s)
{
if (stringToInt[s] == 0)
{
stringToInt[s] = idNumber;
intToString[idNumber] = s;
return idNumber++;
}
else
{
return stringToInt[s];
}
}
void dfs(int u, int &head, int &numMember, int &totalweight)
{
vis[u] = true;
numMember++;
if (wight[u] > wight[head])
head = u;
for (int v = 1; v < idNumber; v++)
{
if (G[u][v] > 0)
{
totalweight += G[u][v];
G[u][v] = G[v][u] = 0;
if (vis[v] == false)
dfs(v, head, numMember, totalweight);
}
}
}
void dfstrace()
{
for (int i = 1; i < idNumber; i++)
{
if (!vis[i])
{
int head = i, numMember = 0, totalweight = 0;
dfs(i, head, numMember, totalweight);
if (numMember > 2 && totalweight > k)
ans[intToString[head]] = numMember;
}
}
}
int main()
{
int n, w;
cin >> n >> k;
string s1, s2;
for (int i = 0; i < n; i++)
{
cin >> s1 >> s2 >> w;
int id1 = stoifunc(s1);
int id2 = stoifunc(s2);
wight[id1] += w;
wight[id2] += w;
G[id1][id2] += w;
G[id2][id1] += w;
}
dfstrace();
cout << ans.size() << endl;
for (auto it = ans.begin(); it != ans.end(); it++)
cout << it->first << " " << it->second << endl;
return 0;
}