PAT甲级1034

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zengyz-wh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值