度度熊给定一棵树,树上的第个节点有点权。请你找出一条最长的路径,使得从沿着唯一路径走到的途中,点权不断严格递增。
换句话说,设路径为,则需要满足。输出最长满足条件的路径的长度。
第一行树的节点个数 , 接下来一行个数字,表示每个点的点权。接下来行,每行两个数代表树上的一条边,连接点。.
一行一个数字表示答案,即最长的长度。
5 3 5 5 4 1 1 2 1 3 2 4 2 5
2
4 3 4 1 2 1 2 2 3 2 4
2
//邻接表 import java.util.*; public class Main { static List<List<Integer>> graph = new ArrayList<>(); static int max = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] num = new int[n]; for (int i = 0; i < n; i++) { num[i] = sc.nextInt(); } for (int i = 0; i < num.length; i++) { graph.add(new ArrayList<>()); } // 构建图,邻接矩阵 for (int i = 0; i < n - 1; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; graph.get(a).add(b); graph.get(b).add(a); } for (int i = 0; i < num.length - 1; i++) { dfs(num, i, 1); } System.out.println(max); } public static void dfs(int[] num, int start, int wayNum) { max = Math.max(max, wayNum); for (int i : graph.get(start)) { if (num[i] > num[start]) { dfs(num, i, wayNum + 1); } } } }
#include <bits/stdc++.h>//ASI typedef long long ll; using namespace std; int n,a[100005],d[100005],dp[100005],ans; vector<int>e[100005]; int main() { ios::sync_with_stdio(0),cin.tie(0); int i,j,x,y; cin>>n; for(i=1;i<=n;i++) cin>>a[i]; for(i=1;i<n;i++) { cin>>x>>y; if(a[x]<a[y]) e[x].push_back(y),d[y]++; else if(a[x]>a[y]) e[y].push_back(x),d[x]++; } queue<int>q; for(i=1;i<=n;i++) if(!d[i]) q.push(i); while(q.size()) { int t=q.front(); q.pop(); for(i=0;i<e[t].size();i++) { y=e[t][i]; dp[y]=max(dp[y],dp[t]+1); ans=max(ans,dp[y]); d[y]--; if(!d[y]) q.push(y); } } cout<<ans+1; return 0; }
import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * @Author: LI * @Date: Created in 14:34 2022/9/13 */ class TreeNode { int weight; List<TreeNode> friend; public TreeNode(int weight) { this.weight = weight; friend = new ArrayList<>(); } } public class Main { static int max = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); TreeNode[] nodes = new TreeNode[n]; for (int i = 0; i < n; i++) { nodes[i] = new TreeNode(sc.nextInt()); } for (int i = 0; i < n - 1; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; nodes[a].friend.add(nodes[b]); nodes[b].friend.add(nodes[a]); } for (TreeNode node : nodes) { dfs(node, 1); } System.out.println(max); } private static void dfs(TreeNode node, int preSum) { max = Math.max(max, preSum); for (TreeNode friend : node.friend) { if (friend.weight > node.weight) { dfs(friend, preSum + 1); } } } }
// golang package main import ( "fmt" ) /* 实际上是舍弃无向图中至少一半的边变成树或森林, 而且最长路径不一定是从树的根节点出发。 */ func main() { n := 0 _, err := fmt.Scan(&n) if err != nil{ fmt.Println(0) return } w := make([]int, n+1) for i:=1;i<=n;i++ { wi := 0 _, err := fmt.Scan(&wi) if err != nil{ break } w[i] = wi } m := make(map[int] map[int]struct{}) child := make([]bool, n+1) for { var s, t int _, err := fmt.Scan(&s, &t) if err != nil{ break } if w[s] == w[t] { continue } if w[s] > w[t] { s, t = t, s } if _, ok := m[s]; !ok { m[s] = map[int]struct{}{} } m[s][t] = struct{}{} } ret := 0 for i:=1;i<=n;i++ { last := []int{i} cret := 0 for len(last) > 0 { cret++ cur := []int{} for i := range last { s := last[i] for t, _ := range m[s] { cur = append(cur, t) } } last = cur } if cret > ret { ret = cret } } fmt.Println(ret) }
def dfs(cur,src,step): if book[cur]>step: return None book[cur] = step for neighbor in adjList[cur]: if neighbor!=src and weight[neighbor]>weight[cur]: dfs(neighbor,cur,step+1) n = int(input()) weight = [0] w = input() for i in w.split(" "): weight.append(int(i)) adjList = [[] for i in range(n+1)] #把树当成图,构建邻接表 for i in range(n-1): a,b = map(int,input().split(' ')) adjList[a].append(b) adjList[b].append(a) book = [1 for i in range(n+1)] for i in range(1,n+1): dfs(i,0,1) #print(book) maxStep = 1 for i in range(1,n+1): maxStep = max(maxStep,book[i]) print(maxStep)
树形DP
def solve(): n = int(input()) weights = list(map(int, input().split())) from collections import defaultdict edges = defaultdict(list) for _ in range(n-1): u, v = map(int, input().split()) edges[u-1].append(v-1) edges[v-1].append(u-1) dp = [-1] * n def helper(root): if dp[root] != -1: return dp[root] res = 1 for nxt in edges[root]: if weights[root] < weights[nxt]: res = max(res, 1+helper(nxt)) dp[root] = res return res for i in range(n): helper(i) print(max(dp)) solve()
import sys class Node(object): def __init__(self, val) -> None: self.val = val self.childs = [] n = int(input().strip()) nodes = list(map(lambda x:Node(val=int(x)), input().strip().split())) for line in sys.stdin: if line.strip() == "": break father, child = map(int, line.strip().split()) father, child = child-1, father-1 nodes[father].childs.append(nodes[child]) res = float('-inf') def dfs(node): global res if node == None: return 0, 0 inc, dec = 0, 0 for child in node.childs: cinc, cdec = dfs(child) if node.val > child.val: inc = max(inc, cinc) elif node.val < child.val: dec = max(dec, cdec) res = max(res, inc + dec + 1) #print(res) return inc + 1, dec + 1 dfs(nodes[0]) # print("result: {}".format(res)) print(res)