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,有向环里的每个点都是可比的,都要分别给个集合才行,最后应该把这些强连通分量缩成一个点,最后保证图里是 有向无环 ...
随机推荐
- 靶形数独(codevs 1174)
1174 靶形数独 2009年NOIP全国联赛提高组 时间限制: 4 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Descri ...
- C --gettimeofday===获取某个程序经历的时间
#include <stdio.h> #include <sys/time.h> #include <time.h> int gettimeofday(struct ...
- 根据复选框checkbox的选中状态来打开或关闭隐藏层
HTML: <input type="checkbox" id="check-expert"> <div id="expert&q ...
- 关于Linux环境变量
查看全局变量: printenv 查看单个环境变量的值可以用echo命令,必须在环境变量的名称前放一个$符号 如:
- Index on DB2 for z/OS: DB2 for z/OS 的索引
可以创建在任何表上的索引: Unique Index:An index that ensures that the value in a particular column or set of col ...
- NYOJ之奇偶数分离
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAscAAAJ8CAIAAACdmZvPAAAgAElEQVR4nO3dPVLjStsG4G8T5CyEFC
- CLR via C#(05)- 访问限定、数据成员
今天跟大家分享一下关于访问限定和数据成员的知识.主要包括以下两点: Abstract, sealed, virtual, new, override怎么用? Const 和 readonly好像都表示 ...
- Delphi中线程类TThread实现多线程编程2---事件、临界区、Synchronize、WaitFor……
接着上文介绍TThread. 现在开始说明 Synchronize和WaitFor 但是在介绍这两个函数之前,需要先介绍另外两个线程同步技术:事件和临界区 事件(Event) 事件(Event)与De ...
- HTML5应用之文件拖拽上传
使用HTML5的文件API,可以将操作系统中的文件拖放到浏览器的指定区域,实现文件上传到服务器.本文将结合实例讲解HTML5+jQuery+PHP实现拖拽上传图片的过程,来看下HTML5的魅力吧. H ...
- tornado使用(Mac)
安装需求 Tornado 在 Python 2.5, 2.6, 2.7 中都经过了测试.要使用 Tornado 的所有功能,你需要安装 PycURL (7.18.2 或更高版本) 以及 simplej ...