ZOJ 3795 Grouping(Tarjan缩点+DAG)
来源:互联网 发布:手机淘宝直通车怎么开 编辑:程序博客网 时间:2024/05/05 00:15
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 41 21 32 43 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)
- ZOJ 3795 Grouping 强连通缩点 + DAG最长路
- ZOJ 3795 — Grouping 缩点+拓扑
- zoj-3795-Grouping-tarjan缩点求最长路
- ZOJ 3795 Grouping (tarjan缩点求最长链)
- ZOJ 3795 Grouping 缩点拓扑序下求最长链
- ZOJ 3795 Grouping(强联通分量 + 缩点 + Dp)
- ZOJ 3795 Grouping(强联通缩点,记忆化搜索)
- zoj 3795 Grouping(强连通缩点)
- poj 2186 DAG+缩点+tarjan模板 (模板待消化
- ZOJ 3795 Grouping 强联通缩点+拓扑序+偏序集的最大链的大小
- 训练赛 Grouping(强连通分量缩点 + DAG求最长路)
- zoj 3795 Grouping(强连通+dp)
- ZOJ 3795 Grouping
- ZOJ 3795 Grouping
- ZOJ-3795 Grouping
- ZOJ-3795-Grouping 解题报告
- zoj 3795 Grouping(tarjin+dfs)
- 通过Servlet 将服务器硬盘图片 展示到浏览器
- DICOM医学图像处理:WEB PACS初谈
- Struts2使用拦截器完成权限控制示例
- 如何搭建VS的uc/os—III实验平台操作流程
- arithmetic Dimension too large Latex 报错的解决办法
- ZOJ 3795 Grouping(Tarjan缩点+DAG)
- Spring Security3学习
- 微信平台开发与微信营销
- tomcat用tomcatX.exe无法启动,用startup.bat可以启动
- html学习笔记二
- warning C4996: “scanf”被声明为否决的
- 设计模式(四)The Factory Pattern 工厂模式
- 判断链表是否循环,找出循环点
- 4、libgdx应用框架