ZOJ 3795 Grouping(Tarjan缩点+DAG)

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}

记忆化搜索最长路径:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )

const int maxn=100100;
const int maxm=300100;
struct node{
    int u,v;
    int next;
}e[maxm],e2[maxm];
int head[maxn],cntE,cntF;
int DFN[maxn],low[maxn],h[maxn];
int s[maxm],top,dex,cnt;
int belong[maxn],instack[maxn];
int dp[maxn],num[maxn];
int n,m;
void init()
{
    top=cntE=cntF=0;
    dex=cnt=0;
    CLEAR(DFN,0);
    CLEAR(head,-1);
    CLEAR(instack,0);
    CLEAR(num,0);//fuck num没清0wa了2小时
}
void addedge(int u,int v)
{
    e[cntE].u=u;e[cntE].v=v;
    e[cntE].next=head[u];
    head[u]=cntE++;
}
void Tarjan(int u)
{
    DFN[u]=low[u]=++dex;
    instack[u]=1;
    s[top++]=u;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].v;
        if(!DFN[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
            low[u]=min(low[u],DFN[v]);
    }
    int v;
    if(DFN[u]==low[u])
    {
        cnt++;
        do{
            v=s[--top];
            belong[v]=cnt;
            instack[v]=0;
        }while(u!=v);
    }
}
int dfs(int x)
{
    if(dp[x]) return dp[x];
    dp[x]=num[x];
    for(int i=h[x];i!=-1;i=e2[i].next)
        dp[x]=max(dp[x],dfs(e2[i].v)+num[x]);
    return dp[x];
}
void work()
{
    REPF(i,1,n)
      if(!DFN[i])  Tarjan(i);
    REPF(i,1,n)
       num[belong[i]]++;
    CLEAR(h,-1);
    CLEAR(dp,0);
    REPF(k,1,n)
    {
        for(int i=head[k];i!=-1;i=e[i].next)
        {
            int v=e[i].v;
            if(belong[k]!=belong[v])
            {
                e2[cntF].u=belong[k];
                e2[cntF].v=belong[v];
                e2[cntF].next=h[belong[k]];
                h[belong[k]]=cntF++;
            }
        }
    }
    int ans=0;
//    cout<<"2333  "<<cnt<<endl;
    REPF(i,1,cnt)
        ans=max(ans,dfs(i));
    printf("%d\n",ans);
}
int main()
{
    int u,v;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        work();
    }
    return 0;
}
时间: 2024-08-01 05:08:30

ZOJ 3795 Grouping(Tarjan缩点+DAG)的相关文章

ZOJ 3795 Grouping 强连通分量-tarjan

一开始我还天真的一遍DFS求出最长链以为就可以了 不过发现存在有向环,即强连通分量SCC,有向环里的每个点都是可比的,都要分别给个集合才行,最后应该把这些强连通分量缩成一个点,最后保证图里是 有向无环图才行,这个时候再找最长链,当然缩点之后的scc是有权值的,不能只看成1,缩点完了之后,用记忆化搜索DP就可以再On的复杂度内求出结果 所以现学了一下SCC-Tarjan,所谓Scc-tarjan,就是找到强连通分量并且缩点,特别好用,其原理就是利用dfs时间戳,每个点有自己的时间戳,同时再开一个记

UvaLive4287 roving Equivalences(Tarjan缩点+DAG)

UvaLive4287 roving Equivalences 题意:给n个定理,以及m个关系,即u定理可以推出v定理.问至少还需要加多少个条件,才能是定理两两互推. 思路:Tarjan缩点.然后变成一个DAG.ans1记录入度为0的联通块,ans2记录出度为0的联通块.输出较大值即可.注意如果点数为1或者只有一个强连通分量要输出0. /* ID: onlyazh1 LANG: C++ TASK: Knights of the Round Table */ #include<iostream>

ZOJ 3795 Grouping 强联通缩点+拓扑序+偏序集的最大链的大小

题意:有n个人,m个关系,关系是这两个人前一个人可以跟后一个比较. 那么问你我最少分多少组可以使这个组里的人都不可以比较. 只会强联通缩点,真特么不知道怎么做,想了一个小时,网上一看,还要会偏序集的东西,有一个叫Dilworth定理的东西. 定理1 令(X,≤)是一个有限偏序集,并令r是其最大链的大小.则X可以被划分成r个但不能再少的反链. 其对偶定理称为Dilworth定理: 定理2 令(X,≤)是一个有限偏序集,并令m是反链的最大的大小.则X可以被划分成m个但不能再少的链. 然后我们用到的是

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

[SDOI2010] 所驼门王的宝藏 [建图+tarjan缩点+DAG dp]

题面传送门: 传送门 思路: 看完题建模,容易得出是求单向图最长路径的问题 那么把这张图缩强联通分量,再在DAG上面DP即可 然而 这道题的建图实际上才是真正的考点 如果对于每一个点都直接连边到它所有的后继节点,那么可以被卡掉(1e5个点在同一行上) 考虑改变思路,运用网络流建图中的一个常用技巧:把横边和竖边映射成点,再从每个点向所在横坐标.纵坐标代表的点连边即可 这样会有2e6+1e5个点,但是tarjan算法效率O(n),完全无压力 自由(和谐)门的话,目前还没有比较好的方法解决 上网看了一

ZOJ 3795 Grouping

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

POJ2762-Going from u to v or from v to u?(Tarjan缩点,DAG判直链)

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14474   Accepted: 3804 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors

【BZOJ-1924】所驼门王的宝藏 Tarjan缩点(+拓扑排序) + 拓扑图DP

1924: [Sdoi2010]所驼门王的宝藏 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 787  Solved: 318[Submit][Status][Discuss] Description Input 第一行给出三个正整数 N, R, C. 以下 N 行,每行给出一扇传送门的信息,包含三个正整数xi, yi, Ti,表示该传送门设在位于第 xi行第yi列的藏宝宫室,类型为 Ti.Ti是一个1~3间的整数, 1表示可以传送到第 xi行任意

[BZOJ 1051][HAOI 2006]受欢迎的牛(tarjan缩点)

http://www.lydsy.com:808/JudgeOnline/problem.php?id=1051 唔...这题好像在POJ上见过? 比较水的题,很好想出思路.牛和牛之间的关系就像有向图,牛a喜欢牛b相当于建立有向边a->b,然后在这个有向图中,每个强连通分量里的牛们相当于是相互喜欢的,把这个图缩点成DAG,DAG里如果有且仅有一个出度为0的点,则这个点对应强连通分量里的所有牛都是受欢迎的牛,如果没有出度为0的点,当然就没受欢迎的牛了,如果出度为0的点的个数大于1,则每个出度为0的