A1034. 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 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
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <string>
 8 #include <stack>
 9 #include <queue>
10 #include <vector>
11 #include <map>
12 using namespace std;
13 const int maxn=2010;
14 const int INF=1000000000;
15
16 map<string,int> sToi;
17 map<int,string> iTos;
18 map<string,int> gang;//按字母顺序排序
19
20 int G[maxn][maxn]={0},weight[maxn]={0};
21 int n,k,numPerson;
22 bool vis[maxn]={false};
23
24 int change(string s)
25 {
26     if(sToi.find(s)!=sToi.end())
27     {
28         return sToi[s];
29     }else
30     {
31         sToi[s]=numPerson;
32         iTos[numPerson]=s;
33         return numPerson++;
34     }
35 }
36 void DFS(int now,int &head,int &numM,int &totalv)
37 {
38     numM++;
39     vis[now]=true;
40     if(weight[now]>weight[head])
41     {
42         head=now;
43     }
44     for(int i=0;i<numPerson;i++)
45     {
46         if(G[now][i]>0)
47         {
48             totalv+=G[now][i];
49             G[now][i]=G[i][now]=0;
50             if(vis[i]==false)
51             {
52                 DFS(i,head,numM,totalv);
53             }
54         }
55     }
56 }
57 void DFSTra()
58 {
59   for(int i=0;i<numPerson;i++)
60   {
61    if(vis[i]==false)
62    {
63     int head=i,numMember=0,totalvalue=0;
64     DFS(i,head,numMember,totalvalue);
65     if(totalvalue>k&&numMember>2)
66     {
67         gang[iTos[head]]=numMember;
68     }
69
70    }
71   }
72 }
73
74 int main(){
75   string s1,s2;
76   cin>>n>>k;
77   for(int i=0;i<n;i++)
78   {
79       int w;
80       cin>>s1>>s2>>w;
81       //将s1 s2转换成数字
82     int id1=change(s1);
83     int id2=change(s2);
84      G[id1][id2]+=w;//這裡必須是+=,否则不能输入重复边,导致样                      //例不过
85      G[id2][id1]+=w;
86      weight[id1]+=w;
87      weight[id2]+=w;
88   }
89   DFSTra();
90   printf("%d\n",gang.size());
91   map<string,int>::iterator it;
92   for(it=gang.begin();it!=gang.end();it++)
93   {
94       cout<<it->first<<" "<<it->second<<endl;
95   }
96   return 0;
97 }
时间: 2024-10-12 08:49:54

A1034. Head of a Gang (30)的相关文章

A1034 Head of a Gang (30分)

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

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.

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

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

A1034. 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 b

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

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

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