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. Linux查看用户数、登录用户

    如果是系统中全部只要默认shell是bash的就包括那么二楼正解,就是cat /etc/passwd|grep bash|wc -l如果是正在登陆系统的账户中使用bash shell的,那么ps -e ...

  2. 安卓---项目中插入百度地图sdk

    百度地图 应用里面 自带地图 搜房网 下载百度地图的sdk 熟悉api 注冊百度开发人员的账号 2.12 仅仅要有一个ak就能够 高版本号须要提供应用程序的包名和签名返回开发人员的序列号 使用百度地图 ...

  3. Javascrpt 页面工具

    /**  *  笔者:DL  *  时间:2014-3-19   * PagingTool模块提供最基本的.网页工具栏.和页面数据 回电话 可扩展性 分页工具栏介绍,和页面呈现的数据   *   主意 ...

  4. varchar 分享影响记忆 试

    准备数据 sysbench --test=oltp --oltp-nontrx-mode=update_key --mysql-table-engine=innodb --oltp-table-siz ...

  5. ECshop 表结构

    -- 表的结构 `ecs_account_log`CREATE TABLE IF NOT EXISTS `ecs_account_log` (`log_id` mediumint(8) unsigne ...

  6. Linux应用环境实战05:在Ubuntu 14.10中借用Windows的字体 (转)

    阅读目录 设置系统字体 安装微软的英文字体 查看系统的配置文件 借用Windows的字体 编写配置文件 在前一篇随笔中,我详细讨论了字体的分类及用途,也以Fedora 20为例,展示了字体配置的思路和 ...

  7. iOS 通讯录-获取联系人属性

    内容均来自关东升老师的ios开发指南 上一篇写了联系人框架的一些必须知道的知识 如今写一下读取联系人数据相关操作 要读取通讯录数据库 须要 创建通讯录对象 查询获取数据(全部或者部分) 获取通讯录某一 ...

  8. 开源Office Word——DocX

    1.前言 请阅读前请看以下这位大神的文章 http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html 另附两个附件 1.DocX.DL ...

  9. MVC 学习 区域

    http://www.cnblogs.com/fly_dragon/archive/2011/10/12/2209438.html

  10. The app references non-public selectors in payload With Xcode6.1

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: p=591" style="color: rgb(255, 97, 0 ...