ZOJ 3795 Grouping(Tarjan缩点+DAG)
2014-10-19 01:47
363 查看
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.
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.
记忆化搜索最长路径:
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 thenumber 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缩点 + DGA上的最长路
- zoj-3795-Grouping-tarjan缩点求最长路
- ZOJ 3795 Grouping (tarjan缩点求最长链)
- ZOJ 3795 Grouping(Tarjan收缩点+DAG)
- ZOJ 3795 Grouping 强连通缩点 + DAG最长路
- zoj-3795-Grouping-tarjan确定最长的公路收缩
- ZOJ 3795 Grouping 缩点拓扑序下求最长链
- ZOJ 3795 Grouping(强联通缩点,记忆化搜索)
- ZOJ 3795 Grouping 强连通分量-tarjan
- ZOJ 3795 Grouping(强联通分量 + 缩点 + Dp)
- zoj 3795 Grouping 强连通缩点+拓扑排序最长链
- ZOJ 3795 Grouping(强连通+最长路)
- ZOJ_3795 Grouping(强连通分量 拓扑)
- ZOJ-3795-Grouping 解题报告
- ZOJ 3795 Grouping
- ZOJ 3795 Grouping 强联通 最长路径
- POJ 1236 Network of Schools(Tarjan Algorithm求强连通子集,缩点后DAG上出或入度为0的点)
- ZOJ 3795 - Grouping (强连通+dp)
- ZOJ Problem Set - 3795(缩点拓补)
- ZOJ 3795 Grouping(scc+最长路)