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把强联通分量缩成一个点,点权就代表该强联通分量中点的数目,再求最长路
#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);
}
}
zoj 3795 Grouping tarjan缩点 + DGA上的最长路的更多相关文章
- Grouping ZOJ - 3795 (tarjan缩点求最长路)
题目链接:https://cn.vjudge.net/problem/ZOJ-3795 题目大意:给你n个人,m个关系, 让你对这个n个人进行分组,要求:尽可能的分组最少,然后每个组里面的人都没有关系 ...
- ZOJ 3795 Grouping (强连通缩点+DP最长路)
<题目链接> 题目大意: n个人,m条关系,每条关系a >= b,说明a,b之间是可比较的,如果还有b >= c,则说明b,c之间,a,c之间都是可以比较的.问至少需要多少个集 ...
- ZOJ 3795 Grouping(Tarjan收缩点+DAG)
Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. The i-t ...
- NYOJ_矩形嵌套(DAG上的最长路 + 经典dp)
本题大意:给定多个矩形的长和宽,让你判断最多能有几个矩形可以嵌套在一起,嵌套的条件为长和宽分别都小于另一个矩形的长和宽. 本题思路:其实这道题和之前做过的一道模版题数字三角形很相似,大体思路都一致,这 ...
- UVa 10285 最长的滑雪路径(DAG上的最长路)
https://vjudge.net/problem/UVA-10285 题意: 在一个R*C的整数矩阵上找一条高度严格递减的最长路.起点任意,但每次只能沿着上下左右4个方向之一走一格,并且不能走出矩 ...
- HDU 3249 Test for job (有向无环图上的最长路,DP)
解题思路: 求有向无环图上的最长路.简单的动态规划 #include <iostream> #include <cstring> #include <cstdlib ...
- Vulnerable Kerbals CodeForces - 772C【拓展欧几里得建图+DAG上求最长路】
根据拓展欧几里得对于同余方程 $ax+by=c$ ,有解的条件是 $(a,b)|c$. 那么对于构造的序列的数,前一个数 $a$ 和后一个数 $b$ ,应该满足 $a*x=b(mod m)$ 即 $ ...
- ZOJ 3795 Grouping(scc+最长路)
Grouping Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose there are N people in ZJU, whose ...
- ZOJ 3795 Grouping 强连通分量-tarjan
一开始我还天真的一遍DFS求出最长链以为就可以了 不过发现存在有向环,即强连通分量SCC,有向环里的每个点都是可比的,都要分别给个集合才行,最后应该把这些强连通分量缩成一个点,最后保证图里是 有向无环 ...
随机推荐
- 如何获得images.xcassets 中图片的路径?
UIImage加载图片的方式以及Images.xcassets对于加载方法的影响 重点: Images.xcassets中的图片资源只能通过imageNamed:方法加载,通过NSBundle的pat ...
- iOS -类目,延展,协议
1.类目 类目就是为已存在的类添加新的方法.但是不能添加实例变量.比如系统的类,我们看不到他的.m文件,所以没有办法用直接添加方法的方式去实现. @interface NSMutableArray ( ...
- Jquery的鼠标移动上去显示div,鼠标离开的时候隐藏div效果
有时候我们需要这个效果:当鼠标放上去的时候显示一个div,当鼠标移走的时候就将div隐藏了.代码如下,记得引入Jquyer库.(当鼠标移动到id=menu的div上的时候,显示id=list的div, ...
- 单击双击手势(UITapGestureRecognizer)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- printf 与c的基本类型之间的关系
型 書式 注意事項 ssize_t %zd size_t %zu intmax_t %jd uintmax_t %ju ptrdiff_t %t signed char %hhd unsigned c ...
- 解决VS2010 C++ DLL不能断点调试的问题
问题产生的过程是这样的,向exe项目(CSharp)中添加dll工程(c++开发)的引用,并将引用工程的属性“Link Library Dependencies”的值设为true,这样,在不加入lib ...
- Codeforces Round #370 (Div. 2)(简单逻辑,比较水)
C. Memory and De-Evolution time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Android中shell命令语句
最近学习了Android中碰到了shell命令,故收集终结了一下 Ccat zdd 浏览文件zdd的内容cat zdd1 zdd2 浏览多个文件的内容cat -n zdd浏览文件zdd的内容并显示行号 ...
- 四、优化及调试--网站优化--Yahoo军规中
8.避免使用CSS表达式(避免在CSS中使用Expressions) 什么是CSS表达式:是用来把CSS属性和JavaScript关联起来.
- C/C++学习笔记---高地址、低地址、大段字节序、小段字节序
字节顺序是指占内存多于一个字节类型的数据在内存中的存放顺序,通常有小端.大端两种字节顺序. 小端字节序指低字节数据存放在内存低地址处,高字节数据存放在内存高地址处: 大端字节序是高字节数据存放在低地址 ...