P3119 [usaco 15jan] Grass identification grass cownoisseur

Source: Internet
Author: User

Portal

Obviously, every time you go to a Unicom block, you must repeat the entire grassland of the UNICOM block.

Then you can directly create a layered graph and run the longest path.

(For convenience, the following strongly connected components are called points)

However, there is a small problem. How can we judge if we move backwards to the previous point (because each point has only one contribution)

In fact, there is no need to judge, because if you start from, you must go retrograde once you go out (because there is no ring)

If you use the retrograde opportunity to go through the previous point (except the starting point), you will never go back to the starting point.

So it won't contribute to the answer, and Dijk is the longest running path.

The hierarchy chart clearly shows that DIS [I] [0/1] indicates arrival point I, and whether the longest path of the retrograde opportunity has been used

When creating a graph, remember to create a reverse graph.

// Luogu-judger-enable-o2 # include <cstdio> # include <algorithm> # include <cmath> # include <cstring> # include <iostream> # include <vector> # include <queue> using namespace STD; typedef long ll; inline int read () {int x = 0, F = 1; char CH = getchar (); while (CH <'0' | ch> '9') {If (CH = '-') F =-1; CH = getchar ();} while (CH> = '0' & Ch <= '9') {x = (x <1) + (x <3) + (CH ^ 48 ); ch = getchar ();} return x * F;} const int n = 2e5 + 7; int n, m; int F IR [N], from [n <1], to [n <1], cntt = 0; inline void add (Int & A, Int & B) {from [++ cntt] = FIR [a]; FIR [a] = cntt; To [cntt] = B ;}// the following Tarjan template int dfn [N], low [N], dfs_clock, St [N], top, be [N], TOT, CNT [N]; void Tarjan (int x) {dfn [x] = low [x] = ++ dfs_clock; ST [++ top] = x; For (INT I = FIR [X]; I; I = from [I]) {Int & V = to [I]; If (! Dfn [v]) Tarjan (V), low [x] = min (low [X], low [v]); else if (! Be [v]) low [x] = min (low [X], dfn [v]);} If (low [x] = dfn [x]) {tot ++; CNT [tot] = 1; while (ST [Top]! = X) {CNT [tot] ++; Be [st [top --] = tot;} be [st [top --] = tot ;}} vector <int> V1 [N], V2 [N]; // For the convenience of directly storing images using vector, V1 is the positive image, V2 is the reverse image void build () // create a graph {for (INT I = 1; I <= N; I ++) for (Int J = FIR [I]; j = from [J]) {Int & V = to [J]; If (be [I] = be [v]) continue; V1 [be [I]. push_back (be [v]); V2 [be [v]. push_back (be [I]) ;}} int dis [N] [2]; struct data {int POs, DIS, P; // current position, passing through distance, retrograde inline bool operator <(const Data & TMP) const {return d Is <TMP. dis ;}}; priority_queue <DATA> q; void Dijk () {memset (DIS, 128, sizeof (DIS); q. push (data) {be [1], 0, 0}); DIS [be [1] [0] = 0; // initial state data X; while (! Q. Empty () {x = Q. Top (); q. Pop (); If (DIS [X. Pos] [x. p]! = X. dis) continue; int Len = V1 [X. pos]. size (); For (INT I = 0; I <Len; I ++) {Int & V = V1 [X. pos] [I]; If (DIS [v] [x. p] <X. DIS + CNT [v]) dis [v] [x. p] = x. DIS + CNT [v], Q. push (data) {v, DIS [v] [x. p], X. p});} If (! X. p) // if it is not retrograde, consider retrograde {Len = v2 [X. pos]. size (); For (INT I = 0; I <Len; I ++) {Int & V = v2 [X. pos] [I]; If (DIS [v] [1] <X. DIS + CNT [v]) dis [v] [1] = x. DIS + CNT [v], Q. push (data) {v, DIS [v] [1], 1}) ;}}} int main () {int A, B; n = read (); M = read (); For (INT I = 1; I <= m; I ++) {A = read (); B = read (); add (, b) ;}for (INT I = 1; I <= N; I ++) if (! Dfn [I]) Tarjan (I); // remember to scale down every point. If the positive graph fails, the reverse graph may go if (TOT = 1) {printf ("% d", n); Return 0 ;}// remember to judge a wave of build (); Dijk (); printf ("% d ", max (DIS [be [1] [0], DIS [be [1] [1]); Return 0 ;}

 

P3119 [usaco 15jan] Grass identification grass cownoisseur

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.