立志用最少的代码做最高效的表达
PAT甲级最优题解——>传送门
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 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 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<bits/stdc++.h>using namespace std;struct Set{//作为辅助求解的集合类int head=-1;//头目int weight=0;//集合的总权值int num=0;//集合的人数};const int MAXV=;int weight[MAXV]={0};//点权bool visit[MAXV]={false};//结点是否已被访问Set gang[MAXV];//辅助的计算Gang的数组vector<vector<int>>graph(MAXV);void DFS(int v,int start){visit[v]=true;//将该节点设置为已访问++gang[start].num;//递增该集合人数gang[start].weight+=weight[v];//增加该集合总权值if(gang[start].head==-1)//更新该集合头目gang[start].head=v;else if(weight[v]>weight[gang[start].head])gang[start].head=v;for(int i:graph[v])if(!visit[i])DFS(i,start);}int main(){int N,K;scanf("%d%d",&N,&K);unordered_map<string,int>STOI;//将名字映射到一个整数vector<string>ITOS;//将整数映射到名字for(int i=0;i<N;++i){//读入数据getchar();string s1,s2;cin>>s1>>s2;int w;scanf("%d",&w);if(STOI.find(s1)==STOI.cend()){//如果STOI中没有改名字,将名字加入STOI,并同步更新ITOSSTOI[s1]=ITOS.size();ITOS.push_back(s1);}if(STOI.find(s2)==STOI.cend()){STOI[s2]=ITOS.size();ITOS.push_back(s2);}weight[STOI[s1]]+=w;//更新点权weight[STOI[s2]]+=w;//更新点权graph[STOI[s1]].push_back(STOI[s2]);//向图中增加无向边graph[STOI[s2]].push_back(STOI[s1]);//向图中增加无向边}for(int i=0;i<2*N;++i)//深度优先遍历if(!visit[i])DFS(i,i);map<string,int>result;//存储最终输出结果,利用map自动按头目名字排序for(int i=0;i<N;++i)//遍历找到符合条件的Gangif(gang[i].num>2&&gang[i].weight/2>K)result.insert({ITOS[gang[i].head],gang[i].num});printf("%d\n",result.size());for(auto i=result.cbegin();i!=result.cend();++i)printf("%s %d\n",(i->first).c_str(),i->second);return 0;}