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. 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<cstdio>
 2 #include<stack>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 #include<vector>
 9 using namespace std;
10 map<string,vector<string> > edge;
11 map<string,int> pv;
12 map<string,bool> vis;
13 map<string,int> output;
14 void DFS(string s,string &maxper,int &pernum,int &total){
15     vis[s]=true;
16     pernum++;
17     total+=pv[s];
18     if(pv[s]>pv[maxper]){
19         maxper=s;
20     }
21     vector<string>::iterator it;
22     for(it=edge[s].begin();it!=edge[s].end();it++){
23         if(!vis[*it]){
24             DFS(*it,maxper,pernum,total);
25         }
26     }
27 }
28 int main(){
29     //freopen("D:\\INPUT.txt","r",stdin);
30     int n,k;
31     scanf("%d %d",&n,&k);
32     int i,v;
33     string name1,name2;
34     for(i=0;i<n;i++){
35         cin>>name1>>name2;
36         scanf("%d",&v);
37         edge[name1].push_back(name2);
38         pv[name1]+=v;
39         vis[name1]=false;
40         edge[name2].push_back(name1);
41         pv[name2]+=v;
42         vis[name2]=false;
43     }
44     map<string,vector<string> >::iterator it;
45     int count=0;
46     string maxper;
47     int pernum,total;
48     for(it=edge.begin();it!=edge.end();it++){
49         if(!vis[it->first]){
50             maxper=it->first;
51             pernum=0;
52             total=0;
53             DFS(it->first,maxper,pernum,total);
54             if(pernum>2&&total*1.0/2>k){
55                 output[maxper]=pernum;
56                 count++;
57             }
58         }
59     }
60     map<string,int>::iterator itt;
61     if(count){
62         printf("%d\n",count);
63         for(itt=output.begin();itt!=output.end();itt++){
64         cout<<itt->first;
65         printf(" %d\n",itt->second);
66         }
67     }
68     else{
69         printf("0\n");
70     }
71     return 0;
72 }
时间: 2024-10-14 16:36:38

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

PAT-1034 Head of a Gang (30)

这道题目刚开始想用并查集来做,但是其实不通的,因为有可能是环,不确保是一个图. 这道题目第一想法应该是用DFS来做,BFS也行,不过用DFS习惯了. // 1034.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<stdio.h> #include<string.h> #include<string>//string 使用c++的string,如果使用c语言的string,那么就会导致map声

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

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 b

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

PAT1034. 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

A1034 Head of a Gang (30分)

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