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 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.
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.
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 ( 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.
Output
For each test output one line contains a integer,denoting the maximum size of Bobo Set.
Sample Input
7 3 30 350 100 200 300 400 1 2 2 3 3 4 4 5 5 6 6 7
Sample Output
5
Source
这里给出两种方法。【其实差不多。。
构建出一个单向图,每次dfs出一个没有更新的点去加上能更新的点。用一个数组记录每个点的值,也在这里更新。最后输出最大的
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stdio.h>
#include <queue>
#include <vector>
#include <string.h>
using namespace std;
#define Max 500010
vector<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 500010
int 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;
}