ZOJ 3795 Grouping(强联通缩点,记忆化搜索)

18 篇文章 0 订阅
13 篇文章 0 订阅

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3795

Grouping

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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 4
1 2
1 3
2 4
3 4
Sample Output
3
Hint

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


居然有环!

 题意:

有n个人,给出m条关系(u,v)表示u>=v, 现在要将这些人分开,能确定大小关系的不能放在一个集合,求最少的集合数。

分析:

对于每条关系(u,v),我们建立有向边u->v,这样就形成一个有向图,我们只要找到图中的那条最长的通路,这条路上的点都不能放在一起,但其他的点一定能找到这条路上的点和它在一起。这样就是求解一个有向图的最长路径。

但是给出的关系是u>=v,就会出现a=b=c的情况,即a->b->c->a,这样就有环了,这样就需要强联通缩点,将这个图转化成DAG,然后就可以用记忆化搜索愉快地求解了。


代码略搓:

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define mm 300007
#define nn 100007

using namespace std;

itn fir[nn];
itn u[mm],v[mm],nex[mm];
int w[nn];
itn n,m;
int st[mm];
int top;
int far[nn];
int cnt;
int pre[nn],low[nn],sccno[nn],dfs_clock,scc_cnt;

void read_graph()
{
    //scanf("%d %d",&n,&m);
    memset(fir,-1,sizeof fir);

    for (itn e=0;e<m;e++)
    {
        scanf("%d %d",&u[e],&v[e]);
        nex[e]=fir[u[e]];
        fir[u[e]]=e;
    }
}

void tarjan_dfs(int _u)
{
    pre[_u]=low[_u]=++dfs_clock;
    st[++top]=_u;
    for (int e=fir[_u];e!=-1;e=nex[e])
    {
        int _v=v[e];
        if (pre[_v]==0)
        {
            tarjan_dfs(_v);
            low[_u]=min(low[_u],low[_v]);
        }
        else
        {
            if (sccno[_v]==0)
                low[_u]=min(low[_u],pre[_v]);
        }
    }

    if (low[_u]==pre[_u])
    {
        scc_cnt++;
//        printf("scccnt %d\n",scc_cnt);
        while (true)
        {
            int x=st[top--];
//            printf("%d -> %d\n",x,scc_cnt);
            sccno[x]=scc_cnt;
            w[scc_cnt]++;
            if (x==_u)   break;
        }
//        printf("\n");
    }
}

void find_scc()
{
    dfs_clock=scc_cnt=0;
    top=-1;
    memset(sccno,0,sizeof sccno);
    memset(pre,0,sizeof pre);
    for (int i=1;i<=n;i++)
    {
        if (pre[i]==0)  tarjan_dfs(i);
    }
}

itn dp[nn];

int dfs(int _u)
{
//    printf("_u=%d\n",_u);
    if (dp[_u]!=0) return dp[_u];

    for (int e=fir[_u];e!=-1;e=nex[e])
    {
        dp[_u]=max(dp[_u],dfs(v[e]));
    }

    dp[_u]+=w[_u];
    return dp[_u];
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    #endif // ONLINE_JUDGE
	int T;
	//scanf("%d",&T);
	while (~scanf("%d %d",&n,&m))
    {
        read_graph();
        memset(w,0,sizeof w);

        find_scc();

        cnt=-1;
        memset(fir,-1,sizeof fir);
        memset(far,0,sizeof far);
//
//        for (int i=1;i<=n;i++)
//        {
//            printf("%d -> %d\n",i,sccno[i]);
//        }
//
//        puts("");

        for (int e=0;e<m;e++)
        {
            if (sccno[u[e]]==sccno[v[e]])   continue;
            ++cnt;

//            printf("(%d->%d)----->(%d->%d)\n",u[e],sccno[u[e]],v[e],sccno[v[e]]);

            u[cnt]=sccno[u[e]];
            v[cnt]=sccno[v[e]];
            nex[cnt]=fir[u[cnt]];
            fir[u[cnt]]=cnt;

            far[v[cnt]]=1;

//            printf("%d %d\n",u[cnt],v[cnt]);

        }

        int ans=0;
        memset(dp,0,sizeof dp);

        for (int i=1;i<=scc_cnt;i++)
        {
            if (far[i]==0)
            {
                //dp[i]=w[i];
                ans=max(ans,dfs(i));
            }
        }

        printf("%d\n",ans);

    }
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值