zoj 3795 Grouping tarjan缩点 + DGA上的最长路
Description
Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. The i-th message shows that the age of person si is not smaller than the age of person ti. Now we need to divide all these N people into several groups. One's age shouldn't be compared with each other in 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 are multiple test cases. For each test case: The first line contains two integers N(1≤ N≤ 100000), M(1≤ M≤ 300000), N is the number of people, and M is is the number of messages. Then followed by M lines, 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 4 1 2 1 3 2 4 3 4
Sample Output
3
Hint
set1= {1}, set2= {2, 3}, set3= {4}
思路:题目给出a, b 是指a不小于b, 即a可以等于b, a到b拉一条有向边,多个相等的时候会成环,直接求最长路则会死循环, 所以应该先用tarjan把强联通分量缩成一个点,点权就代表该强联通分量中点的数目,再求最长路
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <map> #include <stack> #include <vector> using namespace std; const int N = 211111; const int E = 333333; vector< int > G[N]; int pre[N], low[N], sccno[N], num[N], dfs_ck, scc_cnt; stack< int > s; void dfs( int u) { pre[u] = low[u] = ++dfs_ck; s.push(u); for ( int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (!pre[v]) { dfs(v); low[u] = min(low[u], low[v]); } else if (!sccno[v]) { low[u] = min(low[u], pre[v]); } } if (low[u] == pre[u]) { scc_cnt++; for (;;) { num[scc_cnt]++; int x = s.top(); s.pop(); sccno[x] = scc_cnt; if (x == u) break ; } } } void find_scc( int n) { dfs_ck = scc_cnt = 0; memset (sccno, 0, sizeof sccno); memset (pre, 0, sizeof pre); memset (num, 0, sizeof num); for ( int i = 1; i <= n; ++i) if (!pre[i]) dfs(i); } int dp[N]; vector< int > G2[N]; void init( int n) { for ( int i = 0; i <= n; ++i) G[i].clear(); for ( int i = 0; i <= n; ++i) G2[i].clear(); while (!s.empty()) s.pop(); } int get( int u) { int & res = dp[u]; if (res != -1) return res; res = num[ u ]; // 一开始写成 res = num[ sccno[u] ], wa, 建图的时候已经是通过scc编号来建图了 int sx = G2[u].size(); for ( int j = 0; j < sx; ++j) res = max(res, get(G2[u][j]) + num[ u ]); return res; } int main() { int n, m; while (~ scanf ( "%d%d" , &n, &m)) { int u, v; init(n); for ( int i = 0; i < m; ++i) { scanf ( "%d%d" , &u, &v); G[u].push_back(v); } find_scc(n); // printf("%d\n", scc_cnt); // for(int i = 1; i <= n; ++i) printf("%d %d\n", i, sccno[i]); for ( int i = 1; i <= n; ++i) { int sx = G[i].size(); for ( int j = 0; j < sx; ++j) { int v = G[i][j]; if (sccno[i] != sccno[v]) { G2[ sccno[i] ].push_back(sccno[v]); //通过scc编号来建图 // printf("%d %d\n", sccno[i], sccno[v]); } } } memset (dp, -1, sizeof dp); int ans = 0; for ( int i = 1; i <= scc_cnt; ++i) ans = max(ans, get(i)); printf ( "%d\n" , ans); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