1034 Head of a Gang (30 分)

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

 题意:若干人的通话时间视为无向边,将这些结点分为不同的连通块,每个结点的点权为该结点所参于的总的通话时长,而如果每个连通块的的总通话时长大于k并且结点个数大于2个,则这个连通块为一个gang,输出gang的个数和其中的人数和head(head为该连通块中点权最大的点),然后按每个gang中的head的字母顺序升序排列。(map会自动按t1的顺序从小到大排序)。

思路:1首先解决名字与编号的对应关系。使用map。

2获得每个人的点权,在读入时处理。

3进行图的遍历。dfs遍历每个连通块,获得连通块的总通话时长,节点个数,和head。

4遍历每个连通块,找到gang。

细节:1通话记录最多为1000条,人数最多为2000人,所有maxn为2000以上。

2每个结点访问后不应该再次访问,但图中可能有环,即遍历过程中发生连接已访问点的情况。图FFF,GGG,HHH为一个环,累加完边FFF->GGG的比边权后,访问GGG时又会累加边GGG->FFF的边权,故要删去避免走回头路。

#include 
#include 
#include 
#include 
using namespace std;
const int maxn=2019;//总人数 
const int INF=999999999;
map stringToInt;//姓名->编号 
map intToString;//编号->姓名 
map ans;//head->人数 

int G[maxn][maxn]={0},weight[maxn]={0},vnum,n,k;//vnum为总人数 
bool vis[maxn]={false};

void dfs(int now,int &head,int &num,int &totalvalue)//求连通块的总权值,头目,人数 
{
	num++;//连通块人数加1 
	if(weight[now]>weight[head]){//如果该结点的点权大于头结点 
		head=now;
	}
	vis[now]=true;
	for(int i=0;i2&&totalvalue>k){//成员数大于2且总边权大于k
				ans[intToString[head]]=num;
			}
		}
	}
}

int change(string str)//返回姓名所对应的编号
{
	if(stringToInt.find(str)!=stringToInt.end()){//如果这个人已经出现过了 
		return stringToInt[str];//返回编号
	}
	else{
		stringToInt[str]=vnum;
		intToString[vnum]=str;
		return vnum++;
	}
}


int main()
{
	int w;
	string str1,str2;
	scanf("%d %d",&n,&k);
	for(int i=0;i>str1>>str2>>w;
		int id1=change(str1);
		int id2=change(str2);
		G[id1][id2]+=w;
		G[id2][id1]+=w;
		weight[id1]+=w;
		weight[id2]+=w;
	}
	dfstravel();
	cout<first<<" "<second<

 

你可能感兴趣的:(图的遍历)