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
SampleOutput
先推导树的一个性质
一棵树的叶子节点数目+中间节点数目=分支数目+1;
每个分支必定指向一个节点,不管是叶子还是中间的节点;即除根节点之外,每个节点都有一个分支指向它,可得上述公式。
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;
}
相关文章推荐
- codeforces 226 C 数论,利用Fibonacci数列的一个性质
- 利用这个 性质,随着父view 的变化而变化,
- MT【118】利用线面角最小解题
- 【寒江雪】利用异或性质实现双向链表
- HDU 5869 树状数组+GCD性质的利用
- BZOJ 1370 浅谈并查集性质利用
- 【解题报告】 URAL 1333 Genie Bomber 2 -- 利用类似统计的方法计算覆盖效率
- JZOJ4555【NOI2016模拟6.20】没有强联通分量的无聊世界 利用Dag性质的简单Dp
- 51nod 1116 K进制下的大数 利用同余式的乘法传递性质
- HDU 4818 RP problem (2013年长春现场赛F题) 高斯消元性质利用
- HDU Electrical Outlets
- 利用python闭包性质写只用函数来写一个类
- 利用动态规划解题思路
- 关于动态规划解题步骤和两个重要性质的理解---以最长递增子序列为例
- 将一棵二叉树的所有结点存储在一维数组中,虚结点用#表示,利用性质5
- Uva679--利用二叉树性质的有趣解法
- POJ 2491 Scavenger Hunt 解题报告 再次利用map判重
- BZOJ 4443 浅谈二分+二分图即四分图性质利用
- 滴滴笔试题,异或性质的利用
- 山东省第五届省赛 F - Full Binary Tree (利用满二叉树性质的一个思维题)