Grouping


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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}

题目给出N个点 , M条边, 然后问一些相关联的人(存在一条路u -> v)不能分在同一组 , 最少分的组的数目 。

这样肯定要求一个  scc ..  那么在同一个scc里面的人(假设有w个人)都要拆分成w组 , 那么我们以这个w作为

scc图的点权 。 目的是所有 scc图(DAG) 的最长路所构成的权 ,取最大。

最长路用一个记忆化搜索即可 , 训练的时候不会这么用 , 卡蒙了~~ 。

#include <bits/stdc++.h>
using namespace std;
const int N = ; int pre[N] , lowlink[N] , sccno[N] , dfs1_clock , scc_cnt ;
stack<int>st;
int n , m ;
int w[N] , dep_w[N];
bool vis[N];
int in[N];
int dp[N]; int eh[N] , et[N<<] ,nxt[N<<] , tot ; void addedge( int u , int v )
{
et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot ++ ;
} struct node
{
int u , v ;
}e[N*]; void dfs1( int u )
{
pre[u] = lowlink[u] = ++dfs1_clock;
st.push(u);
for( int i = eh[u]; ~i ; i = nxt[i] ){
int v = et[i] ;
if(!pre[v]){
dfs1(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if( !sccno[v] ){
lowlink[u] = min( lowlink[u] , pre[v] );
}
}
if( lowlink[u] == pre[u] )
{
scc_cnt++;
for(;;){
int x = st.top(); st.pop();
sccno[x] = scc_cnt ;
if( x == u ) break;
}
}
} void find_scc()
{
dfs1_clock = scc_cnt = ;
memset ( pre , , sizeof pre ) ;
memset ( sccno , , sizeof sccno ) ;
while( !st.empty() ) st.pop(); for( int i = ; i <= n ; ++i ){
if( !pre[i] )dfs1(i);
}
} void init()
{ tot= ;
memset( eh , - ,sizeof eh );
memset( dp , - ,sizeof dp );
memset( w , ,sizeof w );
memset( vis ,false , sizeof vis );
memset( in , , sizeof in );
} void dfs( int u )
{
if( dp[u] == - ){
int sum_now = w[u] ;
for( int i = eh[u] ; ~i ; i = nxt[i] ){
int v = et[i];
dfs(v);
sum_now = max( sum_now , w[u] + dp[v] );
}
dp[u] = sum_now;
}
} void run()
{
int u , v ;
init();
for( int i = ; i < m ;++i ){
scanf("%d%d",&e[i].u,&e[i].v);
addedge( e[i].u , e[i].v );
}
find_scc();
tot = ;
memset(eh , - , sizeof eh ); for( int i = ;i < m ;++i ){
if( sccno[e[i].u] != sccno[e[i].v] ){
addedge( sccno[e[i].u] , sccno[e[i].v] );
in[ sccno[e[i].v] ] ++;
}
} for( int i = ; i <= n ; ++i )w[ sccno[i] ] ++ ; int ans = ;
for( int i = ; i <= scc_cnt ; ++i ) if( !in[i] ) addedge(,i);
dfs();
for( int i = ; i <= scc_cnt ; ++i ) ans = max( ans , dp[i] );
printf("%d\n",ans);
} int main()
{
#ifdef LOCAL
freopen("in","r",stdin);
#endif
ios::sync_with_stdio();
while( ~scanf("%d%d",&n,&m))run();
}

ZOJ 3795 Grouping(scc+最长路)的更多相关文章

  1. ZOJ 3795 Grouping 求最长链序列露点拓扑

    意甲冠军:特定n积分.m向边条. 该点被划分成多个集合随机的每个集合,使得2问题的关键是无法访问(集合只能容纳一个点) 问至少需要被分成几个集合. 假设没有戒指,接着这个话题正在寻求产业链最长的一个有 ...

  2. zoj 3795 Grouping tarjan缩点 + DGA上的最长路

    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practic ...

  3. ZOJ 3795 Grouping (强连通缩点+DP最长路)

    <题目链接> 题目大意: n个人,m条关系,每条关系a >= b,说明a,b之间是可比较的,如果还有b >= c,则说明b,c之间,a,c之间都是可以比较的.问至少需要多少个集 ...

  4. zoj 3088 Easter Holidays(最长路+最短路+打印路径)

    Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...

  5. ZOJ 3795 Grouping 强连通分量-tarjan

    一开始我还天真的一遍DFS求出最长链以为就可以了 不过发现存在有向环,即强连通分量SCC,有向环里的每个点都是可比的,都要分别给个集合才行,最后应该把这些强连通分量缩成一个点,最后保证图里是 有向无环 ...

  6. ZOJ 3795 Grouping

    大致题意是给n个人和m组关系,每组关系都是两个人s和t,表示s年龄不小于t的年龄,然后让你把这n个人分组,使得任何一个组里面的任意两人都不能直接或间接的得出这两个人的年龄大小关系. 思路:根据给出的关 ...

  7. 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 ...

  8. Grouping ZOJ - 3795 (tarjan缩点求最长路)

    题目链接:https://cn.vjudge.net/problem/ZOJ-3795 题目大意:给你n个人,m个关系, 让你对这个n个人进行分组,要求:尽可能的分组最少,然后每个组里面的人都没有关系 ...

  9. ZOJ 3795:Grouping(缩点+最长路)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5303 题意:有n个人m条边,每条边有一个u,v,代表u的年龄大于等于v,现在要 ...

随机推荐

  1. Fiddler之iOS手机抓包代理设置

    一.前置步骤:官网下载并安装好 二.设置iOS手机代理到windows电脑 1.打开Fiddler,点击上方Tools,进入Options,选择HTTPS,按下图设置 2.fiddler默认监听端口8 ...

  2. C++中的内联函数分析

    1,本节课学习 C++ 中才引入的新的概念,内联函数: 2,常量与宏回顾: 1,C++ 中的 const 常量可以替代宏常数定义,如: 1,const int A = 3; <==> #d ...

  3. JavaWeb的几种设计模式

    原文:http://blog.csdn.net/yue7603835/article/details/7479855 Java Web开发方案有多种可供选择,这里列举一些经典的开发模式进行横向比较,为 ...

  4. 在线px转换rem工具

    今天推荐一个在线工具,在线px转换rem工具 只要输入1rem = 多少px即可在线转换 和cssrem插件差不多的功能   rem在线转换工具: http://www.ofmonkey.com/fr ...

  5. Pandas之loc\iloc\ix

    ---------------------------------------------------------------------------------------------------- ...

  6. linux-redis-install

    安装redis3.2.9 wget cd make 编译完成后,将redis-cli redis-server redis-conf redis-benchmark配置文件复制到usr/redis文件 ...

  7. 小白jquery横向菜单弹出菜单制作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 第06章 AOP XML

    第06章 以XML方式配置切面 1.概述 除了使用AspectJ注解声明切面,Spring也支持在bean配置文件中声明切面.这种声明是通过aop名称空间中的XML元素完成的. 正常情况下,基于注解的 ...

  9. stat函数学习

    stat函数组 前面介绍的通过ls命令查看到的文件信息,都可以使用stat函数组提取出来• stat函数组– 使用命令man stat查看相关文档• 函数int stat(const char *pa ...

  10. memset函数及原补反码关系

    memset函数及原补反码关系 计算机存储的是补码 几组常用的memset函数初始化值 10000000 128 10000000 10000000 10000000 10000000 -213906 ...