[Noip simulation questions] graph (Tarjan + DFS)

Source: Internet
Author: User

Seems like I am doing something too complicated?

First, the Tarjan point is reduced, and then the DFS line is Qaq.

(I did not say that I was mistakenly called by an SB for half an hour .... Don't think that after the scale-down, the DFS will be able to judge vis with no scrubs ..

BFS practice: subtract the reverse graph, and then enumerate (Greedy) from the largest to the smallest to mark it.

# Include <cstdio> # include <cstring> # include <cmath> # include <string> # include <iostream> # include <algorithm> # include <queue> # include <set> # include <vector> # include <map> using namespace STD; typedef long ll; # define PII pair <int, int> # define mkpii make_pair <int, int> # define PDI pair <double, int> # define mkpdi make_pair <double, int> # define PLI pair <LL, int> # define mkpli make_pair <LL, int> # define rep (I, n) for (INT I = 0; I <(n); ++ I) # define for1 (I, A, n) for (INT I = (a); I <= (n); ++ I) # define for2 (I, A, n) for (INT I = (); I <(n); ++ I) # define for3 (I, A, n) for (INT I = (a); I> = (n); -- I) # define for4 (I, A, n) for (INT I = (a); I> (n); -- I) # define CC (I, A) memset (I, a, sizeof (I) # define read (A) A = getint () # define print (a) printf ("% d", a) # define dbg (X) cout <(# X) <"=" <(x) <Endl # define error (x )(! (X )? Puts ("error"): 0) # define printarr2 (a, B, c) for1 (_, 1, B) {for1 (__, 1, C) cout <A [_] [_]; cout <Endl ;}# define printarr1 (a, B) for1 (_, 1, B) cout <A [_] <'\ T'; cout <endlinline const int getint () {int r = 0, k = 1; char c = getchar (); for (; C <'0' | C> '9'; C = getchar () if (C = '-') k =-1; (; c> = '0' & C <= '9'; C = getchar () r = r * 10 + C-'0'; return K * r ;} inline const int max (const Int & A, const Int & B ){ Return A> B? A: B;} inline const int min (const Int & A, const Int & B) {return a <B? A: B;} const int n = 1e5 + 10; int M, MX [N], ll [N], FF [N], vis [N], TOT, SCC, st [N], Fa [N], top; struct gr {int ihead [N], CNT, N; struct dat {int next, to;} e [N]; void add (int u, int v) {e [++ CNT]. next = ihead [u]; ihead [u] = CNT; E [CNT]. to = V;} void Tarjan () {for1 (I, 1, n) if (! FF [I]) Tarjan (I);} void Tarjan (INT X) {ll [x] = FF [x] = ++ tot; vis [x] = 1; st [++ top] = x; For (INT I = ihead [X]; I; I = E [I]. next) {int y = E [I]. to; If (! FF [y]) {Tarjan (y); LL [x] = min (LL [X], ll [y]);} else if (vis [y] & FF [y] <ll [x]) ll [x] = FF [y];} if (FF [x] = ll [x]) {++ SCC; int y; do {Y = sT [top --]; vis [y] = 0; fa [y] = SCC; MX [SCC] = max (MX [SCC], Y);} while (y! = X) ;}} void DFS () {CC (VIS, 0); for1 (I, 1, n) if (! Vis [I]) DFS (I);} void DFS (int x) {If (vis [x]) return; // I won't talk about it anymore... Vis [x] = 1; for (INT I = ihead [X]; I; I = E [I]. next) {DFS (E [I]. to); MX [x] = max (MX [X], MX [E [I]. to]) ;}}g, G; int main () {read (G. n); read (m); for1 (I, 1, m) {int u = getint (), V = getint (); G. add (u, v); w.g.tar Jan (); G. N = SCC; for1 (x, 1, G. n) for (INT I = G. ihead [X]; I; I = G. E [I]. next) if (Fa [x]! = Fa [G. E [I]. to]) g. add (Fa [X], Fa [G. E [I]. to]); G. DFS (); for1 (I, 1, G. n) printf ("% d", MX [Fa [I]); Return 0 ;}

  

 

 

 

 

[Description]

Returns a directed graph of N points and m edges. For each vertex v, evaluate a (v) to indicate the largest vertex number that can be reached starting from vertex v.

 

[Input format]

1st rows, two integers n, m. In the next m row, there are two integers in each line. The UI, Vi, indicates the edge? UI, Vi ?. Point is numbered with 1, 2,..., n.

 

[Output format]

N integers A (1), a (2),..., a (n ).

 

[Example input]

4 3

1 2

2 4

4 3

[Sample output]

4 4 3 4

[Data Scope]

For 60% of the data, 1 ≤ n, k ≤ 10 ^ 3

For 100% of data, 1 ≤ n, m ≤ 10 ^ 5.

[Noip simulation questions] graph (Tarjan + DFS)

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.