Codeforces Round #279 (Div. 2) F. Treeland Tour
2014-12-02 00:17
330 查看
题意:
给你一棵树,然你在树上找到最长的上升序列,输出最大的长度
这道题的我的想法是最简单的,就是 DFS+LIS
,所以花的时间是最多的
将树上的值用类似树链剖分的想法变成线性的LIS序列,然后回溯
DP渣,第一眼就觉得是树P,但是不会喵~
给你一棵树,然你在树上找到最长的上升序列,输出最大的长度
这道题的我的想法是最简单的,就是 DFS+LIS
,所以花的时间是最多的
将树上的值用类似树链剖分的想法变成线性的LIS序列,然后回溯
DP渣,第一眼就觉得是树P,但是不会喵~
</pre><pre name="code" class="cpp">// whn6325689 #include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <climits> #include <complex> #include <fstream> #include <cassert> #include <cstdio> #include <bitset> #include <vector> #include <deque> #include <queue> #include <stack> #include <ctime> #include <set> #include <map> #include <cmath> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef complex<ld> point; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef vector<int> vi; #define CLR(x,y) memset(x,y,sizeof(x)) #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) #define lowbit(x) (x&(-x)) #define MID(x,y) (x+((y-x)>>1)) #define eps 1e-9 #define INF 0x3f3f3f3f #define LLINF 1LL<<62 template<class T> inline bool read(T &n) { T x = 0, tmp = 1; char c = getchar(); while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar(); if(c == EOF) return false; if(c == '-') c = getchar(), tmp = -1; while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar(); n = x*tmp; return true; } template <class T> inline void write(T n) { if(n < 0) { putchar('-'); n = -n; } int len = 0,data[20]; while(n) { data[len++] = n%10; n /= 10; } if(!len) data[len++] = 0; while(len--) putchar(data[len]+48); } //----------------------------------- const int MAXN=6010; struct Egde { int next,to; }e[MAXN<<1]; int g[MAXN],val[MAXN],head[MAXN],cnt,n,ans,top; void add_edge(int u,int v) { e[cnt].to=v; e[cnt].next=head[u]; head[u]=cnt++; e[cnt].to=u; e[cnt].next=head[v]; head[v]=cnt++; } void dfs(int u,int top,int fa) { int tmp,pos; if(top==0||val[u]>g[top]) { pos=++top; tmp=g[top]; g[top]=val[u]; ans=max(ans,top); } else { pos=lower_bound(g+1,g+1+top,val[u])-g; tmp=g[pos]; g[pos]=val[u]; } for(int i=head[u];~i;i=e[i].next) { int v=e[i].to; if(v!=fa) dfs(v,top,u); } g[pos]=tmp; } int main() { int x,y; while(read(n)) { ans=cnt=0; CLR(head,-1);CLR(g,-1); for(int i=1;i<=n;i++) read(val[i]); for(int i=1;i<n;i++) { read(x),read(y); add_edge(x,y); } for(int i=1;i<=n;i++) dfs(i,0,-1); write(ans); } return 0; }
相关文章推荐
- [树上LIS 线段树合并] Codeforces 490F #279 (Div. 2) F. Treeland Tour
- Codeforces Round #279 (Div. 2) F. Treeland Tour(lis+dfs)
- 【CF】135 Div2 Choosing Capital for Treeland
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs
- Codeforces 490F. Treeland Tour 暴力+LIS
- Codeforces Round #135 (Div. 2) D Choosing Capital for Treeland
- codeforces Round 447 div2 D Ralph And His Tour in Binary Country
- Codeforces Round #135 (Div. 2) - D. Choosing Capital for Treeland(dfs / 树形DP)
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
- Codeforces 490F. Treeland Tour 暴力+LIS
- Codeforces Round #135 (Div. 2)VD. Choosing Capital for Treeland
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
- cf 290F. Treeland Tour 最长上升子序列 + 树的回溯 难度:1
- Codeforces Round #447 (Div. 2) D. Ralph And His Tour in Binary Country(dfs)
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
- TopCoder East China College Tour Round 1 Div 1 1000Points
- Codeforces Round #135 (Div. 2)-D. Choosing Capital for Treeland
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland(从每个点出发最小需要修改几条边的方向可以到达其它所有点,输出最小改变几)
- IE6/IE7和Firefox对Div处理的差异
- 分别用marquee和div+js实现首尾相连循环滚动效果,仅3行代码