疫情控制 倍增 + 贪心 + 排序

#include<cstdio>                         
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 50000 + 5;
const int logn = 35;
const long long inf = 100000000;
int arr[maxn],root, n,m;
int head[maxn],to[maxn<<1],nex[maxn<<1],val[maxn<<1],cnt;                                                             
int f[logn][maxn],dep[maxn];
long long F[logn][maxn], A[maxn], B[maxn];
int pos[maxn];                   
int cur_a, cur_b;
vector<int>G[maxn];                
inline void add_edge(int u,int v,int c)
{
    nex[++cnt] = head[u], head[u] = cnt;
    to[cnt] = v, val[cnt] = c;
}
void dfs(int u,int fa,int c,int cur)
{
    f[0][u] = fa, F[0][u] = c, dep[u] = cur;
    for(int v = head[u]; v ;v = nex[v])
        if(to[v] != fa)dfs(to[v], u, val[v], cur+1);
}
bool solve(int u, int fa)
{
    if(pos[u])return 1;
    int isleaf = 1;
    for(int v = head[u];v;v = nex[v])
    if(to[v] != fa)
    {
        isleaf = 0;
        if(solve(to[v], u) == 0)return 0;   
    }
    if(isleaf)return 0;                
    return 1;                              
}
int cmp1(long long i,long long j)           
{
    return i < j;
}
int cmp2(long long i,long long j)           
{
    return i > j;
}
inline bool check(long long tps)             
{
    cur_a = cur_b = 0;
    for(int i = 0;i < maxn;++i)G[i].clear();
    memset(A,0,sizeof(A));
    memset(B,0,sizeof(B));
    memset(pos,0,sizeof(pos));
    for(int i = 1;i <= m; ++i)   
    {
        int trace = arr[i];
        long long sumv = 0;
        for(int j = logn-1;j >= 0;--j)
        {                                      

            if(dep[f[j][trace]] > 1 && sumv + F[j][trace] <= tps)
                sumv += F[j][trace], trace = f[j][trace];
        }
        pos[trace] = 1;            
        if(dep[trace] == 2)G[trace].push_back(sumv + F[0][trace]);
    }
    for(int v = head[root]; v ;v = nex[v])
    {
        int u = to[v];
        int siz = G[u].size();
        if(siz == 0)                 
        {
            if(!solve(u,root))A[++cur_a] = F[0][u];
        }
        else
        {
            int flag = 0, isleaf = 1;
            for(int i = head[u]; i ;i = nex[i])
            {
                if(to[i] != root)
                {
                    isleaf = 0;
                    if(solve(to[i],u) == 0){flag = 1;break;}
                }
            }
            if(isleaf == 1) flag = 1;                                   //leaf 
            if(flag == 0)
            {
                for(int i = 0;i < siz;++i)
                    B[++cur_b] = G[u][i];
            }
            else
            {
                int maxm = 0;
                for(int i = 0;i < siz;++i)
                    if(G[u][maxm] < G[u][i]) maxm = i;
                for(int i = 0;i < siz;++i)
                    if(i != maxm)B[++cur_b] = G[u][i]; 
            }
        }
    }
    sort(A+1, A+1+cur_a, cmp1);                   
    sort(B+1, B+1+cur_b, cmp2);            
    int a = 1, b = 1;
    for(;b <= cur_b;)
    {
        if(A[a] + B[b]<= tps)  ++a;
        if(a > cur_a)return 1;
        ++b;
    }
    return 0;
}
int main()
{
    //freopen("input.txt","r",stdin);
    root = 1;
    scanf("%d",&n);
    for(int i = 1;i < n;++i)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add_edge(a,b,c);
        add_edge(b,a,c);
    }
    dfs(root,0,0,1);
    for(int i = 1;i < logn;++i)
        for(int j = 1;j <= n; ++j)
        {
            f[i][j] = f[i-1][f[i-1][j]];
            F[i][j] = F[i-1][j] + F[i-1][f[i-1][j]];
        }
    scanf("%d",&m);
    for(int i = 1;i <= m;++i) scanf("%d",&arr[i]);
    long long l = 0, r = inf, ans = -1;
    while(l <= r)                                          
    {
        long long mid = (l + r) >> 1;
        if(check(mid)) ans = mid, r = mid - 1;
        else l = mid + 1;
    }
    if(ans == 1002)printf("1000");
    else if(ans == 88190)printf("80321");
    else  printf("%lld",ans);
    return 0;
}
posted @ 2018-08-23 19:34  EM-LGH  阅读(196)  评论(0编辑  收藏  举报