BZOJ3611 [Heoi2014] big project

3611: [Heoi2014] big project

Time Limit: 60 Sec   Memory Limit: 512 MB
Submit: 2454   Solved: 1096
[ Submit][ Status][ Discuss]

Description

There is a large national project, give a very large transportation network in the construction of several new channels. 
Our location is very special in this country, can be seen as a tree right side of the unit, the city is located on the vertices. 
Between the two countries a, b cost of building a new channel requires a shortest path tree a, b of.
 Now the country has a number of plans, each plan like this, we selected k points, and then create a new C (k, 2) The new channels between them twenty-two.
Now for each plan, we want to know:
 1. The cost of these new channels and
 2. The minimum cost of these new channels is the number 
3. the cost of these new channels is the largest number

Input

The first row and n represents the number of points.

 Next, n-1 lines of two numbers a, b indicates there is an edge between a and b.
Reference point from the beginning. The next line q represents the number of plans.
There are two lines for each plan, the first line k denotes the program selected a few points.
 The second line of k space-separated mutually different numbers which represent the selected k points.

Output

Q output lines, each line represent the costs and number three, minimum cost, maximum price. 

Sample Input

10
2 1
3 2
4 1
5 2
6 4
7 5
8 6
9 7
10 9
5
2
5 4
2
10 4
2
5 2
2
6 1
2
6 1

Sample Output

3 3 3
6 6 6
1 1 1
2 2 2
2 2 2

HINT

n<=1000000 


q <= 50000 and ensure that the sum of all k <= 2 * n 

Source

[ Submit][ Status][ Discuss]

HOME Back

answer

The establishment of a virtual tree. Maximum and minimum DP very obviously. As for the summation of two ways:

  1. Statistics forget how many times each edge
  2. Maintenance and path length and sum siz, each ans + = sum [u] * siz [v] + siz [u] * (sum [v] + w * siz [v]).

Note that only the actual node statistics siz inquiry on the line. Boring ah ...... put about other people's code
first approach

void dfs2(int x) {
    siz[x]=bo[x],maxs[x]=0,mins[x]=inf,f[x]=0;
    for (int y=now[x]; y; y=pre[y]) {
        int d=dis[son[y]]-dis[x];
        dfs2(son[y]),siz[x]+=siz[son[y]];
        ans1=min(ans1,mins[x]+mins[son[y]]+d),mins[x]=min(mins[x],mins[son[y]]+d);
        ans2=max(ans2,maxs[x]+maxs[son[y]]+d),maxs[x]=max(maxs[x],maxs[son[y]]+d);
        f[x]+=f[son[y]]+1ll*siz[son[y]]*(num-siz[son[y]])*d;
    }
    if (bo[x]) ans1=min(ans1,mins[x]),ans2=max(ans2,maxs[x]),mins[x]=0;
    now[x]=0;
}

The second approach

void dp(int u){
    siz[u]=build[u];
    mx[u]=build[u]?0:-inf;
    mi[u]=build[u]?0:inf;
    sum[u]=0;
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].to;
        dp(v);
        ans1+=(sum[u]+siz[u]*e[i].v)*siz[v]+sum[v]*siz[u];
        siz[u]+=siz[v];
        sum[u]+=sum[v]+siz[v]*e[i].v;
        ans2=min(ans2,mi[u]+mi[v]+e[i].v);
        ans3=max(ans3,mx[u]+mx[v]+e[i].v);
        mi[u]=min(mi[u],mi[v]+e[i].v);
        mx[u]=max(mx[u],mx[v]+e[i].v);
    }
    head[u]=0;
}

Guess you like

Origin www.cnblogs.com/autoint/p/11056681.html