2015 Multi-University Training Contest 3 1010
2015-07-29 15:44
447 查看
Crazy Bobo
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 905 Accepted Submission(s): 271
[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
[align=left]Source[/align]
2015 Multi-University Training Contest 3
题意:给定一棵树,每个点都有其权值,现在有对集合的定义:
1、集合是树的一个子图并且要连通
2、点按照权值排序,求排序后相邻的两点X,Y,原路径上两点间的任意点的权值要小于min(X,Y)
求符合条件的集合最多含有多少个点
分析:官方题解说是在原来树上的点,权值小的到权值大的构成一条单项边,然后求权值小的点为起点构成的最大的点集。
可是题目意思明明是求排序后相邻的两个点的原路径上符合条件的点集最大值啊!o(╯□╰)o
可以这么想,因为是权值小的点向权值大的点构成一条单项边,如果存在一条边,那么可以沿着这条边继续搜下去,当边不存在的时候说明两端的点是集合里面最大的两个点,有因为在一棵树上,所以这两点的排序后的位置是相邻的。
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<string> #include<iostream> #include<cstring> #include<cmath> #include<stack> #include<queue> #include<vector> #include<map> #include<stdlib.h> #include<algorithm> #define LL __int64 using namespace std; const int MAXN=500000+5; int n; int val[MAXN]; int cn[MAXN]; vector<int> w[MAXN]; int DFS(int s) { if(cn[s]!=0) return cn[s]; cn[s]=1; for(int i=0;i<w[s].size();i++) cn[s]+=DFS(w[s][i]); return cn[s]; } int main() { //freopen("in.txt","r",stdin); while(scanf("%d",&n)!=EOF) { memset(cn,0,sizeof(cn)); memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { scanf("%d",&val[i]); w[i].clear(); } for(int i=1;i<n;i++) { int u,v; scanf("%d %d",&u,&v); if(val[u]<val[v]) w[u].push_back(v); if(val[v]<val[u]) w[v].push_back(u); } for(int i=1;i<=n;i++) if(!cn[i]) DFS(i); int ans=-1; for(int i=1;i<=n;i++) if(ans<cn[i]) ans=cn[i]; printf("%d\n",ans); } return 0; }
View Code
相关文章推荐
- 2015 Multi-University Training Contest 3 hdu 5323 Solve this interesting problem
- HDU 5316 Magician 2015 Multi-University Training Contest 3 1001 线段树
- Drainage Ditches
- Drainage Ditches 分类: POJ 图论 2015-07-29 15:01 7人阅读 评论(0) 收藏
- Climbing Stairs
- UVA 10976 - Fractions Again?!
- http://blog.csdn.net/ym19860303/article/details/8462450
- HDU-5319 Painter
- 关于RAID
- 2015 Multi-University Training Contest 3 RGCDQ
- Painter(模拟)
- 在服务 ObtainData 实现的协定列表中找不到协定名称 "IMetadataExchange"。将 ServiceMetadataBehavior 添加到配置文件或直接添加到 ServiceHost,以启用对该协定的支持。
- 有关Error - RtlWerpReportException failed with status code :-1073741823.的解决方法
- HDU 1021 Fibonacci Again
- HDU 5319 Painter
- gensim安装http://blog.csdn.net/helihongzhizhuo/article/details/47038575
- Failed to push selection: Read-only file system的解决方法
- zz 通过INFORMATION_SCHEMA.INNODB_TRX、INNODB_LOCKS、INNODB_LOCK_WAITS 三个表获取事务与锁的信息
- 2015 Multi-University Training 3 多校集训 第三场 部分题解及反思
- [模拟] 多校联合第三场 painter HDU 5319