题目描述
One way that the police finds the head of a gang is to checkpeople's
phone calls. If there isa phone call between A and B, we say that A
and B is related. Theweight of a relation is defined to be the total
time length of all the phone calls made between the twopersons. A
"Gang" is a cluster of more than 2 persons who arerelated to
each other with total relation weight being greater than a given
threshold K. In eachgang, the one with maximum total weight is the
head. Now given a list ofphone calls, you are supposed to find the
gangs and the heads.
输入描述:
Each input file contains one test case. For each case, the first line contains twopositive numbers N and K (both less than or equal to 1000), the number of phonecalls 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, andTime is the length of the call. A nameis a string of three capital letters chosen from A-Z. A time length is a positive integer which isno more than 1000 minutes.
输出描述:
For each test case, first print in a line the total number ofgangs. Then for each gang, print in aline the name of the head and the total number of the members. It is guaranteed that the head is unique foreach gang. The output must be sortedaccording to the alphabetical order of the names of the heads.
输入例子:
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
输出例子:
2
AAA 3
GGG 3
这是一道最大强联通分量的题,按照求最大强联通分量的传统方法就好了,这一题有一个陷阱,就是同一条边可能出现多次,当同一条边出现多次的时候,需要累加
这一题在求最大强联通分量的基础上加了很多的限制条件,比如对于某个强联通分量而言,当其中的节点个数超过2并且边上的权重之和超过K的时候这个强联通分量才算在最终的结果里面,并且最终的结果需要按照头目名字的字典序排序
这一题还有一点就是节点不是按照ID给出的,而是按照字符串给出的,但是以字符串为索引进行深度优先遍历很耗时间,所以使用两个map,一个用来把string转化成id,一个用来把id装换成string,这两个map还能够起到统计人数的作用
通过这一道题,练习了强联通分量算法的流程,以及大规模代码的处理能力
以后在使用time,theGroup等敏感字符作为变量名的时候,需要在前面加上_或者是j_,保证其不和系统变量重名
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <memory.h>
using namespace std;
struct _theGroup
{
int peopleNum;
int phoneCount;
string headName;
int headPhones;
_theGroup(int a=0,int b=0,string c="",int d=0) :peopleNum(a),phoneCount(b),headName(c),headPhones(d){}
};
int ID = 0;
map<string, int> nameToId;
map<int, string> iDtoName;
int theEdges[1002][1002] = { 0 };
int theStack[1002];
bool vst[1002] ;
int low[1002];
int vstTime[1002] = { 0 };
int _time = 1;
int stackP = 0;
int groupCount = 0;
int theGroupBelong[102];
int peopleCount;
void P(int t)
{
vstTime[t] = low[t] = _time++;
theStack[stackP++] = t;
vst[t] = true;
for (int i = 0; i < peopleCount; i++)
{
if (theEdges[t][i] != 0)
{
if (vst[i])
{
if (low[i] < low[t]) low[t] = low[i];
}
else
{
if(vstTime[i] == 0)
{
P(i);
if (low[i] < low[t]) low[t] = low[i];
}
}
}
}
int u;
if (low[t] == vstTime[t])
{
do
{
u = theStack[--stackP];
theGroupBelong[u] = groupCount;
} while (u != t);
groupCount++;
}
}
int main()
{
int N, K;
peopleCount = 0;
string a, b;
int c;
int idA, idB;
cin >> N >> K;
for (int i = 0; i < N; i++)
{
cin >> a >> b >> c;
if(nameToId.count(a))
{
idA = nameToId[a];
}
else
{
idA = ID++;
nameToId.insert(make_pair(a, idA));
iDtoName.insert(make_pair(idA, a));
}
if (nameToId.count(b))
{
idB = nameToId[b];
}
else
{
idB = ID++;
nameToId.insert(make_pair(b, idB));
iDtoName.insert(make_pair(idB, b));
}
theEdges[idA][idB] += c;
theEdges[idB][idA] += c;
}
peopleCount = ID;
for (int i = 0; i < peopleCount; i++)
{
if (vstTime[i] == 0)
{
memset(vst, false, sizeof(vst));
P(i);
}
}
int theGangCount = 0;
map<string, int> theGangInfo;
vector<_theGroup> theGroups(groupCount);
int p = 0;
for (int i = 0; i < peopleCount; i++)
{
int otherPhones=0, thePhones=0;
for (int j = 0; j < i; j++)
{
if (theEdges[i][j] != 0)
{
otherPhones += theEdges[i][j];
}
}
for (int j = i; j < peopleCount; j++)
{
if (theEdges[i][j] != 0)
{
thePhones += theEdges[i][j];
}
}
theGroups[theGroupBelong[i]].phoneCount += thePhones;
if (theGroups[theGroupBelong[i]].headPhones < thePhones + otherPhones)
{
theGroups[theGroupBelong[i]].headName = iDtoName[i];
theGroups[theGroupBelong[i]].headPhones = thePhones + otherPhones;
}
theGroups[theGroupBelong[i]].peopleNum++;
}
for (int i = 0; i < groupCount; i++)
{
if (theGroups[i].peopleNum > 2 && theGroups[i].phoneCount > K)
{
theGangCount++;
theGangInfo.insert(make_pair(theGroups[i].headName, theGroups[i].peopleNum));
}
}
cout << theGangCount << endl;
map<string, int> ::iterator it = theGangInfo.begin();
for (; it != theGangInfo.end(); it++)
{
cout << it->first<<" "<<it->second<<endl;
}
return 0;
}