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 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 <map>
 3 #include <iostream>
 4 #include <string>
 5 using namespace std;
 6 #define MAX 2010
 7 //#define INF 0x3fffffff
 8 int G[MAX][MAX]={
 9     0
10 };
11 bool visited[MAX]={
12     false
13 };
14 int weight[MAX]={
15     0
16 };
17 map<string,int>str2num,gang;
18 map<int,string>num2str;
19 int numstr=0;
20 int totalvalue,number,head;
21 int N,K;
22 int Change(string str)
23 {
24     if(str2num.find(str)!=str2num.end())
25        return str2num[str];
26     else
27     {
28         num2str[numstr]=str;
29         str2num[str]=numstr;
30         return numstr++;
31     }
32 }
33 void DFS(int index)
34 {
35     visited[index]=true;
36     number++;
37     if(weight[index]>weight[head])
38     {
39         head=index;
40     }
41     for(int i=0;i<numstr;i++)
42     {
43         if(G[index][i]>0)
44         {
45             totalvalue+=G[index][i];
46             G[index][i]=G[i][index]=0;
47             if(!visited[i])
48               DFS(i);
49         }
50     }
51 }
52 void DFSTraverse()
53 {
54     for(int i=0;i<numstr;i++)
55     {
56         if(!visited[i])
57         {
58             totalvalue=0;
59             number=0;
60             head=i;
61             DFS(i);
62             if(number>2&&totalvalue>K)
63             {
64                 gang[num2str[head]]=number;
65             }
66         }
67     }
68 }
69 int main()
70 {
71
72     cin>>N>>K;
73     for(int i=0;i<N;i++)
74     {
75         string str1,str2;
76         int length;
77         int id1,id2;
78         cin>>str1>>str2>>length;
79         id1=Change(str1);
80         id2=Change(str2);
81         G[id1][id2]+=length;//注意。。。。
82         G[id2][id1]+=length;
83         weight[id1]+=length;
84         weight[id2]+=length;
85     }
86     DFSTraverse();
87     cout<<gang.size()<<endl;
88     map<string,int>::iterator it;
89     for(it=gang.begin();it!=gang.end();it++)
90        cout<<it->first<<" "<<it->second<<endl;
91
92 }

				
时间: 2024-10-11 18:10:53

PAT1034. Head of a Gang的相关文章

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)

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

【并查集】BZOJ1370- [Baltic2003]Gang团伙

[题目大意] 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: 所有是朋友的人组成一个团伙.告诉你关于这n个人的m条信息,即某两个人是朋友,或者某两个人是敌人,请你编写一个程序,计算出这个城市最多可能有多少个团伙? [思路] 水………NOIP的小孩都不屑于玩…… 把i拆成i和i+n,其中i表示i的朋友,i+n表示i的敌人.对于(i,j): (1)i,j是朋友,那么合并i和j. (2)i,j不是朋友,那么合并i和j+

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

Head of a Gang (map+邻接表+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 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

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