码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 5303 Grouping 缩点求最长路

时间:2015-03-21 18:41:36      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

点击打开链接
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}


Author: LUO, Jiewei

Source: ZOJ Monthly, June 2014


m条a年龄大于b年龄的信息,最少需要多少组,能够使得每组里面的年龄无法比较。

缩点之后求最长路径。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define M 100007
using namespace std;
using namespace std;
int head[M],low[M],dfn[M];
int stack[M],vis[M],belong[M];
int head1[M],dis[M],val[M];
int cnt,scnt,begin,num,num1;
int n,m;
struct E
{
    int to,next;
}edge[M*3],edge1[M*3];

void addedge(int u,int v)
{
    edge[num].to=v;
    edge[num].next=head[u];
    head[u]=num++;
}

void addedge1(int u,int v)
{
    edge1[num1].to=v;
    edge1[num1].next=head1[u];
    head1[u]=num1++;
}

void init()
{
    cnt=scnt=begin=num=num1=0;
    memset(head,-1,sizeof(head));
    memset(head1,-1,sizeof(head1));
    memset(dfn,0,sizeof(dfn));
    memset(vis,0,sizeof(vis));
    memset(low,0,sizeof(low));
    memset(val,0,sizeof(val));
}

void tarjan(int x)
{
    int v;
    dfn[x]=low[x]=++cnt;
    stack[++begin]=x;
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        v=edge[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(!vis[v])
            low[x]=min(low[x],dfn[v]);
    }
    if(low[x]==dfn[x])
    {
        scnt++;
        do
        {
            v=stack[begin--];
            belong[v]=scnt;
            val[scnt]++;
            vis[v]=1;
        }while(v!=x);
    }
}

void cal(int x)
{
    if(dis[x]!=-1)return;
    dis[x]=val[x];
    int now=val[x];
    for(int j=head1[x];j!=-1;j=edge1[j].next)
    {
        int v=edge1[j].to;
        cal(v);
        dis[x]=max(dis[x],dis[v]+now);
    }
}

void solve()
{
     for(int i=1;i<=n;i++)
            if(!dfn[i])tarjan(i);
    for(int u=1;u<=n;u++)
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(u!=v&&belong[u]!=belong[v])
                addedge1(belong[u],belong[v]);
        }
    for(int i=1;i<=scnt;i++)
        dis[i]=-1;
    for(int i=1;i<=scnt;i++)
        cal(i);
    int ans=0;
    for(int i=1;i<=scnt;i++)
        ans=max(ans,dis[i]);
    printf("%d\n",ans);
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        int u,v;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        solve();
    }
    return 0;
}


zoj 5303 Grouping 缩点求最长路

标签:

原文地址:http://blog.csdn.net/crescent__moon/article/details/44518559

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!