hdu 5325 Crazy Bobo (树形dp)
2015-07-29 19:48
375 查看
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Total Submission(s): 1077 Accepted Submission(s): 326
[align=left]Problem Description[/align]
Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node has a weight wi. All the weights are distrinct.
A set with m nodes v1,v2,...,vm is a Bobo Set if:
- The subgraph of his tree induced by this set is connected.
- After we sort these nodes in set by their weights in ascending order,we get u1,u2,...,um,(that is,wui<wui+1 for i from 1 to m-1).For any node x in the path from ui to ui+1(excluding ui and ui+1),should satisfy wx<wui.
Your task is to find the maximum size of Bobo Set in a given tree.
[align=left]Input[/align]
The input consists of several tests. For each tests:
The first line contains a integer n (1≤n≤500000). Then following a line contains n integers w1,w2,...,wn (1≤wi≤109,all the wi is distrinct).Each of the following n-1 lines contain 2 integers ai and bi,denoting an edge between vertices ai and bi (1≤ai,bi≤n).
The sum of n is not bigger than 800000.
[align=left]Output[/align]
For each test output one line contains a integer,denoting the maximum size of Bobo Set.
[align=left]Sample Input[/align]
7
3 30 350 100 200 300 400
1 2
2 3
3 4
4 5
5 6
6 7
[align=left]Sample Output[/align]
5
YY一下,想到只要保证一棵子树的节点到其所有的叶子节点的路径都是满足递增的,找出一棵这样的最大的子树即可。
先从下往上dp一遍,然后再从上往下,把父亲的信息传送下来,嗯,然后就以O(n)得到了所需要的结果
不过直接dfs会爆栈,所以得开一下栈,然后用C++提交
Crazy Bobo
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1077 Accepted Submission(s): 326
[align=left]Problem Description[/align]
Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node has a weight wi. All the weights are distrinct.
A set with m nodes v1,v2,...,vm is a Bobo Set if:
- The subgraph of his tree induced by this set is connected.
- After we sort these nodes in set by their weights in ascending order,we get u1,u2,...,um,(that is,wui<wui+1 for i from 1 to m-1).For any node x in the path from ui to ui+1(excluding ui and ui+1),should satisfy wx<wui.
Your task is to find the maximum size of Bobo Set in a given tree.
[align=left]Input[/align]
The input consists of several tests. For each tests:
The first line contains a integer n (1≤n≤500000). Then following a line contains n integers w1,w2,...,wn (1≤wi≤109,all the wi is distrinct).Each of the following n-1 lines contain 2 integers ai and bi,denoting an edge between vertices ai and bi (1≤ai,bi≤n).
The sum of n is not bigger than 800000.
[align=left]Output[/align]
For each test output one line contains a integer,denoting the maximum size of Bobo Set.
[align=left]Sample Input[/align]
7
3 30 350 100 200 300 400
1 2
2 3
3 4
4 5
5 6
6 7
[align=left]Sample Output[/align]
5
YY一下,想到只要保证一棵子树的节点到其所有的叶子节点的路径都是满足递增的,找出一棵这样的最大的子树即可。
先从下往上dp一遍,然后再从上往下,把父亲的信息传送下来,嗯,然后就以O(n)得到了所需要的结果
不过直接dfs会爆栈,所以得开一下栈,然后用C++提交
//##################### //Author:fraud //Blog: http://www.cnblogs.com/fraud/ //##################### #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <sstream> #include <ios> #include <iomanip> #include <functional> #include <algorithm> #include <vector> #include <string> #include <list> #include <queue> #include <deque> #include <stack> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <climits> #include <cctype> using namespace std; #define XINF INT_MAX #define INF 0x3FFFFFFF #define MP(X,Y) make_pair(X,Y) #define PB(X) push_back(X) #define REP(X,N) for(int X=0;X<N;X++) #define REP2(X,L,R) for(int X=L;X<=R;X++) #define DEP(X,R,L) for(int X=R;X>=L;X--) #define CLR(A,X) memset(A,X,sizeof(A)) #define IT iterator typedef long long ll; typedef pair<int,int> PII; typedef vector<PII> VII; typedef vector<int> VI; int Scan() { int res=0, ch; while(ch=getchar(), ch<'0'||ch>'9'); res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0'; return res; } void Out(int a) { if(a>9) Out(a/10); putchar(a%10+'0'); } int w[500010]; vector<int>G[500010]; int dp[500010]; void dfs1(int u,int fa){ dp[u] = 1; REP(i,G[u].size()){ int v = G[u][i]; if(v == fa)continue; dfs1(v,u); if(w[v] > w[u])dp[u] += dp[v]; } } void dfs2(int u,int fa){ if(fa!=-1){ if(w[fa] > w[u])dp[u] += dp[fa]; } REP(i,G[u].size()){ int v = G[u][i]; if(v == fa)continue; dfs2(v,u); } } int main() { //ios::sync_with_stdio(false); int n; while(scanf("%d",&n)!=EOF){ REP(i,n)w[i] = Scan(); REP(i,n)G[i].clear(); int u,v; REP(i,n-1){ u = Scan(); v = Scan(); u--;v--; G[u].PB(v); G[v].PB(u); } dfs1(0,-1); dfs2(0,-1); int ans = 0; REP(i,n)ans = max(dp[i],ans); Out(ans); puts(""); } return 0; } /* 7 5 1 7 2 3 8 6 1 2 1 3 2 4 2 5 3 6 3 7 9 1 2 3 4 5 6 7 8 9 9 1 1 2 4 2 2 3 3 7 3 6 2 5 5 8 9 1 2 3 4 5 6 7 8 9 1 9 9 8 1 5 1 2 2 7 7 4 2 6 6 3 */
相关文章推荐
- 关于cassandra vnode的理解-marsyoung.
- 排序算法的稳定性讨论
- 【CodeForces】E. Xenia and Tree(分块 + LCA)
- Trim(),TrimStart(),TrimEnd()用于去除字符串中的特定字符
- 多校第三场 1010 hdu 5325 Crazy Bobo(拓扑排序+树形dp)
- linux防火墙基础和管理设置iptables规则
- java中的并发:线程通信
- 脚本学习之路之----我要熟悉awk, sed, rpm 工具
- 简单明了的明白进程和线程的区别
- 【dp】【bzoj 1079】【SCOI 2008】着色方案
- ExQuilla的使用和破解
- E 接口
- 使用freemarker中的小数点处理的一点心得!
- PHP获取当前页面的URL作为参数以供下一层的页面可以返回上一层页面
- Android网络状态广播监听
- MinGW安装及配置过程
- STM8 的MEMORY MODEL应用
- NOIP2008 火柴棒等式
- HDU 1693 Eat the Trees 插头DP
- 在线最优化求解(Online Optimization)之二:截断梯度法(TG)