ZOJ

来源:互联网 发布:网络推广招聘信息 编辑:程序博客网 时间:2024/06/10 23:19

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.

<h4< dd="">
Output

For each the case, print the minimum number of groups that meet the requirement one line.

<h4< dd="">
Sample Input
4 41 21 32 43 4
<h4< dd="">
Sample Output
3
<h4< dd="">
Hint

set1= {1}, set2= {2, 3}, set3= {4}


一开始想的直接求一个最长路就是,不过没考虑到成环的情况,MLE。

有环的话,那么就先强连通缩点然后求最长路就可以了。

因为边还是有点多,spfa会超时,利用记忆化搜索就可以过了。

#include<bits/stdc++.h>using namespace std;const int MAXN=1e5+7;int n,m;int du[MAXN];vector<int>head[MAXN],G[MAXN];int dfn[MAXN],sccno[MAXN],sum[MAXN],dfs_clock,scc_cnt;stack<int>S;int dfs(int u){    int lowu=dfn[u]=++dfs_clock;    S.push(u);    for(int i=0,l=head[u].size(); i<l; ++i)    {        int v=head[u][i];        if(!dfn[v])        {            int lowv=dfs(v);            lowu=min(lowu,lowv);        }        else if(!sccno[v])        {            lowu=min(lowu,dfn[v]);        }    }    if(lowu==dfn[u])    {        scc_cnt++;        while(1)        {            int x=S.top();            S.pop();            sccno[x]=scc_cnt;            if(x==u)break;        }    }    return lowu;}void find_scc(){    dfs_clock=scc_cnt=0;    fill(dfn+1,dfn+1+n,0);    fill(sccno+1,sccno+1+n,0);    for(int i = 1; i <= n; ++i)if(!dfn[i])dfs(i);}//bool vis[MAXN];//int dis[MAXN];//int s,e;//int bfs()//{//    fill(vis+1,vis+1+scc_cnt,0);//    fill(dis+1,dis+1+scc_cnt,0);//    queue<int>q;//    for(int i = 1; i <= scc_cnt; ++i)if(!du[i])//        {//            q.push(i);//            dis[i] = sum[i];//            vis[i] = 1;//        }////    while(!q.empty())//    {//        int u=q.front();//        q.pop();//        vis[u] = 0;//        for(int i=0,l=G[u].size(); i<l; ++i)//        {//            int v=G[u][i];//            if(dis[v] < dis[u] + sum[v])//            {//                dis[v] = dis[u] + sum[v];//                if(!vis[v])//                {//                    vis[v]=1;//                    q.push(v);//                }//            }//        }//    }//    int ans = 0;//    for(int i = 1 ; i <= scc_cnt ; ++i)ans = max(ans,dis[i]);//    return ans;//}int dis[MAXN];void dfsl(int u){    if(dis[u])return ;    dis[u] = sum[u];    for(int i = 0,l = G[u].size(); i<l; ++i)    {        int v=G[u][i];        dfsl(v);        dis[u] = max(dis[u],dis[v] + sum[u]);    }}int main(){    while(~scanf("%d%d",&n,&m))    {        for(int i = 1; i <= n; ++i)head[i].clear();        int u,v;        while(m--)        {            scanf("%d%d",&u,&v);            head[u].push_back(v);        }        find_scc();        fill(sum+1,sum+1+scc_cnt,0);        for(int i = 1; i <= n; ++i)sum[sccno[i]]++;        for(int i=1; i<=scc_cnt; ++i)G[i].clear();        for(int i = 1; i <= n; ++i)du[i] = 0;        for(int i = 1; i <= n; ++i)            for(int j=0,l=head[i].size(); j<l; ++j)            {                u=sccno[i];                v=sccno[head[i][j]];                if(u!=v)                {                    G[u].push_back(v);                    du[v]++;                }            }        int ans = 0 ;        fill(dis+1,dis+1+scc_cnt,0);        for(int i = 1 ; i <= scc_cnt ; ++i)        {            if(!du[i])            {                dfsl(i);                ans = max(ans,dis[i]);            }        }        printf("%d\n",ans);    }}






原创粉丝点击