日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people‘s pho

發布時間:2024/2/28 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people‘s pho 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

立志用最少的代碼做最高效的表達


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=2005; 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; }

耗時:

總結

以上是生活随笔為你收集整理的1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people‘s pho的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。