uva11324 The Largest Clique --- 强连通+dp

给一个有向图G,求一个子图要求其中任意两点至少有一边可达,

问这个子图中最多含多少个顶点。

首先找SCC缩点建图,每个点的权值就是该点包含点的个数。

要求其中任意两点可达,实际上所有边只能同方向,不然一定有两点不可达,

这样题目又转换成求DAG图最长路的问题了。

然后从入度为0的点开始记忆化搜索,dp[i]表示以i为根最多包含多少点。

#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 1010
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,t;
    scanf("%d",&t);
    while(t--)
    {
        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);
        /*printf("ccnt:%d\n",ccnt);
        for(i=0;i<ccnt;i++)
        {
            for(j=0;j<part[i].size();j++)
                printf("%d ",part[i][j]);
            puts("");
        }*/
        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;
}

uva11324 The Largest Clique --- 强连通+dp

时间: 2024-08-28 19:36:56

uva11324 The Largest Clique --- 强连通+dp的相关文章

UVa11324 - The Largest Clique(DAG+DP+SCC)

Problem B: The Largest Clique Given a directed graph G, consider the following transformation. First, create a new graph T(G) to have the same vertex set as G. Create a directed edge between two vertices u and v in T(G) if and only if there is a path

UVA11324 The Largest Clique[强连通分量 缩点 DP]

UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直接DP行了 这个新图不需要判重边,重边就是真实存在 // // main.cpp // 最大团 // // Created by Candy on 02/11/2016. // Copyright © 2016 Candy. All rights reserved. // #include <ios

UVA11324 The Largest Clique —— 强连通分量 + 缩点 + DP

题目链接:https://vjudge.net/problem/UVA-11324 题解: 代码一: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <sta

UVA - 11324 The Largest Clique 强连通缩点+记忆化dp

题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每个点的权值就是当前强连通分量点的个数. /* Tarjan算法求有向图的强连通分量set记录了强连通分量 Col记录了强连通分量的个数. */ #include <iostream> #include<cstring> #include<cstdio> #include<string> #include<algo

uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=show_problem&problem=2299 题意:输入n和m,有n个点和m条有向边,求出一个节点集合包括的节点个数最多,而且该节点内的不论什么两点a,b,要么a能到达b,要么b能到达a,要么a和b互相到达. 思路:强连通分量缩点形成有向无环图DAG,把缩点后的每一个点的权值置为该强连通分量的节点个

UVA-11324 The Largest Clique 【有向图强连通+缩点+DP】

题目链接:https://vjudge.net/problem/UVA-11324 题目大意:给定一张有向图G,求一个结点数最大的结点集,集合中每两个点都至少有一条路径相连(方向任意). 题解: 易知如果一个点被选择,则它所在强连通分量中的其他点也一定要选,如果不选,则其他点也不可选,因此先求出强连通分量,利用缩点创建出另一张有向图G2,每个结点的权值就是该强连通分量的结点数,再DP求解. 代码: 1 #include<bits/stdc++.h> 2 using namespace std;

UVA 11324 The Largest Clique (强连通分量缩点,图DP)

题目: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=show_problem&problem=2299 题意: 给你一个有向图,求一个点集合的最大大小,使得此点集合中对于任意点对(u,v),有从u到v或者从v到u的边 方法: 先找强连通分量缩点,每个强连通分量显然满足条件,然后在缩点后的图中找到一条权值最大的路径,权值为此路径的点权之和,点权为这个

UVa 11324 The Largest Clique (强连通分量+DP)

题意:给定一个有向图,求一个最大的结点集,使得任意两个结点,要么 u 能到 v,要么 v 到u. 析:首先,如果是同一个连通分量,那么要么全选,要么全不选,然后我们就可以先把强连通分量先求出来,然后缩成一个点,然后该图就成了一个DAG,然后就可以直接用DP来做了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #i

UVA - 11324 The Largest Clique (强连通缩点+dp)

题目链接 题意:从有向图G中找到一个最大的点集,使得该点集中任意两个结点u,v满足u可达v或v可达u. 解法:先把同处于一个强连通分量中的结点合并(缩点),得到一张DAG图,在DAG上dp即可. 感觉自己的建图写得好丑啊,一直在纠结用数组还是结构体~~ 1 #include<bits/stdc++.h> 2 3 using namespace std; 4 const int N=1e5+10; 5 int head[N],nxt[N],to[N],ne,n,m; 6 void addedge