您的位置:首页 > 其它

【PAT】【Advanced Level】1034. Head of a Gang (30)

2017-07-23 15:28 399 查看


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


原题链接:

https://www.patest.cn/contests/pat-a-practise/1034

思路:

DFS

CODE:

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#define N 2001
using namespace std;

int n,mc;

typedef struct S
{
string name;
int time;
};
typedef struct S1
{
string name;
int pn;
};
S node
;
S1 dg
;

bool able

;
bool fl
;
int num=1;

int mt=0;
string boss;
int rs=0;
int maxt=-1;
map<string,int> ma;

void dfs(int now)
{
//cout<<node[now].name<<endl;
if (node[now].time>maxt)
{
maxt=node[now].time;
boss=node[now].name;
}
mt+=node[now].time;
rs++;
for (int i=1;i<num;i++)
{
if (fl[i]==1) continue;
if (able[now][i]==0) continue;
fl[i]=1;
dfs(i);
}
}
bool cmp(S1 a, S1 b)
{
return a.name<b.name;
}
int main()
{
memset(able,0,sizeof(able));
memset(fl,0,sizeof(fl));
cin>>n>>mc;

for (int i=1;i<=n;i++)
{
string a,b;
int c;
cin>>a>>b>>c;
if (ma[a]==0)
{
ma[a]=num;
node[num].name=a;
node[num].time=c;
num++;
}
else
node[ma[a]].time+=c;
if (ma[b]==0)
{
ma[b]=num;
node[num].name=b;
node[num].time=c;
num++;
}
else
node[ma[b]].time+=c;
able[ma[a]][ma[b]]=able[ma[b]][ma[a]]=1;
//cout<<ma[a]<<" "<<ma[b]<<endl;
}
int sum=0;

for (int i=1;i<num;i++)
{
if (fl[i]==0)
{
rs=0;
mt=0;
maxt=-1;
fl[i]=1;
dfs(i);
mt/=2;
//cout<<rs<<" "<<mt<<endl;
if (rs>2&&mt>mc)
{
dg[sum].name=boss;
dg[sum].pn=rs;
sum++;
}
//cout<<endl;
}
}
sort(dg,dg+sum,cmp);
cout<<sum<<endl;
for (int i=0;i<sum;i++)
cout<<dg[i].name<<" "<<dg[i].pn<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: