pat甲级1034 Head of a Gang

One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N lines follow, each in the following format:

Name1 Name2 Time

where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:

For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1:

8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 1:

2
AAA 3
GGG 3

Sample Input 2:

8 70
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 2:

0

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

我的代码

#include<iostream>
#include<map>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;

const int N = 100100;
int e[N], ne[N], h[N], idx;
int n, k, res;

struct info {
    string name1;
    string name2;
    int w;
}f[N];

struct sor {
    string sa;
    int re;
}qq[N];

map<string, int>mp;
map<int, int>l;
map<int, string>q;
int cnt;
bool st[N];
string head;
int num;
int sum;
int ans;
string tt[N];
int hh;
int b[N];

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

bool cmp(info x, info y) {
    return x.name1 < y.name1;
}

bool cmp1(sor x, sor y) {
    return x.sa < y.sa;
}
void dfs(int u) {
    res++;
    st[u] = true;
    sum += l[u];
    if (l[u] > num) {
        num = l[u];
        head = q[u];
    }

    for (int i = h[u]; ~i; i = ne[i]) {
        if (!st[e[i]])  dfs(e[i]);
    }

}

bool check(int a, int b) {
    if (a > 2 && b / 2 > k) {
        return true;
    }
    else return false;
}
int main() {
    cin >> n >> k;
    memset(h, -1, sizeof h);
    for (int i = 1; i <= n; i++) {
        l[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
        cin >> f[i].name1 >> f[i].name2 >> f[i].w;
        if (!mp[f[i].name1]) {
            mp[f[i].name1] = ++cnt;
            q[cnt] = f[i].name1;
        }
        if (!mp[f[i].name2]) {
            mp[f[i].name2] = ++cnt;
            q[cnt] = f[i].name2;
        }
        l[mp[f[i].name1]] += f[i].w;
        l[mp[f[i].name2]] += f[i].w;
        add(mp[f[i].name1], mp[f[i].name2]);
    }
    sort(f + 1, f + 1 + n, cmp);

    for (int i = 1; i <= cnt; i++) {
        res = 0, sum = 0, num = 0;
        if (!st[i]) {
            dfs(i);
            if (check(res, sum)) {
                if (res > 2) ans++;
                qq[ans].sa= head;
                qq[ans].re = res;
            }
        }
    }
    sort(qq + 1, qq + 1 + ans,cmp1);
    cout << ans << endl;

    for (int i = 1; i <= ans; i++) {
        cout << qq[i].sa << " " << qq[i].re << endl;
    }

    return 0;
}

大牛代码

#include<bits/stdc++.h>
using namespace std;

int n, k;
unordered_map<string, vector<pair<string, int>>> g;
unordered_map<string, int> total;
unordered_map<string, bool> st;

//找到该人所在连通块的所有成员
int dfs(string ver, vector<string>& nodes)
{
    nodes.push_back(ver);
    st[ver] = true;

    int sum = 0;  //计算总边权
    for (auto& it : g[ver])    //遍历与其相关的其他人
    {
        sum += it.second;
        string cur = it.first;
        if (!st[cur])    sum += dfs(cur, nodes);
    }

    return sum;
}

int main()
{
    cin >> n >> k;
    for (int i = 0; i < n; i++)    //输入通话信息
    {
        string a, b;
        int t;
        cin >> a >> b >> t;
        g[a].push_back({ b,t });
        g[b].push_back({ a,t });
        total[a] += t, total[b] += t;
    }

    vector<pair<string, int>> res;
    for (auto& it : total)
    {
        string ver = it.first;
        if (st[ver]) continue;	//如果该人已经计算过,说明该连通块已被计算,直接跳过
        
        vector<string> nodes;
        int sum = dfs(ver, nodes) / 2; //获取总边权以及该连通块所有人

        if (nodes.size() > 2 && sum > k)   //如果满足帮派要求
        {
            string boss = nodes[0];   //找到该帮派的头目
            for (auto& no : nodes) //总通话时间最长的就是头目
                if (total[no] > total[boss])
                    boss = no;
            res.push_back({ boss,nodes.size() });
        }
    }

    sort(res.begin(), res.end());    //按头目名字字典序排序

    //输出答案
    cout << res.size() << endl;
    for (auto& it : res)
        cout << it.first << " " << it.second << endl;

    return 0;
}

  • 32
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值