【九度】题目1446:Head of a Gang 怎么老是Wrong Answer ??

[复制链接]
查看11 | 回复0 | 2021-1-27 06:26:13 | 显示全部楼层 |阅读模式
题目描述:
Onewaythatthepolicefindstheheadofagangistocheckpeople'sphonecalls.IfthereisaphonecallbetweenAandB,wesaythatAandBisrelated.Theweightofarelationisdefinedtobethetotaltimelengthofallthephonecallsmadebetweenthetwopersons.A"Gang"isaclusterofmorethan2personswhoarerelatedtoeachotherwithtotalrelationweightbeinggreaterthanagiventhretholdK.Ineachgang,theonewithmaximumtotalweightisthehead.Nowgivenalistofphonecalls,youaresupposedtofindthegangsandtheheads.
输入:
Foreachcase,thefirstlinecontainstwopositivenumbersNandK(bothlessthanorequalto1000),thenumberofphonecallsandtheweightthrethold,respectively.ThenNlinesfollow,eachinthefollowingformat:
Name1Name2Time
whereName1andName2arethenamesofpeopleatthetwoendsofthecall,andTimeisthelengthofthecall.AnameisastringofthreecapitalletterschosenfromA-Z.Atimelengthisapositiveintegerwhichisnomorethan1000minutes.
输出:
Foreachtestcase,firstprintinalinethetotalnumberofgangs.Thenforeachgang,printinalinethenameoftheheadandthetotalnumberofthemembers.Itisguaranteedthattheheadisuniqueforeachgang.Theoutputmustbesortedaccordingtothealphabeticalorderofthenamesoftheheads.
样例输入:
859
AAABBB10
BBBAAA20
AAACCC40
DDDEEE5
EEEDDD70
FFFGGG30
GGGHHH20
HHHFFF10
870
AAABBB10
BBBAAA20
AAACCC40
DDDEEE5
EEEDDD70
FFFGGG30
GGGHHH20
HHHFFF10
样例输出:
2
AAA3
GGG3
0
在PAT上测试为“段错误”
我把递归都换成普通循环了,还是提示段错误?
求大神帮忙看看哪里可能出现段错误?

这是PAT测试截图,九度上则一直显示是“WrongAnswer”



#include
#include
#include
#include
#include
#include
usingnamespacestd;
#defineMaxSize1010
typedefstructNode{
charname[4];
inttime;
intparent;
}Node;
typedefstructHead{
charname[4];
intnumb;
}Head;
intcompare1(constvoid*a,constvoid*b)
{
Node*p=(Node*)a;
Node*q=(Node*)b;
//如果指向同一个根节点,则按照通话时长大小排序
if(p->parent==q->parent)return(p->timetime)?1:-1;
//不指向同一个根节点,按根节点大小排序
return(p->parent>q->parent)?1:-1;
}
intcompare2(constvoid*a,constvoid*b)
{
Head*p=(Head*)a;
Head*q=(Head*)b;
returnstrcmp(p->name,q->name);
}
intFind(intloc);//寻找根,用递归使最终所有的节点都指向根
voidUnion(intfirst,intsecond);//合并集合
//全局变量
Nodecall[MaxSize];//存电话单
Headgang[MaxSize];//存头目
intmain()
{
intcount,kval;
charname1[4],name2[4];
inttime;//每个人的单独通话时长
while(scanf("%d%d",&count,&kval)!=EOF)
{
//初始化,让每个节点单独成为一个集合,单人通话时长初始化为0
for(intindex=0;index2&&sumtime>kval)
{
gang[j].numb=numb;
strcpy(gang[j].name,call[loca].name);//保存头目名字
++j;
}
loca=i;
}
qsort(gang,j,sizeof(Head),compare2);//结果按字母排序
printf("%d\n",j);
for(inti=0;icall[root2].time)call[root2].parent=root1;
elsecall[root1].parent=root2;
}

分 -->
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行