Problem Description:
Suppose there N is people in Zju, whose ages is unknown. We have some messages about them. The-th message shows that the age of person is not a smaller than the age of person i si ti . Now we need to divide all these N people into several groups. One's age shouldn ' t is compared with the same group, directly or indirectly. And everyone should be assigned to one and only one group. The task is to calculate the minimum number of groups that meet the requirement.
Input:
There is multiple test cases. For each test case:the first line contains-integers N (1≤ N ≤100000), M (1≤ M ≤300000), is the N num ber of people, and is the number of M messages. Then followed M by lines, each line contain the integers si and TI. There is a blank line between every and cases. Process to the end of input.
Output:
For each case, print the minimum number of groups that meet the requirement one line.
Sample Input:
4 4
0 S
1 3
2 4
3 4
Sample Output:
3
Problem Solving Ideas:
The strongly connected component shrinks, and then DP asks for the longest.
#include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include < cstring> #include <vector> #include <queue> #include <stack>using namespace Std;const int maxn = 100000 + 10;vector<int>g[maxn];vector<int>rg[maxn];int Vis[maxn];int Pre[MAXN], Lowlink[MAXN], sccno[ maxn];//points corresponding to the strong Unicom component number int dfs_clock, scc_cnt;//the number of strong unicom components, numbered 1~scc_cntint num[maxn];//the number of points contained in each strong Unicom stack<int>s ; void Dfs (int u) {pre[u] = lowlink[u] = ++dfs_clock; S.push (U); for (int i=0;i<g[u].size (); i++) {int v = g[u][i]; if (!pre[v]) {DFS (v); Lowlink[u] = min (Lowlink[u], lowlink[v]); } else if (!sccno[v]) {Lowlink[u] = min (Lowlink[u], pre[v]); }} if (lowlink[u] = = Pre[u]) {scc_cnt++; for (;;) {int x = S.top (); S.pop (); SCCNO[X] = scc_cnt; num[scc_cnt]++; if (x = = u) BREak }}}void FIND_SCC (int n) {dfs_clock = scc_cnt = 0; memset (sccno, 0, sizeof (SCCNO)); memset (pre, 0, sizeof (pre)); memset (num, 0, sizeof (num)); for (int i=0;i<n;i++) {if (!pre[i]) DFS (i); }}int dp[maxn];int dp (int u) {if (Dp[u]! =-1) return dp[u]; Dp[u] = Num[u]; for (int i=0;i<rg[u].size (); i++) {int v = rg[u][i]; Dp[u] = max (Dp[u], Num[u] + DP (v)); } return dp[u];} int main () {int n, m; while (scanf ("%d%d", &n, &m)!=eof) {for (int i=0;i<n;i++) g[i].clear (); int u, v; for (int i=0;i<m;i++) {scanf ("%d%d", &u, &v); u--; v--; G[u].push_back (v); } FIND_SCC (n); for (int i=1;i<=scc_cnt;i++) rg[i].clear (); for (int i=0;i<n;i++) {int sz = g[i].size (); for (int j=0;j<sz;j++) {if (Sccno[i]! = Sccno[g[i][j]]) { Rg[sccno[i]].push_back (Sccno[g[i][j]); }}} int ans = 0; Memset (DP,-1, sizeof (DP)); for (int i=1;i<=scc_cnt;i++) ans = max (ans, DP (i)); printf ("%d\n", ans); } return 0;}
ZOJ 3795 Grouping (Strong Unicom component + pinch + Dp)