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.
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.
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.
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
2
AAA 3
GGG 3
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
0
这一题花了六个小时去解,最后仍是参考其他的代码
先看目的:找出头目 找出符合要求的连通分量,统计连通分量数目,统计连通分量成员数目
变量设置:头目 设置为head 连通分量中成员的数目num_all,连通分量的边权和 weight_all
求解办法:dfs遍历这个图
相比别人的代码要修正的地方(以我自己第一二版代码为例,第一二版分别是2分与4分)
//问题很多,最关键的是题目没有读懂,一个连通分量是帮派的条件是边权之和大于K
//问题1:这里的做法是不正确的,虽然G[a][b] 和G[b][a]是不一样的,但为了求所有通话时间,应G[a][b] G[b][a]的通话时间求和,作为(a,b)边的真正权值,陷入了思维定势 G[a][b]=G[b][a] =l
//问题2:字符串和下标的对应,本来想耍个小聪明,用字符串首位字母-'A' 作为字符串的下标,其实麻烦了,直接设置两个map用来字符串和下标,下标和字符串的对应。有时侯一个问题偷懒了别的就会成倍复杂
//问题3: 连通分量的数目,这里按照算法笔记的做法,先在dfstrav()函数中对图进行深度搜索,如果在从一个点 i 深度搜索图后仍发现有结点未被访问,就使得cnt加1.但更方便的做法是由输出结果的性质入手,将每次搜索得到的头目信息存放在map中,这样连通分量的数目就是这个map的数目,是 顺手的事情
//问题4:小细节,在dfs中如果想让全局变量进入到参数中,修改为dfs(int &head)
//还有一个未弄懂的在通过的代码上说
#include
#define maxn 2500
using namespace std;
int N;
int K;
bool visited[maxn];
int G[maxn][maxn];
int w[maxn];
int weight_all = 0;
int number_all = 0;
int max_t = 0;
int max_index = 0;
map m;
void dfs(int u)
{
visited[u] = true;
if(w[u]>max_t)
{
max_t = w[u];
max_index = u;
}
weight_all+=w[u];
number_all++;
for(int i = 0;i2&&(weight_all/2)>K)
{
cnt++; // 问题3
//cout<>N>>K;
fill(visited,visited+maxn,false);
fill(G[0],G[0]+maxn*maxn,0);
fill(w,w+maxn,0);
string n_a,n_b;
int t;
for(int i = 0;i>n_a>>n_b>>t;
int a = (int)(n_a[0]-'A'); //问题2
int b = (int)(n_b[0]-'A');
G[a][b] = t; // 问题1
w[a]+=t;
w[b]+=t;
}
dfstra();
for(auto it = m.begin();it!=m.end();it++)
{
cout<first<<" "<second<
通过的代码:
// 问题5 把和现在点相关的点的权值全部相加求和,下一行的G[u][i] = G[i][u] = 0防止回头
#include
#define maxn 2500
using namespace std;
int N,K;
int num = 0; //代表的是每个帮派的人数
map int_to_string;
map string_to_int;
map gang;
bool visited[maxn] = {false};
int G[maxn][maxn];
int w[maxn] = {0};
int head = 0,num_all = 0,weight_all = 0;
// change 返回姓名str对应的编号,这里是这题没有设计好的
int change(string a)
{
if(string_to_int.find(a)!=string_to_int.end())
{
return string_to_int[a]; // 返回编号
}
else
{
string_to_int[a] = num;
int_to_string[num] = a;
return num++;
}
}
void dfs(int u,int& head,int& num_all,int& weight_all)
{
num_all++;
visited[u] = true;
if(w[u]>w[head])
{
head = u;
}
for(int i = 0;i0)
{
weight_all+=G[u][i]; // 问题5
G[u][i] = G[i][u] = 0;
if(visited[i]==false)
{
dfs(i,head,num_all,weight_all);
}
}
}
}
void dfstra()
{
for(int i = 0;i2&&weight_all>K)
{
gang[int_to_string[head]] = num_all;
}
}
}
}
int main()
{
fill(G[0],G[0]+maxn*maxn,0);
cin>>N>>K;
string n_a,n_b;
int t;
for(int i = 0;i>n_a>>n_b>>t;
int a = change(n_a);
int b = change(n_b);
G[a][b] +=t;
G[b][a] +=t;
w[a]+=t;
w[b]+=t;
}
dfstra();
cout<first<<" "<second<