Codeforces round 261 div.2 e pashmak and graph -- DP on DAG

Source: Internet
Author: User

Question: n vertices, m edges, each edge has a weight value. Find a path with the largest number of edges, and the length of the output path.

Solution: Sort edge weights from small to large, and traverse them from large to small. DP [u] indicates the maximum length of the strictly incrementing path that can be formed from U. DP [u] = max (DP [u], DP [v] + 1). Because there are duplicate edge weights, we use the DIS array to record them first, to update the duplicate edge weights when they are not repeated.

Code: (non-original)

# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <cmath> # include <algorithm> using namespace STD; # define n 100007 struct node {int U, V, W;} edge [3 * n]; int DP [3 * n], DIS [3 * n]; int CMP (node ka, node KB) {return ka. W <kb. w;} int main () {int n, m, I, j; scanf ("% d", & N, & M); for (I = 0; I <m; I ++) scanf ("% d", & edge [I]. u, & edge [I]. v, & edge [I]. w); memset (DP, 0, sizeof (DP); memset (DIS, 0, sizeo F (DIS); sort (edge, edge + M, CMP); edge [M]. W =-1; int ans = 1; int Maxi = 1; for (I = s-1; I> = 0; I --) {int u = edge [I]. u; int v = edge [I]. v; int W = edge [I]. w; Maxi = DP [v] + 1; DIS [u] = max (DIS [u], max (Maxi, DP [u]); ans = max (ANS, maxi); if (I = 0 ||w> edge [I-1]. w) {for (j = I; j <m; j ++) // update the Dp value of the same Edge Weight {DP [edge [J]. u] = dis [edge [J]. u]; If (edge [J]. w! = Edge [J + 1]. W) Break ;}} printf ("% d \ n", ANS); Return 0 ;}
View code

 

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.