A1034 Head of a Gang (30 point(s))

DFS求强连通子图的权重和和顶点数

1. 原文

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

1
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:

1
2
3
4
5
6
7
8
9
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:

1
2
3
2
AAA 3
GGG 3

Sample Input 2:

1
2
3
4
5
6
7
8
9
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:

1
0

2. 解析

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<cstdio>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
const int maxn=1010*2;

int graph[maxn][maxn];
int weigth[maxn]={};
bool visit[maxn]={false};
map<string,int> strtoint,ans;
map<int,string> intostr;

int idx=1;
int convert(char c[])
{
string str=c;
if (strtoint[str]>0)
{
return strtoint[str];
}else{
strtoint[str]=idx;
intostr[idx]=str;
return idx++;
}
}
int n,k;
void dfs(int root,int &head,int &sum,int &cnt){
//printf("%d %d %d %d\n", root,head,sum,cnt);
visit[root]=true;
cnt++;
if (weigth[root]>weigth[head])
{
head=root;
}
for (int i = 1; i <= n; ++i)
{
if (graph[root][i]!=0)
{
sum+=graph[root][i];
graph[root][i]=graph[i][root]=0;
if (visit[i]==false)
{
dfs(i,head,sum,cnt);
}
}
}


}
char str1[4],str2[4];
int main()
{
fill(graph[0],graph[0]+maxn*maxn,0);
scanf("%d%d",&n,&k);
for (int i = 0; i < n; ++i)
{
scanf("%s%s",str1,str2);
int a=convert(str1);
int b=convert(str2);
int c;
scanf("%d",&c);
graph[a][b]+=c;
graph[b][a]+=c;
weigth[a]+=c;
weigth[b]+=c;
}
for (int i = 1; i <= n; ++i)
{
int head=i;
int sum=0;
int cnt=0;
dfs(i,head,sum,cnt);
if (cnt>2&&sum>k)
{
ans[intostr[head]]=cnt;
}
}
printf("%lu\n", ans.size());
for (map<string,int>::iterator i = ans.begin();
i != ans.end(); ++i)
{
printf("%s %d\n", i->first.c_str(),i->second);
}

return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1034/
  • 发布时间: 2019-09-03 12:34
  • 更新时间: 2021-10-29 14:00
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!