Pat 1034head of a gang (30 points)

Time:2021-9-6

Pat 1034head of a gang (30 points)

One way that the police finds the head of a gang is to check people’s phone calls. If there is a phone call betweenAandB, we say thatAandBis 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 thresholdK. 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 numbersNandK(both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. ThenNlines follow, each in the following format:

Name1 Name2 Time

whereName1andName2are the names of people at the two ends of the call, andTimeis the length of the call. A name is a string of three capital letters chosen fromAZ. 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

thinking

  • Enter two people at a time, up to2 * nNodes.
  • usemapEstablish the relationship between string name and number.
  • Traversal of the graph, usingdfsFacilitate each connected block, and record the node with the largest weight and the number of nodes.
  • dfsParameters in use reference form.

code

#pragma warning (disable: 4996)
#include <iostream>
#include <string>
#include <map>

using namespace std;

const int maxn = 2010;



map<int, string> intToString;
map<string, int> stringToInt;
map<string, int> gang;

int G[maxn][maxn], weight[maxn];
bool vis[maxn];
int n, k, numPerson;

void dfs(int u, int& head, int& num, int& totalWeight)
{
    num++;
    vis[u] = true;
    if (weight[u] > weight[head])
    {
        head = u;
    }
    for (int v = 0; v < numPerson; v++)
    {
        if (G[u][v] > 0)
        {
            totalWeight += G[u][v];
            G[u][v] = G[v][u] = 0;
            if (vis[v] == false)
            {
                dfs(v, head, num, totalWeight);
            }
        }
    }
}


void  dfsTrave()
{
    for (int u = 0; u < numPerson; u++)
    {
        if (vis[u] == false)
        {
            int head = u, num = 0, totalWeight = 0;
            dfs(u, head, num, totalWeight);
            if (num > 2 && totalWeight > k)
            {
                gang[intToString[head]] = num;
            }
        }
    }
}

int change(string str)
{
    if (stringToInt.find(str) != stringToInt.end())
    {
        return stringToInt[str];
    }
    else
    {
        stringToInt[str] = numPerson;
        intToString[numPerson] = str;
        numPerson++;
        return stringToInt[str];
    }

}

int main()
{
    //freopen("test.txt", "r", stdin);
    string str1, str2;
    int w;
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> str1 >> str2 >> w;
        int id1 = change(str1);
        int id2 = change(str2);
        weight[id1] += w;
        weight[id2] += w;
        G[id1][id2] += w;
        G[id2][id1] += w;
    }

    dfsTrave();
    cout << gang.size() << endl;
    map<string, int > ::iterator it;
    for (it = gang.begin(); it != gang.end(); it++)
    {
        cout << it -> first << " " << it -> second << endl;

    }

    return 0;
}

Reference: algorithm notes