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 
#include 
#include 
#include 
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 ans;
struct member{
    vector 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)k&&sum>2){
                Head one;
                one.hd = head;
                one.mbrs = sum;
                ans.push_back(one);
            }
            tot = 0;
            max_ = 0;
        }
    }
    cout<>n>>k;
    member gang[20000];
    for(int i=0;i>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;
}

你可能感兴趣的:(1034. Head of a Gang (30) PAT甲级刷题)