Codeforces the longest ascending subsequence on the 490F Treeland tour tree

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions

A tree given n points.

The following n numbers represent the point weights.

The following n-1 row gives the tree.

Find a chain and then find the longest ascending subsequence of the point weights in this chain.

The length of the longest ascending sub-sequence.

Ideas:

The first is to maintain a chain and ask for answers, but if the direct tree DP (which records the length of each point u,u down and you descending) causes the sequence to go back and forth, that is, increment and decrement are on the same chain.

Enumerate each point as the beginning of the subsequence, and then maintain a chain of NLOGN practices for the LIS.


Import Java.io.printwriter;import java.util.arraylist;import Java.util.arrays;import Java.util.Collections;import Java.util.hashmap;import java.util.hashset;import java.util.iterator;import Java.util.list;import java.util.Map; Import Java.util.scanner;import Java.util.set;import Java.util.treeset;public class Main {int max (int x, int y) {return x > y? X:y;} static int N = 6050;int[] A = new Int[n], len = new int[n];int n;int ans; arraylist<integer>[] G = new arraylist[n];int[] Stack = new Int[n];int top;void find (int u, int fa) {int pos =-1, V Al = -1;if (A[u]>stack[top-1]) {pos = -2;//-2 indicates a new element added stack[top++] = A[u];ans = max (ans, top);} Else{int L = 0, r = top-1, Siz = 0;while (l <= r) {int mid = (l+r) >>1;if (Stack[mid] < a[u]) L = mid+1;else {r = m Id-1;siz = mid;}} pos = siz; val = Stack[siz]; Stack[pos] = A[u];} for (int i = 0; i < g[u].size (); i++) {int v = g[u].get (i); if (v = = FA) Continue;find (V, u);} if (pos! =-1) {if (pos = =-2) top--;else {Stack[pos] = val;}}} void soLve (int u) {for (int i = 0; i < g[u].size (); i++) {int v = g[u].get (i); top = 0; stack[top++] = A[u];find (V, U);}} void input () {n = cin.nextint (); for (int i = 1; I <= n; i++) {G[i] = new ArrayList (); A[i] = Cin.nextint ();} for (int i = 1, u, v; i < n; i++) {u = cin.nextint (); v = cin.nextint (); G[u].add (v); G[v].add (U);}} public void work () {input (); ans = 1;for (int i = 1; I <= n; i++) solve (i); out.println (ans); Main () {cin = new Scanner (system.in); out = new PrintWriter (System.out);} public static void Main (string[] args) {main E = new main (); E.work (); Out.close ();} Public Scanner cin;public static printwriter out;}

Codeforces the longest ascending subsequence on the 490F Treeland tour tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.