题解 | #Head of a Gang#

Head of a Gang

https://www.nowcoder.com/practice/a31b1ea6c87647bc86e382acaf21c53b

//暴力解法:照着题目说的统计权重,然后使用map存储结果输出即可
#include <iostream>
#include<cstring>
#include<string>
#include<vector>
#include<map>
const int maxn = 26;
using namespace std;
int father[maxn];
int height[maxn];
int phone[maxn];
void init(int n) {
    for (int i = 0; i < n; i++) {
        father[i] = i;
        height[i] = 0;
        phone[i] = 0;
    }
}

int find(int x) {
    if (x != father[x]) {
        x = father[x];
    }
    return father[x];
}

void Union(int x, int  y) {
    x = find(x);
    y = find(y);
    if (x != y) {
        if (height[x] < height[y]) {
            father[x] = y;
        } else if (height[x] > height[y]) {
            father[y] = x;
        } else {
            father[y] = x;
            height[x] += 1;
        }
    }
}


int countFather(int x) {
    int count = 0;
    for (int i = 0; i < maxn; i++) {
        if (find(i) == find(x)) {
            count++;
        }
    }
    return count;
}


int main() {
    int num, weight;
    string a, b;
    int numa, numb, minute; //通话时长

    while (cin >> num >> weight) { // 注意 while 处理多个 case
        map<int, int> result;
        map<int, string> m;
        init(maxn);
        while (num--) {
            cin >> a >> b >> minute;
            numa = a[0] - 'A';
            numb = b[0] - 'A';
            m[numa] = a;
            m[numb] = b;
            phone[numa] += minute;
            phone[numb] += minute;
            Union(numa, numb);
        }
        for (int i = 0; i < maxn; i++) {
            if (i == find(i) && countFather(i) > 2) {
                int sumWeight, maxWeight, maxIndex;
                sumWeight = 0;
                maxWeight = 0;
                maxIndex = 0;
                for (int j = 0; j < maxn; j++) {
                    if (find(j) == i) {
                        sumWeight += phone[j];
                        if (phone[j] > maxWeight) {
                            maxWeight = phone[j];
                            maxIndex = j;
                        }
                    }
                }
                sumWeight /= 2;
                if (sumWeight > weight) {
                    result[maxIndex] = countFather(i);

                }
            }
        }
        cout << result.size() << endl;
        map<int, int>::iterator it;
        for (it = result.begin(); it != result.end(); it++) {
            cout << m[it->first] << " " << it->second << endl;
        }



    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

03-24 13:24
已编辑
江西农业大学 后端工程师
最近或许大家都听说xxxx厂裁员,无论前端,后端,大数据,测试,运维,人人可危,&nbsp;“前端死了,后端也死了,JAVA崩盘了,你们这群搞大模型的真是码奸”这次AI真的会让我们无路可走吗????????太阳底下已经没有新鲜事了旧的生产力的消失,必然有新的生产力诞生马车夫消失&nbsp;→&nbsp;汽车司机、修车工、石油工业诞生,从业人数是马车夫的百倍手工纺织女工消失&nbsp;→&nbsp;纺织机械工程师、面料设计师诞生,纺织品产量提升百倍2007年苹果开放&nbsp;App&nbsp;Store,&quot;移动端开发者&quot;这个职业压根不存在。八年后,全球应用经济规模突破&nbsp;1000亿美元,凭空诞生了数百万开发者岗位。每一次&quot;这次真的完了...
二十岁的编程男神王大...:那这个时代是什么时代呢? 是全员agent的时代,是前端+AI,后端+AI的时代,AI已经融入了项目生命周期的的每一个角落,那我最近在做的东西举例,检查BUG时,我们会用codex,CC等工具的skill去check,效果好还能直接fix,测试的时候,apifox等工具已经有了AI落地的改造,CI/CD阶段,我们会根据hook去跑AI check脚本,就连不少中间件,也迎来了AI落地的改造,(AI网关,AI在MQ中的运用),都可以去了解下 另外记着,这些东西不是意义,工作只是谋生的一个手段,ai是让开发提效了,但是呢,原先一周的工作流程压缩到了两天内,同时低级的都裁员了,只有高级的去维护,你看似写的大义凛然,或许那天你也会成为你文章里面拒绝往前走的人,你才大二,面对技术有热情是对的
AI求职实录
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务