zoj3795 Grouping --- 强连通,求最长路

给定图,求把至少把图拆成几个集合能够使集合内的点没有直接或间接关系。

首先由题意可得图中可能含环,而环里面的点肯定是要拆开的。

缩点建图得DAG图,可以想象一下。。把图从入度为零的点向下展开,位于同一层的点放在一个集合是没有关系的,

那么题目所求的问题就转化成求图中最长路的问题了。

这个题的实质和 这题 其实是一模一样的。。

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#define eps 1e-6
#define ll __int64
#define M 100010//图中点数
using namespace std;

int sta[M],top;          //Tarjan 算法中的栈
bool vis[M];             //检查是否在栈中
int dfn[M];                  //深度优先搜索访问次序
int low[M];                  //能追溯到的最早的次序
int ccnt;                //有向图强连通分量个数
int id;                 //索引号
vector<int> e[M];        //邻接表表示
vector<int> part[M];   //获得强连通分量结果
int inpart[M];   //记录每个点在第几号强连通分量里
int degree[M];     //记录每个强连通分量的度
vector<int> edge[M];//缩点后建图
int ans,n,m,dp[M],in[M],point[M];

void tarjan(int x)
{
    int i,j;
    dfn[x]=low[x]=id++;
    vis[x]=1;
    sta[++top]=x;
    for(i=0;i<e[x].size();i++)
    {
        j=e[x][i];
        if(dfn[j]==-1)
        {
            tarjan(j);
            low[x]=min(low[x],low[j]);
        }
        else if(vis[j])
            low[x]=min(low[x],dfn[j]);
    }
    if(dfn[x]==low[x])
    {
        do
        {
            j=sta[top--];
            vis[j]=0;
            part[ccnt].push_back(j);
            inpart[j]=ccnt;
            point[ccnt]++;
        }while(j!=x);
        ccnt++;
    }
}

void solve(int n)
{
    memset(sta,-1,sizeof sta);
    memset(vis,0,sizeof vis);
    memset(dfn,-1,sizeof dfn);
    memset(low,-1,sizeof low);
    memset(point,0,sizeof point);

    top=ccnt=id=0;
    for(int i=1;i<=n;i++)
        if(dfn[i]==-1)
            tarjan(i);
}

int dfs(int x)
{
    if(dp[x]) return dp[x];
    dp[x]=point[x];
    int i;
    for(i=0;i<edge[x].size();i++)
    {
        int tmp=edge[x][i];
        dp[x]=max(dp[x],point[x]+dfs(tmp));
    }
    return dp[x];
}

int main()
{
    int n,m,i,j,a,b;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=0;i<=n;i++)
        {
            part[i].clear();
            e[i].clear();
            edge[i].clear();
        }
        while(m--)
        {
            scanf("%d%d",&a,&b);
            e[a].push_back(b);
        }
        solve(n);
        memset(in,0,sizeof in);
        for(i=1;i<=n;i++)//枚举原图中的边
        {
            for(j=0;j<e[i].size();j++)
            {
                int a=inpart[i];
                int b=inpart[e[i][j]];//
                if(a!=b)
                {
                    in[b]++;
                    edge[a].push_back(b);
                }
            }
        }
        ans=0;
        memset(dp,0,sizeof dp);
        for(i=0;i<ccnt;i++)//缩点后的图上是从0到ccnt编号的
        {
            if(!in[i])
                ans=max(ans,dfs(i));
        }
        printf("%d\n",ans);
    }
    return 0;
}

zoj3795 Grouping --- 强连通,求最长路

时间: 2024-10-29 01:06:09

zoj3795 Grouping --- 强连通,求最长路的相关文章

ZOJ3795 Grouping 强连通缩点+图的最长路

给出m条a年龄大于等于b的信息,要求可以比较的两个人不能放在同一组,问最少能分成几组. 由于是大于等于,所以原图可能构成强连通分量,意思就是有很多人年龄相同(想想也该知道,总共10w个人,肯定有很多人年龄重复= =!)将原图缩点后,对新图记忆化搜索求最长路. 如果不缩点,会RE... #include <iostream> #include<cstring> #include<cstdio> #include<string> #include<algo

【连通图|强连通分量+最长路】POJ-3592 Instantaneous Transference

Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Description It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called instantaneous transfer. When an object uses this magic fu

poj 3592 Instantaneous Transference 强连通图 缩点 再求最长路

1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<stack> 5 #include<queue> 6 using namespace std; 7 #define maxx 44 8 #define maxx2 44*44 9 #define INF 99999999 10 char s[maxx][maxx]; 11 bool tong[maxx2

POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;amp;&amp;amp; SPFA求最长路 &amp;amp;&amp;amp; 经典】

Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accepted: 1383 Description It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called instantaneous

POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;&amp; SPFA求最长路 &amp;&amp; 经典】

Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accepted: 1383 Description It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called instantaneous

HDU - 6201 transaction transaction transaction(spfa求最长路)

题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买书,然后再在边上行走,到达目的地后,在目的地b获得price[b]. 2.因此可以建立两个虚拟结点, 虚拟结点1连向n个点,边权分别为-price[i],表示以i为起点,需花费price[i]买书. n个点连向虚拟结点2,边权分别为price[i],表示以i为终点,通过卖书可得price[i]. 3

zoj-3795-Grouping-tarjan缩点求最长路

用tarjan进行缩点. 然后用dfs求最长路.水体... #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<stack> using namespace std; #define maxn 110000 vector<int>ol

【HDOJ1217】【Floyd求最长路】

http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9455    Accepted Submission(s): 4359 Problem Description Arbitrage is the use of discrepa

zoj 5303 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.