URAL 1822. Hugo II ' s WAR tree structure + dichotomy

Source: Internet
Author: User


1822 Hugo II ' s wartime limit:0.5 second
Memory limit:64 MB
The glorious King Hugo II had declared a war-a war that was holy, victorious, almost bloodless, but ruinous! Right after declaring the war the king had started summoning the army. He plans to send a recruitment order to all his immediate vassals, who would send the order to their vassals, and so on. In the end, every nobleman of the kingdom would be a involved in the preparation for the war. As soon as a nobleman who have no vassals receives the order, he immediately begins recruiting soldiers and joins his Overl Ord in a few days. If A nobleman has vassals, he waits until there is at least x% of his immediate vassals ready for the war, then summons his own troops and also joins his overlord. The glorious King Hugo II would go the war as soon as at least x% of his immediate vassals is ready. King Hugo II wants to state the number xIn his recruitment order. The king needs as many soldiers as possible for his military campaign. However, if the recruitment takes more than TDays, the enemy could learn about the imminent intrusion and strike first. Help Hugo II Find the maximal possible value of x. Inputthe First line contains the number Nof noblemen in the kingdom and the maximal number of days TThat can is spent for summoning the army (1≤ N≤104; 0≤ T≤106). The noblemen is numbered from 1 to N. King Hugo II has a number 1, and the noblemen with numbers from 2 to Nis described in the following N? 1 lines. The I-th of these lines describes the nobleman with number Iand contains integers pI and Ti , where pI Is the number of his overlord and Ti Is the number of the I-th nobleman would need to summon his troops (1≤ pI N; 0≤ Ti ≤100). Outputoutput the maximal possible value of xWith absolute error at most 10? 4. Sample
input Output
6 31 22 22 11 21 4
50.00000000




Test instructions

Enter n person, 3 is how much time to let the number 1 person convenor horse.

Then enter pi and Ti by the number 2 to N. The time required for that person to be the direct Lord and the person who summoned his part. Number 1 is the great Lord. Suppose that every Lord can only begin to recruit his own people by calling the people who are directly subordinate to the ≥x%. Ask x what is the maximum, and let the great Lord call his own men in the time of T to go to war.


Practice:

Two x to determine if the X can summon the men within the required time.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include < Iostream> #include <algorithm>using namespace std; #include <vector> long P[10010],t[10010];long long Nowt[10010];long long pai[100010];d ouble x;long long sum_t; Vector <long Long >son[10010];void dfs (Long long nod) {if (Son[nod].size () ==0) {Nowt[nod]=t[nod];return;} For (Long long i=0;i<son[nod].size (); i++) Dfs (Son[nod][i]); Long Long Need=ceil (1.0*son[nod].size () *x/100.0);//Find the minimum number of people required for (long long i=0;i<son[nod].size (); i++) pai[i]= Nowt[son[nod][i]]; Sort (pai,pai+son[nod].size ()); Long long ret;if (need==0) ret=0;elseret=pai[need-1]; The man who spends the longest time nowt[nod]=ret+t[nod]; Long long Deal () {DFS (1); if (nowt[1]>sum_t) return 0;elsereturn 1;} int main () {Long Long n;while (scanf ("%i64d%i64d", &n,&sum_t)!=eof) {for (Long long i=1;i<=n;i++) son[i].clear (); for (long long i=2;i<=n;i++) {scanf ("%i64d%i64d", p+i,t+i); Son[p[i]].push_back (i);} double l=0, R=100;//double Mid;while (ABS (R-L) >0.0001) {mid= (l+r)/2.0;x=mid;if (Deal () ==0)//time required is greater than sum_tr=mid;elsel=mid;} printf ("%.7lf\n", L);  }return 0;}





URAL 1822. Hugo II ' s WAR tree structure + dichotomy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.