您的位置:首页 > 其它

PAT - 甲级 - 1034. Head of a Gang (30)(DFS求连通块)

2017-11-06 13:06 537 查看
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.N条通话记录

2.称之为“团伙”的通话总和最小值K

题目要求:

1.有几个团伙

2.每个团伙的“头目”是谁

3.每个团伙的人数是多少

解答:

1.dfs求图的连通分量,连通分量数即团伙数目

2.每个连通分量中通话时长最多的就是头目

3.每个连通分量的节点数即是团伙人数

限制条件:

1.连通分量各节点的通话总时长超过k,并且节点数超过2,才算是团伙

需要注意:

1.给定的N是“边”的数量而不是“点”的数量

2.在计算通话总和的时候要考虑到“环”的情况

#include<iostream>
#include<cstdio>
#include<string>
#include<map>
using namespace std;

int vis[2010], w[2010];
int e[2010][2010];
map<string, int> strToint;
map<int, string> intTostr;
map<string, int> ans;
int id = 1;

int getInt(string str){
if(strToint[str] == 0){
strToint[str] = id;
return id++;
}else{
return strToint[str];
}
}

void dfs(int &head, int i, int &totnum, int &totwei){
vis[i] = 1;
totnum += 1;
if(w[i] > w[head]){
head = i;
}
for(int j = 1; j < id; j++){
if(e[i][j]){
totwei += e[i][j];
e[i][j] = e[j][i] = 0;
if(!vis[j]){
dfs(head, j, totnum, totwei);
}
}
}
}

int N, K, t;
string s1, s2;
int main(){
freopen("input.txt", "r", stdin);
while(scanf("%d%d", &N,&K) != EOF){
// init
fill(vis, vis+2010, 0);
fill(e[0], e[0]+2010, 0);
fill(w, w+2010, 0);
// input
for(int i = 0; i < N; i++){
cin>>s1>>s2>>t;
int a = getInt(s1);
int b = getInt(s2);
intTostr[a] = s1;
intTostr[b] = s2;
w[a] += t;
w[b] += t;
e[a][b] += t;
e[b][a] += t;
}
//
for(int i = 1; i < id; i++){
if(!vis[i]){
int totnum = 0, totwei = 0, head = i;
dfs(head, i, totnum, totwei);
if(totnum > 2 && totwei > K){
ans[intTostr[head]] = totnum;
}
}
}
cout<<ans.size()<<endl;
for(map<string, int>::iterator it = ans.begin(); it != ans.end(); it++){
cout<<it->first<<" "<<it->second<<endl;
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: