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,有向环里的每个点都是可比的,都要分别给个集合才行,最后应该把这些强连通分量缩成一个点,最后保证图里是 有向无环 ...
随机推荐
- 【leetcode】Jump Game I & II (hard)
Jump Game (middle) Given an array of non-negative integers, you are initially positioned at the firs ...
- 【python】多进程学习
来源:廖雪峰 讲解看来源吧 把例子记一下 1.用fork创建进程 import os print "Process (%s) start..." % os.getpid() pid ...
- HTML标记语法之表格元素
语法与语义: <table>和</table>定义表格的开始和结束 <thead>和</thead>定义表格头部的开始和结束 <tbody> ...
- August 21st 2016 Week 35th Sunday
I figure life is a gift and I don't intend on wasting it. 我觉得生命是一份礼物,我不想浪费它. Tonight when I was runn ...
- python基础——匿名函数
python基础——匿名函数 当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中,对匿名函数提供了有限支持.还是以map()函数为例,计算f(x)=x2时 ...
- nfs server的配置 Starting NFS daemon: [FAILED]
总结了一下是nfs server的制作过程:nfs(Network File System)其实就是说,这个机器的硬盘不够了,我要把文件放到别的服务器上去,服务器端的配置如下:首先(1)确保你的机器上 ...
- hdu 1249 三角形
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1249 part=3*s*(s-1)+2 #include<stdio.h> #includ ...
- JavaScript基础——理解变量作用域
一旦你开始在JavaScript应用程序中添加条件.函数和循环,就需要理解变量作用域.变量作用域规定了如何确定正在执行的代码行上的一个特定变量名的值. JavaScript允许你既定义全局版本又定义局 ...
- MyEclipse破解(MEGen.java)
步骤: 1.将MEGen.java粘贴到任意web项目下,运行结果如下: 2.输入注册名:如sun,得到注册码: 3.Window >> Preference >> S ...
- Android 反编译 -- apktool、dex2jar、jd-gui
原文:http://blog.csdn.net/vipzjyno1/article/details/21039349/ apktool 最新官网: http://ibotpeaches.github. ...