ZOJ 3795 Grouping(Tarjan縮點+DAG),zoj3795

來源:互聯網
上載者:User

ZOJ 3795 Grouping(Tarjan縮點+DAG),zoj3795

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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.