Preorder Test

题目连接:

http://www.codeforces.com/contest/627/problem/D

Description

For his computer science class, Jacob builds a model tree with sticks and balls containing n nodes in the shape of a tree. Jacob has spent ai minutes building the i-th ball in the tree.

Jacob's teacher will evaluate his model and grade Jacob based on the effort he has put in. However, she does not have enough time to search his whole tree to determine this; Jacob knows that she will examine the first k nodes in a DFS-order traversal of the tree. She will then assign Jacob a grade equal to the minimum ai she finds among those k nodes.

Though Jacob does not have enough time to rebuild his model, he can choose the root node that his teacher starts from. Furthermore, he can rearrange the list of neighbors of each node in any order he likes. Help Jacob find the best grade he can get on this assignment.

A DFS-order traversal is an ordering of the nodes of a rooted tree, built by a recursive DFS-procedure initially called on the root of the tree. When called on a given node v, the procedure does the following:

Print v.

Traverse the list of neighbors of the node v in order and iteratively call DFS-procedure on each one. Do not call DFS-procedure on node u if you came to node v directly from u.

Input

The first line of the input contains two positive integers, n and k (2 ≤ n ≤ 200 000, 1 ≤ k ≤ n) — the number of balls in Jacob's tree and the number of balls the teacher will inspect.

The second line contains n integers, ai (1 ≤ ai ≤ 1 000 000), the time Jacob used to build the i-th ball.

Each of the next n - 1 lines contains two integers ui, vi (1 ≤ ui, vi ≤ n, ui ≠ vi) representing a connection in Jacob's tree between balls ui and vi.

Output

Print a single integer — the maximum grade Jacob can get by picking the right root of the tree and rearranging the list of neighbors.

Sample Input

5 3

3 6 1 4 2

1 2

2 4

2 5

1 3

Sample Output

3

Hint

题意

给你一棵树,带点权

让你找到一个dfs搜索的顺序中,至少大于k个点,且这k个点的最小值最大

题解:

二分答案,然后我们进行check

我们把大于mid的点标为1

然后我们就可以开始树dp了

显然对于某个点来说,除了他儿子那棵子树的所有点都是满足条件的,否则他最多选择两个儿子的不完整子树。

然后我们通过这个进行dp就好了,记录最大值和次大值。

对了,还得check一下他的父亲,看看这个点是否能够往上延展。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
int n,k;
int a[maxn],w[maxn],sz[maxn],up[maxn],dp[maxn];
vector<int> E[maxn];
int flag = 1;
void dfs(int x,int fa,int m)
{
sz[x]=1;
for(int i=0;i<E[x].size();i++)
{
int v = E[x][i];
if(v==fa)continue;
dfs(v,x,m);
w[x]+=w[v];
sz[x]+=sz[v];
}
}
void solve(int x,int fa,int m)
{
int Max1=0,Max2=0,tot=0;
for(int i=0;i<E[x].size();i++)
{
int v=E[x][i];
if(v==fa)continue;
if(w[x]-w[v]==sz[x]-sz[v]&&up[x])up[v]=1;
solve(v,x,m);
if(a[v]<m)continue;
if(sz[v]==w[v])tot+=sz[v];
else
{
if(dp[v]>Max1)Max2=Max1,Max1=dp[v];
else if(dp[v]>Max2)Max2=dp[v];
}
}
if(a[x]<m)return;
if(tot+Max1+Max2+up[x]*(n-sz[x])+1>=k)flag=1;
dp[x]=tot+Max1+1;
}
int check(int x)
{
for(int i=1;i<=n;i++)
dp[i]=0,up[i]=0;
for(int i=1;i<=n;i++)
{
if(a[i]>=x)
w[i]=1;
else
w[i]=0;
}
flag = 0;
dfs(1,-1,x);
up[1]=w[1];
solve(1,-1,x);
return flag;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<n;i++)
{
int x,y;scanf("%d%d",&x,&y);
E[x].push_back(y);
E[y].push_back(x);
}
int l = 0,r = 1e6+5,ans = 1e6+5;
while(l<=r)
{
int mid = (l+r)/2;
if(check(mid))l=mid+1,ans=mid;
else r=mid-1;
}
cout<<ans<<endl;
}
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int n,m,t,i,x[11][11];
int mo=10007;
void multiply(int a[11][11],int b[11][11])
{
int i,j,k,c[11][11];
for (i=0;i<=t;i++)
for (j=0;j<=t;j++)
{
c[i][j]=0;
for (k=0;k<=t;k++) c[i][j]=(c[i][j]+a[i][k]*b[k][j])%mo;
} for (i=0;i<=t;i++)
for (j=0;j<=t;j++)
a[i][j]=c[i][j];
}
void binary(int x[11][11],int a)
{
int i,j,y[11][11];
if (a==1) return;
for (i=0;i<=t;i++)
for (j=0;j<=t;j++)
y[i][j]=x[i][j];
multiply(x,x);
binary(x,a/2);
if (a%2==1) multiply(x,y);
}
int main()
{
scanf("%d%d%d",&m,&n,&t);
for (i=0;i<=t;i++)
{
if (i!=0) x[i-1][i]=t-i+1;
x[i][i]=m-t;
if (i!=t) x[i+1][i]=i+1;
}
binary(x,n);
printf("%d\n",x[0][0]);
}

