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 Aand 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

分析: 变量有点多。。得有耐心写

 1 /**
 2 * Copyright(c)
 3 * All rights reserved.
 4 * Author : Mered1th
 5 * Date : 2019-02-21-21.05.03
 6 * Description : A1034
 7 */
 8 #include<cstdio>
 9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std;
19 const int maxn=2010;
20 int G[maxn][maxn]={0},weight[maxn]={0};
21 bool vis[maxn]={false};
22 map<string,int> stringToInt;
23 map<int,string> intToString;
24 map<string,int> Gang; //Gang的人数
25 int numGang=0,numPerson=0;
26 int n,th;
27 int change(string str){
28     if(stringToInt.find(str)!=stringToInt.end()){
29         return stringToInt[str];
30     }
31     else{
32         stringToInt[str]=numPerson;
33         intToString[numPerson]=str;
34         return numPerson++;
35     }
36 }
37
38 void DFS(int index,int& head,int& numMember,int& totalValue){
39     numMember++;
40     vis[index]=true;
41     if(weight[index]>weight[head]){
42         head=index;
43     }
44     for(int i=0;i<numPerson;i++){
45         if(G[index][i]>0){
46             totalValue+=G[index][i];
47             G[index][i]=G[i][index]=0;  //遍历过后把该边删除
48             if(vis[i]==false) DFS(i,head,numMember,totalValue);
49         }
50     }
51 }
52
53 void DFSTravel(){
54     for(int i=0;i<numPerson;i++){
55         if(vis[i]==false){
56             int head=i,numMember=0,totalValue=0;
57             DFS(i, head, numMember, totalValue);
58             if(numMember > 2&& totalValue > th){
59                 Gang[intToString[head]]=numMember;
60             }
61         }
62     }
63 }
64
65 int main(){
66 #ifdef ONLINE_JUDGE
67 #else
68     freopen("1.txt", "r", stdin);
69 #endif
70     int w;
71     string str1,str2;
72     scanf("%d%d",&n,&th);
73     for(int i=0;i<n;i++){
74         cin>>str1>>str2>>w;
75         int id1=change(str1);
76         int id2=change(str2);
77         weight[id1]+=w;
78         weight[id2]+=w;
79         G[id1][id2]+=w;
80         G[id2][id1]+=w;
81     }
82     DFSTravel();
83     cout<<Gang.size()<<endl;
84     for(auto it =Gang.begin();it!=Gang.end();it++){
85         cout<<it->first<<" "<<it->second<<endl;
86     }
87     return 0;
88 }
 
 

原文地址:https://www.cnblogs.com/Mered1th/p/10415767.html

时间: 2024-10-10 21:18:44

1034 Head of a Gang (30 分)的相关文章

PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

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

1034 Head of a Gang (30分)

1. 题目 2. 思路 使用并查集归属集合 3. 注意点 可以使用map<string, string> 模拟int类型的并查集,减少了string和int类型转换的问题 因为c++的map,值如果不存在会自动初始化 map<string, string> father; //定义 // 查找root string findfather(string x){ if(father[x] == ""){ father[x] = x; }else{ while(fat

1034 Head of a Gang (30)(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 b

1034. Head of a Gang (30)

注意这里n是电话数,电话数不超过1000代表人数不超过两千,...好坑.... 时间限制 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

1034. Head of a Gang (30) -string离散化 -map应用 -并查集

题目例如以下: 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 call

PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

题目 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 mad

A1034 Head of a Gang (30分)

一.技术总结 这一题是关于图的遍历的,首先拿到题目理解题意,可以发现第一个需要考虑的问题是如何存储的问题. 然后就是考虑使用哪种遍历方法的问题,这里使用DFS遍历的方法. 然后还有就是如何存储字符串和编号的问题,使用map<string, int>,进行解决.最后就是关于统计每一个连通分量是否达标,一个是人数,一个是阀值.所以需要重新开辟一个空间来进行记录.也是使用map,用权重最大的字符串,进行对应的人数. 同时如果使用邻接矩阵,和一个bool数组用于记录结点是否被访问过是常规操作. 对于D

PAT (Advanced Level) 1034. Head of a Gang (30)

简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<string> #include<queue> #include<stack> #include<algorithm> #include<iostream> using namespace st

PAT 1034 Head of a Gang[难]

1034 Head of a Gang (30)(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 lengt

pat1034. Head of a Gang (30)

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.