tarjan缩点+最长路

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

给n个点,m个关系,把这些点分成最少的组,使得组内的点相互之间不存在关系。

由于可能存在环,所以缩点之后跑dag最长路就好了。为什么是最长路呢?因为这条路上每一个点之间是存在关系的,所以这些点必属于不同的组。dag的点权就是缩点之后每个联通分量包含的点的数量。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 300005;
int dfn[maxn],low[maxn],belong[maxn],sum[maxn];
int head[maxn],Stack[maxn],x[maxn],y[maxn],dis[maxn];
int tot,ans,top,cnt,n,m;
bool vis[maxn];

struct node
{
    int to,next;
}a[maxn];

void add(int u,int v)
{
    a[tot].to = v;
    a[tot].next = head[u];
    head[u] = tot++;
}

void tarjan(int u)
{
    int v;
    dfn[u] = low[u] = ++cnt;
    vis[u] = true;
    Stack[top++] = u;
    for(int i=head[u]; i!=-1; i=a[i].next)
    {
        v = a[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[u] = min(low[u],low[v]);
        }
        else
        {
            if(vis[v])
                low[u] = min(low[u],dfn[v]);
        }
    }
    if(low[u] == dfn[u])
    {
        ans++;
        do
        {
            v = Stack[--top];
            vis[v] = false;
            belong[v] = ans;
            sum[ans]++;
        }
        while(v != u);
    }
}

void solve()
{
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(sum,0,sizeof sum);
    memset(vis,false,sizeof(vis));
    top = cnt = ans = 0;
    for(int i=1; i<=n; i++)
    {
        if(!dfn[i])
        {
            tarjan(i);
        }
    }
}

void dp(int x)
{
    dis[x] = sum[x];
    int mx = 0;
    for(int i=head[x]; i!=-1; i=a[i].next)
    {
        int v = a[i].to;
        if(!dis[v])
        {
            dp(v);
        }
        mx = max(mx,dis[v]);
    }
    dis[x] += mx;
}

int main()
{
    while(scanf("%d%d",&n,&m) != EOF)
    {
        tot = 0;
        memset(head,-1,sizeof(head));
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            add(x[i],y[i]);
        }
        solve();
        tot = 0;
        memset(head,-1,sizeof(head));
        for(int i=1; i<=m; i++)
        {
            if(belong[x[i]] != belong[y[i]])
            {
                add(belong[x[i]],belong[y[i]]);
            }
        }
        memset(dis,0,sizeof(dis));
        int num = 0;
        for(int i=1; i<=ans; i++)
        {
            if(!dis[i])
            {
                dp(i);
                num = max(num,dis[i]);
            }
        }
        printf("%d\n",num);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值