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 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
推荐指数:※※
来源:http://pat.zju.edu.cn/contests/pat-a-practise/1034
这道题通过 寻找图的连通子图问题可以解决。
注意,最后结果排序,可能你按照点的顺序遍历,但Gang的位置是不定的。
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; const int N=26*26*26+1; int name_to_int(char *s){ return (s[0]-'A')*26*26+(s[1]-'A')*26+(s[2]-'A'); } void int_to_name(int n,char *s){ s[3]='\0'; s[2]=n%26+'A'; s[1]=(n/26)%26+'A'; s[0]=n/(26*26)+'A'; } typedef struct peop{ char name[4]; int name_num; }peop; typedef struct res{ char name[4]; int total_num; }res;//store one result bool compare(res a,res b){ return strcmp(a.name,b.name)<0; } int ptime[N];//weight of every people int visited[N];// is visited vector<peop> v[N]; int dfs(int index,int longest_time,char *longest_name,int *total_num) { int j; int s_w=0;//store this Gang weight if(visited[index]==0&&v[index].size()>0){// this people is not visited ,and exist visited[index]=1; for(j=0;j<v[index].size();j++){ if(v[index][j].name_num!=index&&visited[v[index][j].name_num]==0){ if(ptime[v[index][j].name_num]>longest_time){ longest_time=ptime[v[index][j].name_num]; strcpy(longest_name,v[index][j].name); } s_w+=ptime[v[index][j].name_num]; (*total_num)++; s_w+=dfs(v[index][j].name_num,longest_time,longest_name,total_num); } } } return s_w; } int main() { int n,k,i; memset(ptime,0,sizeof(ptime)); memset(visited,0,sizeof(visited)); scanf("%d%d",&n,&k); for(i=0;i<n;i++){ peop ap,bp; int weight; scanf("%s%s%d",ap.name,bp.name,&weight); ap.name_num=name_to_int(ap.name); bp.name_num=name_to_int(bp.name); ptime[ap.name_num]+=weight; ptime[bp.name_num]+=weight; v[ap.name_num].push_back(bp); v[bp.name_num].push_back(ap); } vector<res> r; for(i=0;i<N;i++){ if(visited[i]==0&&v[i].size()>0){ res tmp_r; int total_num=1; int longest_time=ptime[i]; char longest_name[4]; int_to_name(i,longest_name); int tmp_w=ptime[i]; tmp_w+=dfs(i,longest_time,longest_name,&total_num); if(total_num>2&&tmp_w/2>k){ strcpy(tmp_r.name,longest_name); tmp_r.total_num=total_num; r.push_back(tmp_r); } } } printf("%d\n",r.size()); sort(r.begin(),r.end(),compare); for(i=0;i<r.size();i++) printf("%s %d\n",r[i].name,r[i].total_num); return 0; }
发现这样通过映射,如果名字比较长,就没有办法解决。改进的map版(AC)
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<string> #include<vector> #include<map> #include<algorithm> using namespace std; typedef struct res{ string name; int total_num; }res; map< string, vector<string> > calls; map<string,int> time_call; map<string,bool> visited; bool compare(res a,res b){ return (a.name).compare(b.name)<0; } int dfs(string pname,int longest_time,string *longest_name,int *total_num) { int j; int s_w=0;//store this Gang weight if(visited[pname]==0&&calls[pname].size()>0){// this people is not visited ,and exist visited[pname]=true; vector<string>::iterator iter; for(iter=calls[pname].begin();iter!=calls[pname].end();iter++){ if(false==visited[(*iter)]){ string tmp_name=(*iter); if(time_call[tmp_name]>longest_time){ longest_time=time_call[tmp_name]; *longest_name=tmp_name; } s_w+=time_call[tmp_name]; (*total_num)++; s_w+=dfs(tmp_name,longest_time,longest_name,total_num); } } } return s_w; } int main() { int n,k,i; scanf("%d%d",&n,&k); for(i=0;i<n;i++){ int ptime; string a,b; cin>>a>>b>>ptime; calls[a].push_back(b); calls[b].push_back(a); time_call[a]+=ptime; time_call[b]+=ptime; visited[a]=false; visited[b]=false; } vector<res> r; map< string, vector<string> >::iterator iter; for(iter=calls.begin();iter!=calls.end();iter++){ if(false==visited[(*iter).first]){ string tmp_name=(*iter).first; int longest_time=time_call[tmp_name]; string longest_name=tmp_name; int total_num=1; int tal_time=time_call[tmp_name]; tal_time+=dfs(tmp_name,longest_time,&longest_name,&total_num); if(total_num>2&&tal_time>2*k){ res tmp_r; tmp_r.name=longest_name; tmp_r.total_num=total_num; r.push_back(tmp_r); } } } printf("%d\n",r.size()); sort(r.begin(),r.end(),compare); for(i=0;i<r.size();i++) cout<<r[i].name<<" "<<r[i].total_num<<endl; return 0; }