uva 11324 The Largest Clique(图论-tarjan,动态规划)

uva 11324 The Largest Clique(图论-tarjan,动态规划)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Problem B: The Largest Clique

uva 11324 The Largest Clique(图论-tarjan,动态规划)

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 between u and v in G that follows the directed edges only in the forward direction. This graph T(G) is often called the transitive closure of G.

We define a clique in a directed graph as a set of vertices U such that for any two vertices u and v in U, there is a directed edge either from u to v or from v to u (or both). The size of a clique is the number of vertices in the clique.

The number of cases is given on the first line of input. Each test case describes a graph G. It begins with a line of two integers n and m, where 0 ≤ n ≤ 1000 is the number of vertices of G and 0 ≤ m ≤ 50,000 is the number of directed edges of G. The vertices of G are numbered from 1 to n. The following m lines contain two distinct integers u and v between 1 and n which define a directed edge from u to v in G.

For each test case, output a single integer that is the size of the largest clique in T(G).

Sample input

1
5 5
1 2
2 3
3 1
4 1
5 2

Output for sample input

4

Zachary Friggstad

题目大意:

T组測试数据。给一张有向图G。求一个结点数最大的结点集,使得该结点中随意两个结点 u 和 v满足:要么 u 能够到达 v。 要么 v 能够到达 u(u 和 v 相互可达也能够)。

解题思路:

”同一个强连通分量中的点要么都选,要么不选。把强连通分量收缩点后得到SCC图。让每一个SCC结点的权等于它的结点数,则题目转化为求SCC图上权最大的路径。因为SCC图是一个 DAG, 能够用动态规划求解。

解题代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn=1100;
const int maxm=51000;

struct edge{
    int u,v,next;
    edge(int u0=0,int v0=0){
        u=u0;v=v0;
    }
}e[maxm];

int n,m,head[maxn],dfn[maxn],low[maxn],mark[maxn],w[maxn],color[maxn],dp[maxn],cnt,nc,index;
vector <int> vec;
vector <vector<int> > dfsmap;

void addedge(int u,int v){
    e[cnt]=edge(u,v);e[cnt].next=head[u];head[u]=cnt++;
}

void input(){
    cnt=nc=index=0;
    scanf("%d%d",&n,&m);
    vec.clear();
    for(int i=0;i<=n;i++){
        w[i]=dfn[i]=0;
        mark[i]=false;
        color[i]=dp[i]=head[i]=-1;
    }
    int u,v;
    while(m-- >0){
        scanf("%d%d",&u,&v);
        addedge(u,v);
    }
}

void tarjan(int s){
    dfn[s]=low[s]=++index;
    mark[s]=true;
    vec.push_back(s);
    for(int i=head[s];i!=-1;i=e[i].next){
        int d=e[i].v;
        if(!dfn[d]){
            tarjan(d);
            low[s]=min(low[d],low[s]);
        }else if(mark[d]){
            low[s]=min(low[s],dfn[d]);
        }
    }
    if(dfn[s]==low[s]){
        nc++;
        int d;
        do{
            d=vec.back();
            vec.pop_back();
            color[d]=nc;
            mark[d]=false;
            w[nc]++;
        }while(d!=s);
    }
}

int DP(int s){
    if(dp[s]!=-1) return dp[s];
    int ans=w[s];
    for(int i=0;i<dfsmap[s].size();i++){
        int d=dfsmap[s][i];
        if(DP(d)+w[s]>ans) ans=DP(d)+w[s];
    }
    return dp[s]=ans;
}

void solve(){
    for(int i=1;i<=n;i++){
        if(!dfn[i]) tarjan(i);
    }
    dfsmap.clear();
    dfsmap.resize(nc+1);
    for(int i=0;i<cnt;i++){
        int x=color[e[i].u],y=color[e[i].v];
        if(x!=y){
            dfsmap[x].push_back(y);
            //cout<<x<<"->"<<y<<endl;
        }
    }
    int ans=0;
    for(int i=1;i<=nc;i++){
        if(DP(i)>ans) ans=DP(i);
        //cout<<i<<" "<<ans<<endl;
    }
    printf("%d\n",ans);
}

int main(){
    int t;
    scanf("%d",&t);
    while(t-- >0){
        input();
        solve();
    }
    return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116923.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • resque 遍历加载job目录下的类

    resque 遍历加载job目录下的类

    2022年2月21日
    44
  • FLASHFXP3.6注册码「建议收藏」

    FLASHFXP3.6注册码「建议收藏」
    FLASHFXPxgA9UrTnxgAAAAC7W5MNJwTnsl73n0Vuu1AePJgEAG
    41mgSvHSiDWi1Kg8B5xJNCCCAVCMr9Rtmo6u93NoQF27wg6KrQ
    3RIE8Jz6Qy/YlnYIBf58aRJrG7rGITgQJ4KSmyK0wz4xySS+0H
    N/9lJ65abJf7rB7m3R2vyjhakIKSzZnquXQ0/Rdv5nMS+Bbc7n
    rMqoYnTURE8jeePVOg

    2022年7月26日
    9
  • 学习Java必读的10本书籍

    学习Java必读的10本书籍来源|愿码(ChainDesk.CN)内容编辑愿码Slogan|连接每个程序员的故事网站|http://chaindesk.cn愿码愿景|打造全学科IT系统免费课程,助力小白用户、初级工程师0成本免费系统学习、低成本进阶,帮助BAT一线资深工程师成长并利用自身优势创造睡后收入。官方公众号|愿码|愿码服务号|区块链部落免费加入愿码全思维工程师社群|任一…

    2022年6月18日
    24
  • django filter查询_搜索全局排序方法

    django filter查询_搜索全局排序方法前言当我们需要对后台的数据进行过滤的时候,drf有两种,搜索过滤和排序过滤。搜索过滤:比如我们想返回sex=1的,那么我们就可以从所有数据中进行筛选排序过滤:比如我们想对价格进行升序排列,就可以

    2022年7月31日
    8
  • java如何遍历数组中的元素_js数组遍历方法

    java如何遍历数组中的元素_js数组遍历方法packagecom.yang;publicclassDemo01{publicstaticvoidmain(String[]args){int[]a={25,64,19,48,91,23};//遍历数组中的元素for(intx:a){System.out.println(x);}}}

    2026年1月25日
    3
  • python加入中小学课程_浙江中小学信息技术科9月换新教材,课程新增Python、PPT、H5………

    python加入中小学课程_浙江中小学信息技术科9月换新教材,课程新增Python、PPT、H5………今年9月新学期,浙江省三到九年级的信息技术课将替换新教材。与老教材相比,新版教材更接地气,以后小学生就要学会做PPT、H5,开始接触算法、大数据等。据悉,这次教材的修订,是自2017年教育部发布高中信息技术新课标后,浙江省教研室组织省内教研员、骨干教师精心修撰的成果。近段时间,浙江省教研室组织小学和初中信息技术课老师,进行三到九年级新修订教材的培训。记者翻阅新教材大纲发现,从小学三年级一直到九…

    2022年5月16日
    56

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号