PAT 1034 Head of a Gang (连通块)

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.

思路

给定一个图,求每个连通块中路径和大于K的、节点个数大于2的那些联通块,要求输出该块中节点的个数,以及该块中的老大(老大是相邻节点路径和最大的点)。

一开始数组开的小,1500*1500都没有过,开到2100 * 2100才过。

试了下平台开二维数组的极限,反正20000 * 20000是可以的(这应该超了64MB了吧)。

#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <stack>
#include <functional>
#include <limits.h> 
using namespace std;
struct node{
	int num;
	string id;
	node(){	}
	node(int _num, string _id){
		num = _num;
		id = _id; 
	}
	bool operator< (node b) const{
		return id < b.id;
	}
};
vector<node> myv; //记录每个答案 
int maze[2100][2100];
map<string, int> mmp1;// 字符串到编号 
map<int, string> mmp2;// 编号到字符串 
int N, K, pos; 
bool visit[2100];
int maxt = -1, maxi = -1; //用于求连通块中的老大 
int num = 0;
int dfs(int index){
	num++;
	int sum_me = 0, sum = 0;
	for(int i = 0; i < pos; i++){
		if(maze[index][i] && visit[i] == false){
			visit[i] = true;
			sum += dfs(i);
		}
		sum_me += maze[index][i];
		sum_me += maze[i][index];
	}
	if(sum_me > maxt){
		maxt = sum_me;
		maxi = index;
	}
	return sum + sum_me;
}
int main() {
	scanf("%d%d", &N, &K);
	string _a = "", _b = "";
	int _c = 0;
	for(int i = 0; i < N;i++){
		cin >> _a >> _b >> _c;
		if(mmp1.count(_a) == 0){
			mmp1[_a] = pos;
			mmp2[pos++] = _a;
		}
		if(mmp1.count(_b) == 0){
			mmp1[_b] = pos;
			mmp2[pos++] = _b;
		}
		maze[mmp1[_a]][mmp1[_b]] = _c;
	}
	memset(visit, 0, sizeof(visit));
	for(int i = 0; i < pos; i++){
		maxi = -1, maxt = -1, num = 0;
		if(visit[i] == false){
			visit[i] = true;
			// 每条边求了两边 因此要除二 
			if(dfs(i) / 2 > K && num > 2){
				myv.push_back(node(num, mmp2[maxi]));
			}
		}
	}
	sort(myv.begin(), myv.end());
	cout << myv.size() << endl;
	for(int i = 0; i < myv.size(); i++){
		cout << myv[i].id << " " << myv[i].num << endl;
	}
	return 0;
}
posted @ 2020-03-11 22:52  阳离子  阅读(137)  评论(0编辑  收藏  举报