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. java并发编程之美-阅读记录7

    java并发包中的并发队列 7.1ConcurrentLinkedQueue 线程安全的无界非阻塞队列(非阻塞队列使用CAS非阻塞算法实现),其底层数组使用单向列表实现,对于出队和入队操作使用CAS非 ...

  2. hibernate保存数据到mysql时的中文乱码问题

    因为hibernate底层使用的是jdbc的技术,所以我参考了别人使用jdbc保存数据到mysql里面时解决乱码问题的方法! 首先要告诉数据库要插入的字符串的字符集,mysql 默认使用的字符集是 l ...

  3. 分支结构case 语句举例

  4. 五、bootstrap-Table Treegrid

    一.bootstrap-Table Treegrid <!DOCTYPE HTML> <html lang="zh-cn"> <head> &l ...

  5. HDU-3333 Turing Tree 分块求区间不同数和

    HDU-3333 Turning Tree 题目大意:先给出n个数字.面对q个询问区间,输出这个区间不同数的和. 题解:这道题有几种解法.这里讲一下用分块解决的方法.( 离线树状数组解法看这里 Hdu ...

  6. JavaSE---多线程---线程的控制

    1.Java提供了一些工具方法,可以便捷控制线程的执行: 1.1 join Thread提供了让一个线程等待另一个线程执行完成的方法:join: 当某个程序的执行流中调用其他线程的join方法,该线程 ...

  7. fn:indexOf()详解(jsp中JSTL标签库)

    fn:indexOf()函数返回一个字符串中指定子串的位置. 语法 fn:indexOf()函数的语法如下: ${fn:indexOf(<原始字符串>,<子字符串>)} 实例演 ...

  8. docker 运行springboot jar包

    1.将jar包移至自定义的/usr/jar目录下; 2.在/usr/jar目录下创建Dockerfile文件 文件如下: #FROM命令定义构建镜像的基础镜像,该条必须是dockerfile的首个命令 ...

  9. Linux 查看文件夹大小(排序)

    du -s * | sort -nr (-n是按数字大小排序,不能加上参数h)

  10. 【NOIP2019模拟2019.11.13】旅行 && GDKOI2018 还念(二分答案+dij)

    Description: 题解: 显然满足二分性. 并且每一条边要不选l要不选r. 二分的那条链肯定要选l. 考虑有两个人在走最短路,一个人一开始必须走二分的那条链,要求第一个人走的比第二个人快. 安 ...