PTA_PAT甲級_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 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

題意:
給出若干人的通話記錄,若有聯繫的某幾個人(大於2個)的通話總時長大於一個閾值,則這幾個人構成一個犯罪團伙,其中與其他人通話時間和最長的人爲頭目,輸出犯罪團伙的個數和各個團伙的頭目和團伙成員數

分析:
用map映射名字與編號,用兩次DFS分別獲取每個連通塊內的頭目和人數以及每個連通塊的信息

代碼:

#include<iostream>
#include<string>
#include<map>
using namespace std;

#define MAXN 2010
map<int, string> intToString;//編號->姓名 
map<string, int> stringToInt;//姓名->編號 
map<string, int> Gang;//head->人數
int G[MAXN][MAXN]={0}, weight[MAXN]={0};//鄰接矩陣,點權
int N, K, numPerson=0;//邊數,下限,總人數
bool vis[MAXN] = {false};//標記是否被訪問 

//DFS函數訪問單個連通塊,nowVisit爲當前訪問的編號
//head爲頭目,numMember爲成員編號,totalValue爲連通塊的總邊權 
void DFS(int nowVisit, int& head, int& numMember, int& totalValue){
	numMember++;//成員人數加1 
	vis[nowVisit] = true; 
	if(weight[nowVisit]>weight[head]) head = nowVisit;//當前節點點權更大則更新頭目
	for(int i=0;i<numPerson;i++)//枚舉所有人 
		if(G[nowVisit][i]>0){//如果從當前節點能到達 
			totalValue += G[nowVisit][i];//連通塊總邊權增加該邊權
			G[nowVisit][i] = G[i][nowVisit] = 0;//**刪除此邊,防止回頭**
			if(vis[i]==false) DFS(i, head, numMember, totalValue);//若i爲被訪問則遞歸訪問i 
		} 
}

//DFSTrave函數遍歷整個圖,獲取每個連通塊的信息 
void DFSTrave(){
	for(int i=0;i<numPerson;i++)//枚舉所有人 
		if(vis[i]==false){//如果i未被訪問 
			int head=i, numMember=0, totalValue=0;//頭目、成員數及總邊權
			DFS(i, head, numMember, totalValue);//遍歷i所在的連通塊
			if(numMember>2 && totalValue>K)//如果成員數大於2且總邊權大於K 
				Gang[intToString[head]] = numMember; //head的人數爲numMember 
		}
}

//change函數返回姓名str對應的編號
int change(string str){
	if(stringToInt.find(str)!=stringToInt.end()) return stringToInt[str];//已出現過則返回編號
	else{
		stringToInt[str] = numPerson;//str的編號爲numPerson
		intToString[numPerson] = str;//numPerson對應str
		return numPerson++;//總人數加1 
	} 
} 

int main()
{
    cin>>N>>K;
	for(int i=0;i<N;i++){
		string str1, str2;
		int w;
		cin>>str1>>str2>>w;//兩個端點字符和點權 
		int id1 = change(str1);//轉換爲編號 
		int id2 = change(str2); 
		weight[id1] += w;//點權增加w 
		weight[id2] += w;
		G[id1][id2] += w;//邊權增加w 
		G[id2][id1] += w; 
	}
	DFSTrave();//遍歷整個圖的所有連通塊,獲取Gang的信息
	cout<<Gang.size()<<endl;
	for(map<string,int>::iterator it = Gang.begin();it!=Gang.end();it++)
		cout<<it->first<<" "<<it->second<<endl; 
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章