ZOJ - 3795

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 4
1 2
1 3
2 4
3 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);
    }
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值