8VC Venture Cup 2016 - Final Round D. Preorder Test 二分 树形dp的更多相关文章

  1. 8VC Venture Cup 2016 - Final Round (Div. 1 Edition) E - Preorder Test 树形dp

    E - Preorder Test 思路:想到二分答案了之后就不难啦, 对于每个答案用树形dp取check, 如果二分的值是val, dp[ i ]表示 i 这棵子树答案不低于val的可以访问的 最多 ...

  2. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)

    暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...

  3. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A

    A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. 8VC Venture Cup 2016 - Final Round C. Package Delivery 优先队列

    C. Package Delivery 题目连接: http://www.codeforces.com/contest/627/problem/C Description Johnny drives ...

  5. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组

    D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...

  6. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学

    C. XOR Equation 题目连接: http://www.codeforces.com/contest/635/problem/C Description Two positive integ ...

  7. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)B. sland Puzzle 水题

    B. sland Puzzle 题目连接: http://www.codeforces.com/contest/635/problem/B Description A remote island ch ...

  8. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A. Orchestra 水题

    A. Orchestra 题目连接: http://www.codeforces.com/contest/635/problem/A Description Paul is at the orches ...

  9. 8VC Venture Cup 2016 - Final Round (Div2) E

    贪心.当前位置满油可达的gas station中,如果有比它小的,则加油至第一个比他小的.没有,则加满油,先到达这些station中最小的.注意数的范围即可. #include <iostrea ...

随机推荐

  1. UNIX v6

    UNIX v6 http://download.csdn.net/download/u013896535/9106775 https://github.com/chromium/mini_chromi ...

  2. vue 开始开发

    1,引入vue.js文件 2,在body里用标签 编辑一个入口 <div id="app">{{msg}}</div> <-- 用双大括号 取数据显示 ...

  3. jstack 命令学习笔记

    大部分内容转载自:Java命令学习系列(二)--Jstack jstack - 查看堆栈信息 jstack ( Stack Trace for java ) 命令主要作用就是为了查看堆栈信息.它可以用 ...

  4. sharding-jdbc 实现分表

    Sharding-JDBC 简介 Sharding-JDBC直接封装JDBC API,可以理解为增强版的JDBC驱动,旧代码迁移成本: 可适用于任何基于Java的ORM框架,如:JPA.HIberna ...

  5. cvs 日常使用

    http://www.51testing.com/html/44/17144-2954.html http://www.chedong.com/tech/cvs_card.html

  6. Django如何从Model中获取字段名称——verbose_name

    一.背景 CRM项目重新总结一下,重写之前的项目发现有不少知识点已经忘记,所以特此来重新总结一下一便后续能够回忆起 二.代码分析 1. 核心代码 from django.conf.urls impor ...

  7. [Bootstrap]modal弹出框

    写在前面 在实际开发中,为了友好,更需要一种美观的弹出框,js原生的alert,很难满足需求.这里推荐一个bootstrap的弹出框. 一个例子 先看效果吧 代码: <!DOCTYPE html ...

  8. 单源点最短路径的Dijkstra算法

    在带权图(网)里,点A到点B所有路径中边的权值之和为最短的那一条路径,称为A,B两点之间的最短路径;并称路径上的第一个顶点为源点(Source),最后一个顶点为终点(Destination).在无权图 ...

  9. 【ASP.NET MVC】HTML5+MVC上传文件显示进度

    head> <title>Index</title> <style type="text/css"> #statusBorder { po ...

  10. 洛谷 P2680 运输计划-二分+树上差分(边权覆盖)

    P2680 运输计划 题目背景 公元 20442044 年,人类进入了宇宙纪元. 题目描述 公元20442044 年,人类进入了宇宙纪元. L 国有 nn 个星球,还有 n-1n−1 条双向航道,每条 ...