JSOI BZOJ4472salesman - tree dp

Description

A salesman, T, wants to go to several towns to sell goods. Since the area is a mountainous area with inconvenient transportation, there is only one route
between may pass through other towns. Small T can accurately estimate the net
benefit . These net gains may be negative, meaning that the profit from selling the merchandise does not cover the cost. Due to the inconvenience of transportation, small
T needs to stop in each town. The number of stops in each town has nothing to do with the net income in the place, because many fees are not charged
by time, and each town needs small T’s goods. It is also relatively fixed, and it saturates after staying once. In order to
strengthen has strict regulations on the maximum number of stays for foreigners. Please help Xiao T to design a tour
plan , that is, a travel plan that starts from his hometown, stops in every town he passes through, and finally returns to his hometown. Your program only needs to output the
maximum benefit and whether the optimal solution is unique. The plan does not include the details of the route, and the criteria for the same plan is whether the towns selected to pass
and stop are the same. Because cancelling the tour is also an option, the maximum gain will not be negative. Little
T 's net income in his hometown is zero, because he is a local in his hometown, and of course there is no limit on the number of times he can stay in his hometown.

Input

The first line of input is a positive integer n (5<=n<=100000), representing the number of towns. Towns are named after numbers from 1 to n. Little T's hometown
is named 1. The second and third lines both contain n-1 integers separated by spaces, and the ith number in the second line represents
the net benefit of staying in town i+1. The ith number in the third row represents the maximum number of stops specified by town i+1. All maximum
dwell times are not less than 2. The next n-1 lines each have two positive integers x, y from 1 to n,
separated , indicating that there is a two-way road between x and y that does not pass through other towns. The input data ensures that all towns are connected.

Output

The output has two lines, the first contains a natural number representing the maximum benefit of the tour. If
the solution is unique, output "solution is unique" on the second line, otherwise output "solution is not unique" on the second line.

Sample Input

9

-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 9
Sample Output

9

solution is unique

// The best routes include towns 1, 2, 4, 5, 9.


First BB two sentences, I was trapped by the priority queue of C++. When I manually adjusted q.size()=0, q.top() could still return the value, and I was also drunk.
Closer to home, this question requires us to find the maximum value, and we find that the maximum value is required, so the tree-shaped dp is decisive. We open a priority queue and throw the value of each of its children into it when dp. Because a certain place can stay at most w[i] times, which means that w[i]-1 child nodes can be taken. If the node < 0, it is definitely not better to take it. If == 0, it can be taken or not. take, so this loses solution is not unique. If w[i]==1 and there are two remaining child nodes with the same weight, you can take any one, so you also lose solution is not unique. So this question is over.

Code:

#include<bits/stdc++.h>
#include<queue>
#define MAXN 100010
#define pa pair<int,int>
#define mp make_pair
#define fi first
#define se second
using namespace std;
int read(){
    char c;int x=0,y=1;while(c=getchar(),(c<'0'||c>'9')&&c!='-');
    if(c=='-') y=-1;else x=c-'0';while(c=getchar(),c>='0'&&c<='9')
    x=x*10+c-'0';return x*y;
}
int n,v[MAXN],w[MAXN],head[MAXN<<1],nxt[MAXN<<1],to[MAXN<<1],cnt,fa[MAXN];
void add(int x,int y){
    to[cnt]=y;nxt[cnt]=head[x];head[x]=cnt;cnt++;
    to[cnt]=x;nxt[cnt]=head[y];head[y]=cnt;cnt++;
}
void BuildTree(int x,int wy){
    fa[x]=wy;
    for(int i=head[x];i!=-1;i=nxt[i]){
        int go=to[i];
        if(go==fa[x]) continue;
        BuildTree(go,x);
    }
}
pa dfs(int x){
    priority_queue<pa> q;
    for(int i=head[x];i!=-1;i=nxt[i]){
        int go=to[i];
        if(go==fa[x]) continue;
        q.push(dfs(go));
    }
    int res=0,p=0,re=0;
    while(w[x]&&!q.empty()){
        pa a=q.top();q.pop();
        if(a.fi==0){p=1;break;}if(a.fi<0)break;
        if(a.se) p=1;re=a.fi;res+=a.fi;w[x]--;
        if(q.empty()) break;
        pa b=q.top();
        if(!w[x]&&b.fi==re) p=1;
    }
    while(!q.empty()) q.pop();
    return mp(res+v[x],p);
}
int main()
{
    memset(head,-1,sizeof(head));
    n=read();v[1]=0;w[1]=2e9;
    for(int i=2;i<=n;i++) v[i]=read();
    for(int i=2;i<=n;i++) w[i]=read(),w[i]--;
    for(int i=1;i<=n-1;i++){
        int x=read(),y=read();
        add(x,y);
    }
    BuildTree(1,0);
    pa ans=dfs(1);
    printf("%d\n",ans.fi);
    if(ans.se) puts("solution is not unique");
    else puts("solution is unique");
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325624578&siteId=291194637