1034. Head of a Gang (30)

时间限制


100 ms


内存限制


65536 kB


代码长度限制


16000 B


判题程序


Standard


作者


CHEN, Yue


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

输入N K;N个被查的通信记录,成员的总联系时间K;


A B 这两个人的通信时间
……
成员两人以上且总联系时间大于K方成帮派,单人的总通信时间最多为头头
求出帮派个数和每个帮派头头和成员个数;
下面通过DFS深度优先搜索,求出几个连通图,并不断在每个连通图里面找到每个帮派的头头和帮派内的总联系时间(这里的总联系时间是先有重复累加的,所以后面判断要除以二)
用到了map 这个map就是可以用字符当下标用拉(map根据字典序下标[key]自动排好序。
map<string, vector<string>>Gmap; 类似与数组邻接表;第一个string对应可变动的数组vecotr[递增标号]=第二个string;



例子 有个 AA联系BB,AA联系CC,根据[AA].push_back(BB)  ;   [AA].push_back(CC) 的顺序  [AA][0]=BB;[AA][1]=CC;
如果不想用这个,可以自己给定义比如A对应0;z对应26然后AAA就是000000;


评测结果

时间

结果

得分

题目

语言

用时(ms)

内存(kB)

用户

8月04日

11:44

答案正确

​30​

​1034​

​C++ (g++ 4.7.2)​

6

896

​datrilla​

测试点

测试点

结果

用时(ms)

内存(kB)

得分/满分

0

答案正确

1

304

15/15

1

答案正确

1

308

2/2

2

答案正确

1

308

5/5

3

答案正确

6

896

2/2

4

答案正确

5

564

3/3

5

答案正确

5

768

3/3

#include<iostream>    
#include<vector>
#include<map>
#include<string>
using namespace std;
void Readln(map<string, vector<string>>*Gmap,
map<string, int>*EveryWeightTotal, map<string, bool>*ITbelongs, int N)
{
int Times;
string Name1, Name2;
while (N--)
{
cin >> Name1 >> Name2 >> Times;
ITbelongs->operator[](Name1) = true;
(*ITbelongs)[Name2] = true;
(*Gmap)[Name1].push_back(Name2);
(*Gmap)[Name2].push_back(Name1);
(*EveryWeightTotal)[Name2] += Times;
EveryWeightTotal->operator[](Name1) += Times;/*效果一样,只是换个形式看看,话说这个默认初始化是0的样子*/
}
}
void HGDFS(map<string, vector<string>>* Gmap,
map<string, int>*EveryWeightTotal, map<string, bool>*ITbelongs, string*nowHead, string newnext, int*ccount, int*nowTotal)
{
(*ITbelongs)[newnext] = false;
for (vector<string>::iterator iter = (*Gmap)[newnext].begin(); iter != (*Gmap)[newnext].end(); iter++)/*这些人属于同一个帮派;屏幕一个脏东西还一直以为这里形式有错!!*/
{
if ((*ITbelongs)[(*iter)])/*这个人(*iter)还未检查过 */
{
(*ccount)++;/*新增检查一人(*iter)*/
(*nowTotal) += (*EveryWeightTotal)[(*iter)];/*总联系增加*/
if ((*EveryWeightTotal)[(*nowHead)] < (*EveryWeightTotal)[(*iter)])/*这个人(*iter)的总Times最多,级别比较高可能是头头*/
(*nowHead) = (*iter);
HGDFS(Gmap, EveryWeightTotal, ITbelongs, nowHead, (*iter), ccount, nowTotal);
}
}
}
int main()
{
map<string, vector<string>>Gmap;/*作用类似于数组邻接表或者第一个string对应多个第二个string可变动长度数组*/
map<string, int>EveryWeightTotal;
map<string, bool>ITbelongs;
map<string, int>resultGangs;
int N, K,countt,nowTotal;
string nowHead;
cin >> N >> K;
Readln(&Gmap, &EveryWeightTotal, &ITbelongs, N);
for (map<string, bool>::iterator YouBiao = ITbelongs.begin(); YouBiao != ITbelongs.end(); YouBiao++)
{
if (YouBiao->second)
{
nowHead = YouBiao->first;
countt = 1;
nowTotal= EveryWeightTotal[YouBiao->first];
HGDFS(&Gmap, &EveryWeightTotal, &ITbelongs, &nowHead, nowHead, &countt, &nowTotal);
if (2 < countt&&nowTotal / 2 > K)/*两人以上且关系大于K方成帮派,这里的关系重复算了一次*/
{
resultGangs[nowHead] = countt;
}

}
}
cout << resultGangs.size() << endl;
for (map<string, int>::iterator iter = resultGangs.begin(); iter != resultGangs.end(); iter++)
{
cout << iter->first <<" "<<iter->second<< endl;
}
system("pause");
return 0;
}