ZOJ 3795 Grouping(强联通分量 + 缩点 + Dp)

Problem Description:

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

解题思路:
强连通分量缩点,然后DP求最长路

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int MAXN = 100000 + 10;
vector<int>G[MAXN];
vector<int>RG[MAXN];
int vis[MAXN];
int pre[MAXN], lowlink[MAXN], sccno[MAXN];//点对应的强联通分量的编号
int dfs_clock, scc_cnt;//强联通分量的个数,编号为1~scc_cnt
int num[MAXN];//每个强联通分量所含的点的数目
stack<int>S;
void dfs(int u)
{
    pre[u] = lowlink[u] = ++dfs_clock;
    S.push(u);
    for(int i=0;i<G[u].size();i++)
    {
        int v = G[u][i];
        if(!pre[v])
        {
            dfs(v);
            lowlink[u] = min(lowlink[u], lowlink[v]);
        }
        else if(!sccno[v])
        {
            lowlink[u] = min(lowlink[u], pre[v]);
        }
    }
    if(lowlink[u] == pre[u])
    {
        scc_cnt++;
        for(;;)
        {
            int x = S.top();
            S.pop();
            sccno[x] = scc_cnt;
            num[scc_cnt]++;
            if(x == u) break;
        }
    }
}
void find_scc(int n)
{
    dfs_clock = scc_cnt = 0;
    memset(sccno, 0, sizeof(sccno));
    memset(pre, 0, sizeof(pre));
    memset(num, 0, sizeof(num));
    for(int i=0;i<n;i++)
    {
        if(!pre[i]) dfs(i);
    }
}
int dp[MAXN];
int DP(int u)
{
    if(dp[u] != -1)
        return dp[u];
    dp[u] = num[u];
    for(int i=0;i<RG[u].size();i++)
    {
        int v = RG[u][i];
        dp[u] = max(dp[u], num[u] + DP(v));
    }
    return dp[u];
}
int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        for(int i=0;i<n;i++)
            G[i].clear();
        int u, v;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d", &u, &v);
            u--; v--;
            G[u].push_back(v);
        }
        find_scc(n);
        for(int i=1;i<=scc_cnt;i++) RG[i].clear();
        for(int i=0;i<n;i++)
        {
            int sz = G[i].size();
            for(int j=0;j<sz;j++)
            {
                if(sccno[i] != sccno[G[i][j]])
                {
                    RG[sccno[i]].push_back(sccno[G[i][j]]);
                }
            }
        }
        int ans = 0;
        memset(dp, -1, sizeof(dp));
        for(int i=1;i<=scc_cnt;i++)
            ans = max(ans, DP(i));
        printf("%d\n", ans);
    }
    return 0;
}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。