hdu 5325 Crazy Bobo 多校1010
来源:互联网 发布:腾讯来电软件怎么回事 编辑:程序博客网 时间:2024/05/01 11:41
Crazy Bobo
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 670 Accepted Submission(s): 208
Problem Description
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 nodesv1,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 getu1,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.
A set with m nodes
- 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
Your task is to find the maximum size of Bobo Set in a given tree.
Input
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.
The first line contains a integer n (
The sum of n is not bigger than 800000.
Output
For each test output one line contains a integer,denoting the maximum size of Bobo Set.
Sample Input
73 30 350 100 200 300 4001 22 33 44 55 66 7
Sample Output
5
Source
2015 Multi-University Training Contest 3
这里给出两种方法。【其实差不多。。
构建出一个单向图,每次dfs出一个没有更新的点去加上能更新的点。用一个数组记录每个点的值,也在这里更新。最后输出最大的
#pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <stdio.h>#include <queue>#include <vector>#include <string.h>using namespace std;#define Max 500010vector<int> edge[Max];int w[Max],ans[Max],n;void dfs(int a){ ans[a]=1; int len=edge[a].size(); for(int i=0;i<len;i++) { int v=edge[a][i]; if(!ans[v]) dfs(v); ans[a]+=ans[v]; }}int main(){ while(~scanf("%d",&n)) { memset(ans,0,sizeof(ans)); for(int i=1;i<=n;i++) { scanf("%d",&w[i]); edge[i].clear(); } int a,b; for(int i=1;i<n;i++) { scanf("%d%d",&a,&b); if(w[a]<w[b]) edge[a].push_back(b); else edge[b].push_back(a); } for(int i=1;i<=n;i++) { if(ans[i]) continue; dfs(i); } int maxx=-1; for(int i=1;i<=n;i++) { maxx=max(maxx,ans[i]); } cout<<maxx<<endl; } return 0;}
第一句话是hdu防爆栈的。。。
先建图,按值排序
然后
for i = 1 to n
f[a[i].w] = Σf[v] + 1;(v为与a[i].i邻接的点)
for i = 1 to n
f[a[i].w] = Σf[v] + 1;(v为与a[i].i邻接的点)
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <string.h>#include <math.h>#include <queue>#include <stack>#include <stack>#include <set>#include <map>#include <fstream>using namespace std;#define Max 500010int vis[Max],v[Max],k[Max],top;vector<int>mp[Max];int w[Max];struct pt{ int w,p;};pt r[Max];struct ppt{ int w;};ppt qq[Max];bool cmp(pt a,pt b){ return a.w>b.w;}int main(){ int n; while(cin>>n) { int a,b; memset(k,0,sizeof(k)); for(int i=1;i<=n;i++) { scanf("%d",&r[i].w); //cin>>r[i].w; r[i].p=i; mp[i].clear(); } sort(r+1,r+n+1,cmp); for(int i=1;i<n;i++) { scanf("%d%d",&a,&b); //cin>>a>>b; mp[a].push_back(b); mp[b].push_back(a); } int res=0; for(int i=1;i<=n;i++) { int u=r[i].p; int len=mp[u].size(); k[u]++; for(int i=0;i<len;i++) { int v=mp[u][i]; k[u]+=k[v]; } } res=-1; for(int i=1;i<=n;i++) {res=max(k[i],res);} cout<<res<<endl; //outfile.close(); } return 0;}
0 0
- hdu 5325 Crazy Bobo 多校1010
- HDU 5325 Crazy Bobo
- hdu 5325 Crazy Bobo
- HDU 5325 Crazy Bobo
- hdu 5325 Crazy Bobo
- HDU 5325 Crazy Bobo
- HDU 5325 CRAZY BOBO 排序
- HDU 5325 - Crazy Bobo(DFS)
- hdu 5325 Crazy Bobo dfs
- HDU 5325 Crazy Bobo(深搜)
- hdu 5325 Crazy Bobo 乱搞+搜索
- hdu 5325 Crazy Bobo(树形dp)
- hdu 5325 Crazy Bobo 拓扑排序
- Hdu 5325 Crazy Bobo (拓扑排序)
- 多校第三场 1010 hdu 5325 Crazy Bobo(拓扑排序+树形dp)
- HDU 5325 Crazy Bobo(思路+dfs 记忆化)
- hdu-5325 Crazy Bobo (2015 Multi-University Training Contest 3)
- 树上记忆化搜索(Crazy Bobo,HDU 5325)
- 9个Web工具提高Android开发效率
- sencha touch-2.4.1,pullrefresh设置store的param
- 遮罩层特效,登录界面的应用
- 菜鸟的安卓实习之路---在使用popupWindow 的时候放置窗体泄露android.view.WindowManager$BadTokenException: Unable to add wind
- 上下文无关文法1
- hdu 5325 Crazy Bobo 多校1010
- string - memmove源码
- 梦江南(二首)
- LeetCode(75) Sort Colors
- 1008Maya Calendar
- MDI中重新排列view视口(子窗口)
- JSP入门
- 菜鸟的安卓实习之路---微信悬浮窗通知窗口怎么做怎么做?
- eclipse错误:java was started but returned exit code=-1073741510