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 and , we say that and 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 . 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 and (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then 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
#include<iostream> #include<string> #include<map> using namespace std; int N;//线路 int K;//threshold阈值 map<string, int> stringtoint; map<int, string> inttostring;//匹配节点名和数组下标 map<string, int> gang;//记录每个帮派人数 int G[2010][2010] = {0};//邻接矩阵,因为最多1000条路径,因此最多可以右2000人 int weight[2010] = {0};//记录每个节点的权值,用来判断头目 int id_num=0;//总人数 int stoichange(string str) { if (stringtoint.find(str) != stringtoint.end()) {//如果str已经出现 return stringtoint[str];//返回编号 } else {//未出现 stringtoint[str] = id_num;//第id_num个人 inttostring[id_num] = str; return id_num++; } } bool vis[2010];//用来DFS记录是否访问 //获得各联通 void DFS(int u, int& head, int& member_num, int& total_value) { member_num++;//帮派成员加1 vis[u] = true;//已访问 if (weight[u] > weight[head]) {//更新头目 head = u; } for (int i = 0; i < id_num; i++) { //if (vis[i]==false&&G[u][i] > 0) {//只要u和i相连,不管是谁到谁,他俩都在一个连通图里 // total_value += G[u][i]; // //G[u][i] = G[i][u] = 0; // DFS(i, head, member_num, total_value); //} if (G[u][i] > 0) {//A->B->C->A这种情况,则要区别于上面的写法,上面的写法C回不到A则会少计算一条边 total_value += G[u][i]; G[u][i] = G[i][u] = 0;//防止回头,这条边已经访问过了 if (vis[i] == false) { DFS(i, head, member_num, total_value); } } } } void DFSTrave() { for (int i = 0; i < id_num; i++) { if (vis[i] == false) { int head = i; int member_num = 0; int total_weight = 0; DFS(i, head, member_num, total_weight); if (member_num > 2 && total_weight > K) { gang[inttostring[head]] = member_num; } } } } int main() { cin >> N; cin >> K; string h; string t; int w; for (int i = 0; i < N; i++) { cin >> h; cin >> t; cin >> w; int h_id = stoichange(h); int t_id = stoichange(t); //化有向为无向 weight[h_id] += w; weight[t_id] += w; G[h_id][t_id] += w; G[t_id][h_id] += 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; } }
最新评论