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}



记忆化搜索最长路径:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a ) const int maxn=100100;
const int maxm=300100;
struct node{
int u,v;
int next;
}e[maxm],e2[maxm];
int head[maxn],cntE,cntF;
int DFN[maxn],low[maxn],h[maxn];
int s[maxm],top,dex,cnt;
int belong[maxn],instack[maxn];
int dp[maxn],num[maxn];
int n,m;
void init()
{
top=cntE=cntF=0;
dex=cnt=0;
CLEAR(DFN,0);
CLEAR(head,-1);
CLEAR(instack,0);
CLEAR(num,0);//fuck num没清0wa了2小时
}
void addedge(int u,int v)
{
e[cntE].u=u;e[cntE].v=v;
e[cntE].next=head[u];
head[u]=cntE++;
}
void Tarjan(int u)
{
DFN[u]=low[u]=++dex;
instack[u]=1;
s[top++]=u;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
if(!DFN[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],DFN[v]);
}
int v;
if(DFN[u]==low[u])
{
cnt++;
do{
v=s[--top];
belong[v]=cnt;
instack[v]=0;
}while(u!=v);
}
}
int dfs(int x)
{
if(dp[x]) return dp[x];
dp[x]=num[x];
for(int i=h[x];i!=-1;i=e2[i].next)
dp[x]=max(dp[x],dfs(e2[i].v)+num[x]);
return dp[x];
}
void work()
{
REPF(i,1,n)
if(!DFN[i]) Tarjan(i);
REPF(i,1,n)
num[belong[i]]++;
CLEAR(h,-1);
CLEAR(dp,0);
REPF(k,1,n)
{
for(int i=head[k];i!=-1;i=e[i].next)
{
int v=e[i].v;
if(belong[k]!=belong[v])
{
e2[cntF].u=belong[k];
e2[cntF].v=belong[v];
e2[cntF].next=h[belong[k]];
h[belong[k]]=cntF++;
}
}
}
int ans=0;
// cout<<"2333 "<<cnt<<endl;
REPF(i,1,cnt)
ans=max(ans,dfs(i));
printf("%d\n",ans);
}
int main()
{
int u,v;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
}
work();
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

ZOJ 3795 Grouping(Tarjan收缩点+DAG)的更多相关文章

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

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

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

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

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

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

  4. ZOJ 3795 Grouping(scc+最长路)

    Grouping Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose there are N people in ZJU, whose ...

  5. ZOJ 3795 Grouping

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

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

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

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

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

  8. 2014 Super Training #8 G Grouping --Tarjan求强连通分量

    原题:ZOJ 3795 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3795 题目大意:给定一个有向图,要求把点分为k个集 ...

  9. 「BZOJ1924」「SDOI2010」 所驼门王的宝藏 tarjan + dp(DAG 最长路)

    「BZOJ1924」[SDOI2010] 所驼门王的宝藏 tarjan + dp(DAG 最长路) -------------------------------------------------- ...

随机推荐

  1. Sesson禁止使用Cookie

    于WebRoot文件夹MEAT-INF新建文件夹context.xml 增加<context path="XXX" cookies='false"> 更改To ...

  2. 在自己的base脚本中实现自动补全

    在90年代Linux和DOS共存的年代里,Linux的Shell们有一个最微不足道但也最实用的小功能,就是命令自动补全.而DOS那个笨蛋一直到死都没学会什么叫易用. Linux的这个微不足道的小传统一 ...

  3. 重新启动IIS服务的方法

    WINDOWS提供WEB服务的IIS有时候会出现訪问过大导致站点打不开,这时重新启动IIS是最好的选择. 1.界面操作 打开"控制面板"->"管理工具"- ...

  4. struts2集成fckeditor(来自大型门户网站是这样练成的一书)

  5. C#开发Linux守护进程

    用C#开发Linux守护进程   Linux守护进程(Daemon)是Linux的后台服务进程,它脱离了与控制终端的关联,直接由Linux init进程管理其生命周期,即使你关闭了控制台,daemon ...

  6. Windows Phone开发(36):动画之DoubleAnimation

    原文:Windows Phone开发(36):动画之DoubleAnimation 从本节开始,我们将围绕一个有趣的话题展开讨论--动画. 看到动画一词,你一定想到Flash,毕竟WP应用的一个很重要 ...

  7. python几道简单的算法题

    最近看了python的语法,但是总感觉不知道怎么使用它,还是先来敲敲一些简单的程序吧. 1.题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位.十 ...

  8. RTF 格式 说明

    摘要: 本文对RTF文件格式进行分析研究,对RTF文件结构及特性进行了阐述,并分别列举了几个有用性的样例进行具体分析, 终于通过VB程序代码实现了一个RTF书写器(不具有所见即所得特性).本文对软件开 ...

  9. lambda left join .DefaultIfEmpty

    我们知道lambda表达式在Linq to sql 和 Entity framework 中使用join函数可以实现inner join,那么怎么才能在lambda表达式中实现left join呢?秘 ...

  10. oracle转mysql总结(转)

    ares-sdk初始开发测试使用的是oracle数据库,由于宁波通商的特殊需要,必须把数据库环境从oracle转向mysql. 现对转换过程中出现的问题及经验总结如下: 主键生成策略 创建一个专门记录 ...