spoj 1825 FTOUR2 - Free tour II

http://www.elijahqi.win/2018/02/24/spoj-1825/
题意翻译

给定一棵n个点的树,树上有m个黑点,求出一条路径,使得这条路径经过的黑点数小于等于k,且路径长度最大

题目描述

After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, Travel Agent SPOJ goes on with another discount tour.

The tour will be held on ICPC island, a miraculous one on the Pacific Ocean. We list N places (indexed from 1 to N) where the visitors can have a trip. Each road connecting them has an interest value, and this value can be negative (if there is nothing interesting to view there). Simply, these N places along with the roads connecting them form a tree structure. We will choose two places as the departure and destination of the tour.

Since September is the festival season of local inhabitants, some places are extremely crowded (we call them crowded places). Therefore, the organizer of the excursion hopes the tour will visit at most K crowded places (too tiring to visit many of them) and of course, the total number of interesting value should be maximum.

Briefly, you are given a map of N places, an integer K, and M id numbers of crowded place. Please help us to find the optimal tour. Note that we can visit each place only once (or our customers easily feel bored), also the departure and destination places don’t need to be different.

输入输出格式

输入格式:

There is exactly one case. First one line, containing 3 integers N K M, with 1 <= N <= 200000, 0 <= K <= M, 0 <= M <= N.

Next M lines, each line includes an id number of a crowded place.

The last (N - 1) lines describe (N - 1) two-way roads connected N places, form a b i, with a, b is the id of 2 places, and i is its interest value (-10000 <= i <= 10000).

输出格式:

Only one number, the maximum total interest value we can obtain.

输入输出样例

输入样例#1: 复制

8 2 3
3
5
7
1 3 1
2 3 10
3 4 -2
4 5 -1
5 7 6
5 6 5
4 8 3
输出样例#1: 复制

12
设g[i,k]表示在i的子树中黑点不超过K的最长路径是多少

这题有个坑点 注意复杂度不能写成n*k 而且可以存在单点的情况 每次考虑维护这个g[i,k]的时候注意要将所有子树的黑点个数从小到大排序做 这样一不影响答案 二 复杂度有保证 为什么因为黑点总共就这么多 每次点分黑点都会减少 具体题解可以看一看当年的论文2009的

https://wenku.baidu.com/view/1bc2e4ea172ded630b1cb602.html

#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#define N 220000
#define inf 0x3f3f3f3f
#define pa pair<int,int>
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
    return x*f;
}
struct node{
    int y,z,next;
}data[N<<1];
int f[N],size[N],h[N],sum,n,root,k,m,g[N],num,ans=0;
int mxp,mx[N],maxb[N],nmb[N],dis[N],dep[N];
bool visit[N],black[N];
inline void get_root(int x,int fa){
    f[x]=0;size[x]=1;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (y==fa||visit[y]) continue;
        get_root(y,x);size[x]+=size[y];f[x]=max(f[x],size[y]);
    }f[x]=max(f[x],sum-size[x]);
    if (f[root]>f[x]) root=x;
}
inline void dfs(int x,int fa){
    mxp=max(mxp,nmb[x]);
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y,z=data[i].z;if (y==fa||visit[y]) continue;
        dis[y]=dis[x]+z;nmb[y]=nmb[x]+black[y];dfs(y,x);
    }
}
inline void dfs1(int x,int fa){
    g[nmb[x]]=max(g[nmb[x]],dis[x]);
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (y==fa||visit[y]) continue;dfs1(y,x);
    }
}
inline void solve(int x){
    visit[x]=1;dis[x]=0;nmb[x]=0;
    vector<pa> a;k-=black[x];
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (visit[y]) continue;dis[y]=dis[x]+data[i].z;nmb[y]=nmb[x]+black[y];mxp=0;
        if(size[y]>size[x]) size[y]=sum-size[x];dfs(y,x);maxb[y]=mxp;a.push_back(make_pair(maxb[y],y));
    }sort(a.begin(),a.end());int mxdp=0;mx[0]=0;
    for (int i=0;i<a.size();++i){
        int y=a[i].second,now=1;dfs1(y,x);
        for (int j=min(maxb[y],k);~j;--j) {
            while(now<=mxdp&&now+j<=k) mx[now]=max(mx[now],mx[now-1]),++now;
            ans=max(ans,g[j]+mx[now-1]);
        }mxdp=a[i].first;
        for (int j=0;j<=maxb[y];++j) mx[j]=max(mx[j],g[j]),g[j]=-inf;
    }k+=black[x];for (int i=0;i<=mxdp;++i) mx[i]=g[i]=-inf;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (visit[y]) continue;
        sum=size[y];root=0;get_root(y,x);solve(root);
    }
}
int main(){
    freopen("spoj.in","r",stdin);
    freopen("spoj.out","w",stdout);
    n=read();k=read();m=read();
    for (int i=1;i<=m;++i) black[read()]=1;
    for (int i=0;i<=k;++i) g[i]=mx[i]=-inf;
    for (int i=1;i<n;++i){
        int x=read(),y=read(),z=read();
        data[++num].y=y;data[num].next=h[x];data[num].z=z;h[x]=num;
        data[++num].y=x;data[num].next=h[y];data[num].z=z;h[y]=num;
    }f[0]=inf;root=0;sum=n;get_root(1,0);
    solve(root);
    printf("%d",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值