codevs round2 T2

才想起来这题还没做。。。

当时不造LIS有nlogn做法,然后tarjan写残了QAQ。。。

二分查找写得烦了。。。以后还是放弃二分,直接lower_bound吧。。

 1 #include<bits/stdc++.h>
 2 #define inc(i,l,r) for(i=l;i<=r;i++)
 3 #define dec(i,l,r) for(i=l;i>=r;i--)
 4 #define inf 1e9
 5 #define mem(a) memset(a,0,sizeof(a))
 6 #define ll long long
 7 #define succ(x) (1<<x)
 8 #define NM 2000+5
 9 using namespace std;
10 int read(){
11     int x=0,f=1;char ch=getchar();
12     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
13     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
14     return x*f;
15 }
16 struct edge{
17     int t,v;
18     edge *next;
19 }e[2*NM],*h[NM];
20 int n,_x,_y,x,i,a[NM],d[NM],s,len,ans[NM][NM],m;
21 bool v[NM];
22 void add(int x,int y){
23     e[++s].t=y;e[s].next=h[x];h[x]=&e[s];
24 }
25 void dfs(int y){
26     int t,flag=0,tmp;
27     v[y]++;
28     if(a[y]>d[len])t=++len,flag=-1;else t=lower_bound(d,d+1+len,a[y])-d;
29     tmp=d[t];d[t]=a[y];
30     ans[x][y]=len;
31     for(edge *j=h[y];j;j=j->next)
32     if(!v[j->t])dfs(j->t);    
33     d[t]=tmp;len+=flag;
34 }
35 int main(){
36     n=read();
37     inc(i,1,n-1){
38         _x=read();_y=read();
39         add(_x,_y);add(_y,_x);
40     }
41     inc(i,1,n)a[i]=read();
42     inc(x,1,n){
43         mem(d);mem(v);len=0;
44         dfs(x);
45     }
46     m=read();
47     inc(i,1,m){
48         _x=read();_y=read();
49         printf("%d
",ans[_x][_y]);
50     }
51     return 0;
52 }
View Code
原文地址:https://www.cnblogs.com/onlyRP/p/4847094.html