您的位置:首页 > 其它

Electrical Outlets 利用树的性质解题

2009-05-01 23:06 106 查看
题目在下面:

先推导树的一个性质

一棵树的叶子节点数目+中间节点数目=分支数目+1;

每个分支必定指向一个节点,不管是叶子还是中间的节点;即除根节点之外,每个节点都有一个分支指向它,可得上述公式。

http://acm.pku.edu.cn/JudgeOnline/problem?id=2636

Description


Royhasjustmovedintoanewapartment.Well,actuallytheapartmentitselfisnotverynew,evendatingbacktothedaysbeforepeoplehadelectricityintheirhouses.Becauseofthis,Roy'sapartmenthasonlyonesinglewalloutlet,soRoycanonlypoweroneofhiselectricalappliancesatatime.
RoylikestowatchTVasheworksonhiscomputer,andtolistentohisHiFisystem(onhighvolume)whilehevacuums,sousingjustthesingleoutletisnotanoption.Actually,hewantstohaveallhisappliancesconnectedtoapoweredoutlet,allthetime.Theanswer,ofcourse,ispowerstrips,andRoyhassomeoldonesthatheusedinhisoldapartment.However,thatapartmenthadmanymorewalloutlets,soheisnotsurewhetherhispowerstripswillprovidehimwithenoughoutletsnow.
YourtaskistohelpRoycomputehowmanyapplianceshecanprovidewithelectricity,givenasetofpowerstrips.Notethatwithoutanypowerstrips,Roycanpoweronesingleappliancethroughthewalloutlet.Also,rememberthatapowerstriphastobepowereditselftobeofanyuse.
Input
Inputwillstartwithasingleinteger1<=N<=20,indicatingthenumberoftestcasestofollow.ThenfollowNlines,eachdescribingatestcase.Eachtestcasestartswithaninteger1<=K<=10,indicatingthenumberofpowerstripsinthetestcase.Thenfollow,onthesameline,Kintegersseparatedbysinglespaces,O1O2...OK,where2<=Oi<=10,indicatingthenumberofoutletsineachpowerstrip.
Output
Outputonelinepertestcase,withthemaximumnumberofappliancesthatcanbepowered.
SampleInput
3
3234
104444444444
410101010

SampleOutput
7
31
37


C源码

#include<stdio.h>

intmain()
{
intN=0,k=0,sum=0,out=0,i=0,j=0;

scanf("%d",&N);
for(i=0;i<N;i++){
sum=0;
scanf("%d",&k);
for(j=0;j<k;j++){
scanf("%d",&out);
sum+=out;
}
printf("%d/n",sum+1-k);
}

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