4472: [Jsoi2015]salesman
Links: http://www.lydsy.com/JudgeOnline/problem.php?id=4472
Time Limit:10 Sec Memory limit:128 MB
Description
A salesman small t to a number of towns to sell goods, because the area is inaccessible mountains, any two cities and towns
There is only one route that can pass through other towns. Small t can accurately estimate the net stay in each town
Yi. These net income may be negative, that is, the profit of selling goods is not worth the expense. Because of the inconvenient traffic, little t passes through each
Towns need to stay, and the number of stops in each town is irrelevant to the net income in that place, since many costs are not counted
, and each town to the small T commodity demand is also relatively fixed, after staying once saturated. Every town to
Strengthen the law and order, the maximum number of stay of outsiders have strict rules. Please help small t design one of the most profitable tour side
From home, stopping at every town passing by, and finally returning to his hometown for a travel plan. Your program only needs to output
Maximum benefits, and whether the best solution is unique. The plan does not include the details of the route, and the same criteria are chosen
And stay in the same town. Because the cancellation of the tour is also a scheme, so the maximum benefit will not be negative. Little T at home
Rural net income is zero, because in the hometown is a native, home to small t of course there is no limit on the number of stops.
Input
The first line of input is a positive integer n (5<=n<=100000) that represents the number of towns. Towns are named after the number 1 to N. Little T's hometown life
Name is 1. The second and third lines contain n-1 integers separated by spaces, and the number of I in the second row represents the town
Net income of i+1 stay. The number of i+1 in the third row indicates the maximum number of stops stipulated by the town. All the largest
The number of stops is no less than 2. The next n-1 line has two positive integers of 1 to n x, y, and a space between each line
Separated, indicating that x, Y has a two-way road that does not pass through other towns. Enter data to ensure that all towns are connected.
Output
The output has two lines, and the first row contains a natural number that represents the maximum benefit of the tour. If the scenario is unique, the
The second line outputs "solution is unique", otherwise output "solution is not unique" on the second line.
Sample Input9
-3-4 2 4-2 3 4 6
4 4 2 2 2 2 2 2
1 2
1 3
1 4
2 5
2 6
3 7
4 8
4 9Sample Output9
Solution is unique
The best routes include towns 1, 2, 4, 5, 9.
Solution: Tree DP, Dp[i] indicates the maximum benefit of the subtree of I, dp[u] = SUM (Dp[v], Dp[v] >= 0) and can only select C[i]-one; a priority queue is OK, pay attention to the number of sentences; build two-way edge
#include <bits/stdc++.h>using namespacestd;Const intMAXN =100005;#defineINF 10000008inttot, HEAD[MAXN],DP[MAXN],C[MAXN];intUNI[MAXN];structedge{intV,NXT;} G[MAXN*2+ -];structins{intto, CO; BOOL operator< (ConstINS &a)Const{ returna.co >Co; }};p Riority_queue<ins>Q[MAXN];voidAddintUintv) {g[++TOT].NXT =Head[u]; G[TOT].V=v; Head[u]=tot;}voidDfsintUintFA) { for(inti = Head[u]; I i =g[i].nxt) { intv =g[i].v; if(v = = FA)Continue; DFS (v, u); Q[u].push ((INS) {V,dp[v]}); } inttt =0, last =-1; while(!q[u].empty () && TT < c[u]-1) {INS top=Q[u].top (); if(Top.co >=0) { if(!top.co) Uni[u] =1; TT++; Uni[u]= Uni[u] |Uni[top.to]; Dp[u]+=top.co; Last=top.co; Q[u].pop (); } Else Break; } if(!q[u].empty () && Last! =-1) if(Q[u].top (). CO = = last) uni[u] =1;}intMain () {intN; scanf ("%d",&N); for(inti =2; I <= N; i++) scanf ("%d",&Dp[i]); c[1] =inf; for(inti =2; I <= N; i++) scanf ("%d",&C[i]); for(inti =1; I < n; i++){ intu, v; scanf ("%d%d",&u,&v); Add (U, v); Add (V, u); } DFS (1,1); cout<<dp[1]<<Endl; if(uni[1]) cout<<"Solution is not unique"<<Endl; Elsecout<<"solution is unique"<<Endl;}
Bzoj 4472: [Jsoi2015]salesman