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 threthold 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 10Sample Output 1:
2 AAA 3 GGG 3Sample 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 10Sample Output 2:
0
备注:思路就是图的遍历dfs一遍找连通分量,然后判断是否为gang即可。编程细节是名字和数组下标如何映射,我是直接用个map。结果也保存在了map里,省得排序了,偷了个懒。
#include<stdio.h> #include<string.h> #include<vector> #include<map> #include<string> using namespace std; const int MAXSIZE = 2005; typedef struct person { char name[5]; map<int,int> neighbors; bool visited; int weight; }PERSON; map<string, int> Mymap; PERSON graph[MAXSIZE]; void dfs(int node, int *weight, int *n_count, int *maxweight, int *maxid) { PERSON p = graph[node]; graph[node].visited = true; (*n_count)++; (*weight)+= p.weight; if(p.weight>*maxweight) { *maxweight = p.weight; *maxid = node; } for(map<int,int>::iterator iter=p.neighbors.begin();iter!=p.neighbors.end();iter++) { int neigh_id = iter->first; if(!graph[neigh_id].visited) dfs(neigh_id,weight,n_count,maxweight,maxid); } } int main() { int N,Threshold; scanf("%d %d",&N,&Threshold); //initialization for(int i=0;i<MAXSIZE;i++) { graph[i].visited = false; graph[i].weight = 0; } int k = 0; for(int i=0;i<N;i++) { char tmp1[5],tmp2[5]; int length,i1,i2; scanf("%s %s %d",tmp1,tmp2,&length); if(Mymap.count(tmp1)==0) { Mymap[tmp1] = k; i1 = k; k++; } else i1 = Mymap[tmp1]; if(Mymap.count(tmp2)==0) { Mymap[tmp2] = k; i2 = k; k++; } else i2 = Mymap[tmp2]; strcpy(graph[i1].name,tmp1); graph[i1].neighbors[i2] += length; graph[i1].weight += length; strcpy(graph[i2].name,tmp2); graph[i2].neighbors[i1]+= length; graph[i2].weight += length; } //doing dfs to find connected componet int n_clusters = 0; map<string,int> gang; // key -> head's name, value -> num of members in cluster for(int i=0;i<k;i++) { int total_weight = 0, n_nodes = 0; int max_weight = -1, max_id = -1; if(!graph[i].visited) { dfs(i,&total_weight,&n_nodes,&max_weight,&max_id); if(n_nodes>2 && total_weight>Threshold*2) // find a cluster! { n_clusters++; gang[graph[max_id].name] = n_nodes; } } } if(n_clusters==0) { printf("0"); return 0; } printf("%d\n",n_clusters); for(map<string,int>::iterator iter=gang.begin();iter!=gang.end();iter++) printf("%s %d\n",iter->first.c_str(),iter->second); return 0; }