美文网首页
PAT 甲级 刷题日记|A 1034 Head of a Ga

PAT 甲级 刷题日记|A 1034 Head of a Ga

作者: 九除以三还是三哦 | 来源:发表于2021-08-20 08:06 被阅读0次

单词积累

alphabetical 字母的,依字母顺序的

题目

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
结尾无空行

思路

总结题目的关键点:

处理为无向图:虽然题目样例中AAA到BBB和BBB到AAA的时间长度不一样,但是只要A和B之间有连线,就认为是连通的。故处理为无向图,多次出现的边,权值累加即可。

名字的处理:两个map存储 编号-名字,名字-编号信息,即可正常处理。

gang的计算:连通分量的个数:手工开启dfs的个数;连通分量中涉及的节点数,用cnt标记,递归过程中,每调用一次dfs ,cnt++。连通分量中权值的计算:用节点数组存储每个节点涉及的权值,将连通分量涉及的节点权值相加除以二,即可得到。

代码

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

unordered_map<string, int> names;
unordered_map<int, string> id;
int indexs = 1;
int ans = 0;
const int maxn = 2003;
int graph[maxn][maxn];
int visit[maxn];
int vec[maxn];
int wei[maxn];
int num[maxn];
int flag[maxn];
int cnt = 0;

struct node{
    string name;
    int num;
}Node[maxn];

void dfs (int root, int vis) {
    visit[root] = vis;
    cnt++;
    for (int i = 1; i < indexs; i++) {
        if (graph[root][i] != 0 && visit[i] == 0) {
            dfs(i, vis);    
        }
    }
}

bool cmp (node a, node b) {
    return a.name < b.name;
}

int main() {
    int n, ku;
    cin>>n>>ku;
    string name1, name2;
    int len;
    for (int i = 0; i < n; i++) {
        cin>>name1>>name2>>len;
        if (!names[name1]) {
            names[name1] = indexs;
            id[indexs]= name1;
            indexs++;
        }
        if (!names[name2]) {
            names[name2] = indexs;
            id[indexs]= name2;
            indexs++;
        }
        graph[names[name1]][names[name2]] += len;
        graph[names[name2]][names[name1]] += len;
        vec[names[name1]] += len;
        vec[names[name2]] += len;
    }
    for (int i = 0;i < indexs; i++) {
        visit[i] = 0;
    }
    int times = 0;
    for (int i = 1;i < indexs; i++) {
        if(visit[i] == 0) {
            times++;
            cnt = 0;
            dfs(i, times);
            num[times] = cnt;
        }
    }
    for (int i = 1; i <= times; i++) {
        for (int j = 1; j < indexs; j++) {
            if (visit[j] == i ) {
                wei[i] += vec[j];
            }
            if (wei[i] > 2 * ku) break;
        }

        if (wei[i] <= 2 * ku) flag[i] = 1;
        if (num[i] <= 2) flag[i] = 1;
        if (flag[i] == 0) {
            int maxhe = 0;
            int maxid = 0;
            
            for (int j = 1; j < indexs; j++) {
                int he = 0;
                if (visit[j] == i) {
                    for (int k = 0; k < indexs; k++) {
                        if (visit[k] == i) he += graph[j][k];
                    }
                }
                if (he > maxhe) {
                    maxhe = he;
                    maxid = j;
                }
                
            }
            Node[ans].name = id[maxid];
            Node[ans].num = num[i];
            ans++;
        }
    }
    sort(Node, Node+ans, cmp);
    cout<<ans<<endl;
    for (int i = 0; i < ans; i++) {
        cout<<Node[i].name<<" "<<Node[i].num<<endl;
    }
    
    
} 

相关文章

网友评论

      本文标题:PAT 甲级 刷题日记|A 1034 Head of a Ga

      本文链接:https://www.haomeiwen.com/subject/mnuubltx.html