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 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
解題思路
題目大意: 給你N個通話記錄,通話記錄分別是通話的兩個人A和B的Name,以及通話時間Time,如果兩個人有通話記錄,認定他們是一個團伙的,權重以通話時長為標準,比如A和B通話,B和C通話,那麼A、B、C是一個團伙的,規定團伙必須超過兩個人。一個團伙中,通話時間最長且超過給定閾值K的人是該團伙的老大。
解題思路: 一個通話記錄確定兩個結點的通路,通話時間可以看做兩者的權重,N張通話記錄確定一張關係圖,而一個團伙對應圖中的連通分量,我們可以對整張圖做深度優先搜尋,確定連通分量的個數,然後判斷該連通分量是否存在符合要求的大佬,統計大佬個數。、
由於每個人的ID是通過字串給出的,需要離散化。遍歷的過程中需要實時更新權值最大的節點,使用map容器來完成對圖的構建,以及使用map來對節點權值的索引;可以說此題就是在考察對map的靈活使用,當要遍歷某個資料結構的時候,如果要檢索的索引是字串,就要考慮使用map,並且使用map來儲存資料,天然已經按字串的字典序排好了。最後輸出的時候避免排序
/*
** @Brief:No.1034 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2018-12-11
** @status: Accepted!
*/
#include <iostream>
#include <string.h>
#include <map>
#include <vector>
using namespace std;
string head;
int cnt,total;
map<string,int>weight;
map<string,bool>visit;
map<string,vector<string> >adjlist;
map<string,int>res;
void DFS(string start)
{
visit[start]=1;
cnt++;
total+=weight[start];
if(weight[start]>weight[head])head=start;
vector<string>::iterator it=adjlist[start].begin();
for(;it!=adjlist[start].end();it++)
{
if(visit[*it]==0)
{
DFS(*it);
}
}
}
int main()
{
int N,K,T,i=0;
cin>>N>>K;
string member1,member2;
while(i<N)
{
cin>>member1>>member2>>T;
weight[member1]+=T;
weight[member2]+=T;
adjlist[member1].push_back(member2);
adjlist[member2].push_back(member1);
visit[member1]=0;
visit[member2]=0;
i++;
}
map<string,bool>::iterator ite=visit.begin();
for(;ite!=visit.end();ite++)
{
if(ite->second==0)
{
total=0;
cnt=0;
head=ite->first;
DFS(ite->first);
}
if(cnt>2&&total/2>K)res[head]=cnt;
}
map<string,int>::iterator it=res.begin();
cout<<res.size()<<endl;
for(;it!=res.end();it++)cout<<it->first<<" "<<it->second<<endl;
return 0;
}
總結
30分的題,確實挺難的,一下子沒做出來,最後參考了@浙大太子爺 的解題部落格才搞定的,不得不說,大佬的程式碼寫的挺好的。能夠靈活運用map也是牛批。