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. 好用的打包工具webpack

    <什么是webpack> webpack是一个模块打包器,任何静态资源(js.css.图片等)都可以视作模块,然后模块之间也可以相互依赖,通过webpack对模块进行处理后,可以打包成我们 ...

  2. RabbitMq--2--安装

    简单说下个人的理解,mq就是一个消息代理,负责异步消息转发,可以很大程度缓解服务器压力,并且防止服务器宕机影响业务等. 安装: 环境:centos7 1).首先需要安装erlang #wget htt ...

  3. Hibernate4教程五:事务和并发

    Hibernate本身没有事务的实现 Hibernate 直接使用 JDBC 连接和 JTA 资源,不添加任何附加锁定行为.也就是说你在Hibernate里面使用的事务要么是JDBC的事务,要么是JT ...

  4. python2和python3编码

    python2编码 unicode:unicode 你好 u'\u4f60\u597d' | | | | encode('utf8')| |decode('utf8') encode('gbk')| ...

  5. 2019-9-2-win10-uwp-九幽图床

    title author date CreateTime categories win10 uwp 九幽图床 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 1 ...

  6. Hdu-3333 Turning Tree (离线树状数组/线段树)

    Hdu-3333 Turning Tree 题目大意:先给出n个数字.面对q个询问区间,输出这个区间不同数的和. 题解:这道题有多重解法.我另一篇博客写了分块的解法  HDU-3333 Turing ...

  7. HttpClient类详解

    文章链接:https://blog.csdn.net/justry_deng/article/details/81042379 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了 ...

  8. 2PC和3PC

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484077.html 2PC 是计算机网络尤其是在数据库领域内,为了使基于分布式系统架构下的所有节点 ...

  9. isinstance 的用法 判断这个数据是否属于这个类型

  10. bzoj 2435

    http://www.lydsy.com/JudgeOnline/problem.php?id=2435 noi 你为什么那么diao, 这种世纪水题刷一道少一道啊... 我原来还以为是两边的联通块大 ...