[Bzoj4472] [tree DP] Salesman

topic

Original address

Comment

Just read this question still feel quite a mess, might then brain is not awake, once again regain the feel of Tarjan. Of course, you should eventually find DP with a tree.

(Hereinafter dp [u] u representatives to the root of the maximum profit including himself subtree, bool g [u] u represents the number of programs and sub-tree is unique, unique and 0 otherwise 1, t [u] represents the number of u, v [u] represents the value of u)

Calculate the maximum profit is really quite simple. A bit like done before air-conditioned classrooms , but more than a number of restrictions and negative, but it is not difficult to deal with. U computing time because each son must return u after the finish to get back to the root, so we can only son dp values are sorted, before selecting t [u] -1 months, and immediately stop experiencing negative since then the only negative option down the total value becomes smaller.

So how treatment program is unique it?

We opened a bool array u g represents the number of its sub-tree program is unique. Obviously, only the number of programs under the following three conditions is not unique:

A son acquired dp value0 (election can not vote)

The son made a g value of 1(There are different paths in the sub-tree Zheke)

The next election is not the son and the son of the last selected f values are the same (can be replaced) (I wrote when neglected this point but still A, and about data too weak)

Then starting from the root recursively again on it, oh, last seen dp [1] and g [1] on it.

(Part really does not want to come practice with reference to DZN big brother, thank you ~)

 Code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=100000+5,Inf=2147483647;
 4 inline int read(){
 5    int s=0,w=1;
 6    char ch=getchar();
 7    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
 8    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
 9    return s*w;
10 }
11 int n,head[maxn],tot,v[maxn],t[maxn],dp[maxn];
12 bool g[maxn];
13 struct node{
14     int to,next;
15 }e[2*maxn];
16 void Add(int a,int b){
17     e[tot].to=b;
18     e[tot].next=head[a];
19     head[a]=tot;
20     tot++;
21 }
22 void dfs(int x,int fa){
23     priority_queue<pair<int,int> > q;//按照dp值大小排序 
24     for(int i=head[x];i;i=e[i].next){
25         int v=e[i].to;
26         if(v==fa) continue;
27         dfs(v,x);
28          q.push (the make_pair (DP [V], V));
 29      }
 30      int NUM = 0 , SUM = 0 , Judge = 0 ;
 31 is      the while (q.empty () && NUM <T [X] -! . 1 ) {
 32          IF (q.top () First <. 0 ) BREAK ; // drag occurring immediately stop 
33 is          IF (q.top () == First. 0 {) // 0 described embodiment is not 1,
 34          / / and 0 behind either 0 or negative, can not contribute, directly back 
35              Judge = . 1 ;
 36              BREAK ;
 37 [          }
38 is          SUM + = . Q.top () First;
 39          IF (G [. Q.top () SECOND] == . 1 ) Judge = . 1 ;
 40          q.pop ();
 41 is          NUM ++ ;
 42 is      }
 43 is      DP [X] = SUM + V [X];
 44 is      G [X] = Judge;
 45  }
 46 is  int main () {
 47      TOT = . 1 ;
 48      T [ . 1 ] = Inf; // attention to his home can take numerous 
49      n-= Read () ;
 50      for ( int I = 2;i<=n;i++) v[i]=read();
51     for(int i=2;i<=n;i++) t[i]=read();
52     for(int i=1;i<=n-1;i++){
53         int x,y;
54         x=read(); y=read();
55         Add(x,y);
56         Add(y,x);
57     }
58     dfs(1,0);
59     printf("%d\n",dp[1]);
60     if(g[1]) printf("solution is not unique");
61     else printf("solution is unique");
62     return 0;
63 }
View Code

Fortunately, even holy, songs to chant blog.

Guess you like

Origin www.cnblogs.com/DarthVictor/p/12633732.html