ZOJ 3795 Grouping (Tarjan contraction point + DAG), zoj3795

Source: Internet
Author: User

ZOJ 3795 Grouping (Tarjan contraction point + DAG), zoj3795

Suppose there areNPeople in ZJU, whose ages are unknown. We have some messages about them.I-Th message shows that the age of personSiIs not smaller than the age of personTi. Now we need to divide all theseNPeople into several groups. one's age shouldn't be compared with each other in the same group, directly or indirectly. and everyone shoshould be assigned to one and only one group. the task is to calculate the minimum number of groups that meet the requirement.

Input

There are multiple test cases. For each test case: The first line contains two integersN(1 ≤N≤ 100000 ),M(1 ≤M≤ 300000 ),NIs the number of people, andMIs the number of messages. Then followedMLines, each line contain two integers si and ti. There is a blank line between every two cases. Process to the end of input.

Output

For each the case, print the minimum number of groups that meet the requirement one line.

Sample Input
4 41 21 32 43 4
Sample Output
3
Hint

Set1 = {1}, set2 = {2, 3}, set3 = {4}



The longest path of the memory-based search:
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h> typedef long LL; using namespace std; # define REPF (I, a, B) for (int I = a; I <= B; ++ I) # define REP (I, n) for (int I = 0; I <n; ++ I) # define CLEAR (a, x) memset (a, x, sizeof) const int maxn = 100100; const int maxm = 300100; struct node {int u, v; int next;} e [maxm], e2 [maxm]; int head [maxn], cn TE, cntF; int DFN [maxn], low [maxn], h [maxn]; int s [maxm], top, dex, cnt; int belong [maxn], instack [maxn]; int dp [maxn], num [maxn]; int n, m; void init () {top = cntE = cntF = 0; dex = cnt = 0; CLEAR (DFN, 0); CLEAR (head,-1); CLEAR (instack, 0); CLEAR (num, 0 ); // fuck num has not cleared 0wa for 2 hours} void addedge (int u, int v) {e [cntE]. u = u; e [cntE]. v = v; e [cntE]. next = head [u]; head [u] = cntE ++;} void Tarjan (int u) {DFN [u] = low [u] = ++ dex; instack [u] = 1; s [top ++] = U; for (int I = head [u]; I! =-1; I = e [I]. next) {int v = e [I]. v; if (! DFN [v]) {Tarjan (v); low [u] = min (low [u], low [v]);} else if (instack [v]) low [u] = min (low [u], DFN [v]);} int v; if (DFN [u] = low [u]) {cnt ++; do {v = s [-- top]; belong [v] = cnt; instack [v] = 0;} while (u! = V) ;}} int dfs (int x) {if (dp [x]) return dp [x]; dp [x] = num [x]; for (int I = h [x]; I! =-1; I = e2 [I]. next) dp [x] = max (dp [x], dfs (e2 [I]. v) + num [x]); return dp [x];} void work () {REPF (I, 1, n) if (! DFN [I]) Tarjan (I); REPF (I, 1, n) num [belong [I] ++; CLEAR (h,-1); CLEAR (dp, 0); REPF (k, 1, n) {for (int I = head [k]; I! =-1; I = e [I]. next) {int v = e [I]. v; if (belong [k]! = Belong [v]) {e2 [cntF]. u = belong [k]; e2 [cntF]. v = belong [v]; e2 [cntF]. next = h [belong [k]; h [belong [k] = cntF ++ ;}} int ans = 0; // cout <"2333" <cnt <endl; REPF (I, 1, cnt) ans = max (ans, dfs (I )); printf ("% d \ n", ans);} int main () {int u, v; while (~ Scanf ("% d", & n, & m) {init (); for (int I = 0; I <m; I ++) {scanf ("% d", & u, & v); addedge (u, v) ;}work () ;}return 0 ;}





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.