1034 Head of a Gang 30 ☆☆☆

大年三十还在更新的我,是不是可以拿一个爱岗敬业奖,歪头。 github 地址:https://github.com/iofu728/PAT-A-by-iofu728 难度:☆☆☆ 关键词:DFS,map

题目

1034.Head of a Gang (30)

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 threshold, 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

大意

定义“黑帮”是一种经常通过电话联系的,人数大于 n,通过总时长大于 k 的团体。现给出一系列通过记录,输出“黑帮”数量,“黑帮”头目名称,“黑帮”联系总时长。

思路

  1. 这是很典型的 DFS 题目,连通分量数就是“黑帮”可能数量。
  2. 因为题目给的是字符串名称,建图还是用 int 建比较方便,用一个 hash 来进行转换。
  3. 因为要计算连通数,就需要使用两重 for 的 DFS。
  4. 对每个连通分量计算相应的 num,totalweight。

code

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;
const int maxn = 2018;
struct node {
  int id, num;
  string str;
};
vector<node> v;
map<string, int> stringid;
map<int, string> idstring;

int co = 0;
bool vis[maxn];
int G[maxn][maxn], weight[maxn];
int generateid(string str) {
  if (stringid.find(str) == stringid.end()) {
    stringid[str] = (co);
    idstring[co] = str;
    return co++;
  } else {
    return stringid[str];
  }
}
void DFS(int u, int &father, int &num, int &totalwight) {
  ++num;
  vis[u] = true;
  if (weight[u] > weight[father]) {
    father = u;
  }
  for (int i = 0; i < co; ++i) {
    int temp = G[u][i];
    if (G[u][i]) {
      totalwight += G[u][i];
      G[u][i] = G[i][u] = 0;
      if (vis[i] == false) {
        DFS(i, father, num, totalwight);
      }
    }
  }
}
bool cmp(node a, node b) { return a.str < b.str; }
int main(int argc, char const *argv[]) {
  int n, k;
  cin >> n >> k;
  getchar();
  fill(vis, vis + maxn, false);
  fill(G[0], G[0] + maxn * maxn, 0);
  fill(weight, weight + maxn, 0);
  for (int i = 0; i < n; ++i) {
    int temp, id1, id2;
    string str1, str2;
    cin >> str1 >> str2 >> temp;
    id1 = generateid(str1);
    id2 = generateid(str2);
    G[id1][id2] += temp;
    G[id2][id1] += temp;
    weight[id1] += temp;
    weight[id2] += temp;
    //      cout<<id1<<" "<<id2<<" "<<weight[id1]<<" "<<weight[id2]<<endl;
  }
  //    for(int i=0;i<co;++i){
  //        for(int j=0;j<co;++j){
  //            cout<<G[i][j]<<" ";
  //        }
  //        cout<<endl;
  //    }
  for (int i = 0; i < co; ++i) {
    if (!vis[i]) {
      int father = i, num = 0, totalwight = 0;
      DFS(i, father, num, totalwight);
      //            cout<<father<<" "<<num<<" "<<totalwight<<endl;
      if (num > 2 && totalwight > k) {
        //              cout<<"****";
        node temp;
        temp.id = father;
        temp.str = idstring[father];
        temp.num = num;
        v.push_back(temp);
      }
    }
  }
  cout << v.size() << endl;
  sort(v.begin(), v.end(), cmp);
  for (int i = 0; i < v.size(); ++i) {
    cout << v[i].str << " " << v[i].num << endl;
  }
  return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
You can use this BibTex to reference this blog if you find it useful and want to quote it.