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,有向环里的每个点都是可比的,都要分别给个集合才行,最后应该把这些强连通分量缩成一个点,最后保证图里是 有向无环 ...
随机推荐
- 解决win10无法完成更新 正在撤销更改
删除Windows 更新缓存文件按Windows+X,选择“命令提示符(管理员)”:输入:net stop wuauserv,回车(此处会提醒服务停止):输入: %windir%\SoftwareDi ...
- IIS6与IIS7中如何设置文件过期
在IIS6中:一. 打开IIS管理器 二. 选中要设置的网站单击属性,打开站点属性菜单 三. 单击HTTP头选项卡 四. 单击 启用内容过期 如:设置30分钟后过期,此时间段后过期项中填30,单位选择 ...
- TCP UDP 协议的区别和联系
TCP(Transmission Control Protocol,传输控制协议)是基于连接的协议,也就是说,在正式收发数据前,必须和对方建立可靠的连接.一个TCP连接必须要经过三次“对话”才能建立起 ...
- MyBatis mapper文件中的变量引用方式#{}与${}的差别
MyBatis mapper文件中的变量引用方式#{}与${}的差别 #{},和 ${}传参的区别如下:使用#传入参数是,sql语句解析是会加上"",当成字符串来解析,这样相比于$ ...
- NYOJ题目1045看美女
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAK5CAIAAADCdSR7AAAgAElEQVR4nO3dP3Lbuv434HcT7r2Q1F
- 电脑没有关机可能出现发博文dns异常(write)
现在是把装系统的技巧基本掌握,其实弄了这么多次还是有一个突破点,就是安装系统win7选择cd的iso是要ide模式.
- 算法系列:XXX
转载自http://www.cnblogs.com/skynet/p/3372855.html 这次分享的宗旨是——让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择 ...
- POJ2778 DNA Sequence(AC自动机 矩阵)
先使用AC自动机求得状态转移关系,再建立矩阵,mat[i][j]表示一步可从i到j且i,j节点均非终止字符的方案数,则此矩阵的n次方表示n步从i,到j的方法数. #include<cstdio& ...
- <转>Java 常用排序算法小记
排序算法很多地方都会用到,近期又重新看了一遍算法,并自己简单地实现了一遍,特此记录下来,为以后复习留点材料. 废话不多说,下面逐一看看经典的排序算法: 1. 选择排序 选择排序的基本思想是遍历数组的过 ...
- WPF 多语言实现
很多国际化的程序都提供了多语言的选项,这样方便不同国家的使用者更方便的使用软件.这篇博客中将介绍在WPF中实现多语言的方式. 方式一,使用WPF动态资源的方式实现.先简单介绍下StaticResour ...