1034. Head of a Gang (30) PAT甲级刷题

题目描述

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.

输入描述:

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.


输出描述:

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.

输入例子:

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

输出例子:

2
AAA 3

GGG 3

思路:1.对名字进行编号作为图结点的序号,视为3位26进制数,由于不是顺序编号,所以还要一个bool数组记录各个结点是否出现过。

2.dfs算连通分量数,各个连通分量权值和、权值最大结点、结点数。用全局变量或引用传参。

3.题目所示图是又向的,但这里要当成无向图处理,所以最后算各个连通分量权值和时要除2。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int n,k,tm_,tot=0,max_=0;
bool has[20000] = {0};
bool visited[20000] = {0};
struct Head{
    int hd;
    int mbrs;
};
vector<Head> ans;
struct member{
    vector<int> next;
    int time=0;
};
int nametoint(string s){
    for(auto i=s.size();i<3;++i)
        s = 'A'+s;
    return (s[0]-'A')*26*26+(s[1]-'A')*26+s[2]-'A';
}
string inttoname(int n){
    string s;
    while(n!=0){
        char c = n%26+'A';
        s = c+s;
        n/=26;
    }
    return s;
}
bool cmp(Head a,Head b){
    return inttoname(a.hd)<inttoname(b.hd);
}
int dfs(member gang[],int v,int &head,int &n){
    tot += gang[v].time;
    ++n;
    if(max_<gang[v].time){
        max_ = gang[v].time;
        head = v;
    }
    visited[v] = 1;
    for(int i=0;i<gang[v].next.size();++i){
        int nxt = gang[v].next[i];
        if(!visited[nxt])
            dfs(gang,nxt,head,n);
    }
    return tot;
}
void dfstvs(member gang[]){
    for(int i=0;i<20000;++i){
        if(has[i]&&!visited[i]){
            int head = 0,sum = 0;
            int tt = dfs(gang,i,head,sum);
            if(tt/2>k&&sum>2){
                Head one;
                one.hd = head;
                one.mbrs = sum;
                ans.push_back(one);
            }
            tot = 0;
            max_ = 0;
        }
    }
    cout<<ans.size()<<endl;
    sort(ans.begin(),ans.end(),cmp);
    for(int i=0;i<ans.size();++i)
        cout<<inttoname(ans[i].hd)<<' '<<ans[i].mbrs<<endl;
}
int main()
{
    string a,b;
    cin>>n>>k;
    member gang[20000];
    for(int i=0;i<n;++i){
        cin>>a>>b>>tm_;
        int a_int = nametoint(a);
        int b_int = nametoint(b);
        has[a_int] = 1;
        has[b_int] = 1;
        gang[a_int].next.push_back(b_int);
        gang[a_int].time += tm_;
        gang[b_int].next.push_back(a_int);
        gang[b_int].time += tm_;
    }
    dfstvs(gang);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值