题目要求一个最大的弱联通图。

首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构。

对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数。

/*
Tarjan算法求有向图的强连通分量set记录了强连通分量
Col记录了强连通分量的个数。
*/
#include <iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
#define MAXN 2005
#define MAXM 100005
struct node
{
int to,next;
}edge[MAXM],edge2[MAXM];
bool have[1005][1005];
int sums[1005];
int head[MAXN],head2[MAXN],en,en2;
bool root[MAXN];
int low[MAXN],dfn[MAXN],stack[MAXN],top,set[MAXN],col,num;
bool vis[MAXN],instack[MAXN];
int dp[MAXN];
int n;
int m;
void addedge(int a,int b)
{
edge[en].to=b;
edge[en].next=head[a];
head[a]=en++;
}
void addedge2(int a,int b)
{
edge2[en2].to=b;
edge2[en2].next=head2[a];
head2[a]=en2++;
}
void tarjan(int u)
{ vis[u]=1;
dfn[u]=low[u]=++num;
instack[u]=true;
stack[++top]=u;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
if(instack[v])
low[u]=min(dfn[v],low[u]);
}
if(dfn[u]==low[u])
{
int j;
col++;
do
{
j=stack[top--];
instack[j]=false;
set[j]=col;
}
while (j!=u);
}
}
void init()
{
int i;
en=en2=top=col=num=0;
memset(head,-1,sizeof(head));
memset(head2,-1,sizeof(head2));
memset(instack,0,sizeof(instack));
memset(vis,0,sizeof(vis));
memset(set,-1,sizeof(set));
memset(have,0,sizeof(have));
memset(sums,0,sizeof(sums));
memset(root,true,sizeof(root));
memset(dp,-1,sizeof(dp));
}
int ans;
int dfs(int now,int fa)
{
if(~dp[now]) return dp[now];
int maxs=sums[now];
for(int i=head2[now];~i;i=edge2[i].next)
{
int to=edge2[i].to;
if(to!=fa)
{
maxs=max(maxs,sums[now]+dfs(to,now));
}
}
return dp[now]=maxs;
}
int main()
{
int a,b;
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
addedge(a,b);
}
for(int i=1;i<=n;i++)
if(!vis[i])tarjan(i);
for(int i=1;i<=n;i++)
{
sums[set[i]]++;
for(int j=head[i];~j;j=edge[j].next)
{
int to=edge[j].to;
if(set[i]!=set[to]&&!have[set[i]][set[to]])
{
addedge2(set[i],set[to]);
root[set[to]]=false;
have[set[i]][set[to]]=have[set[to]][set[i]]=true;
}
}
}
ans=0;
for(int i=1;i<=col;i++)
{
if(root[i])
{
dfs(i,-1);
ans=max(ans,dp[i]);
}
}
printf("%d\n",ans);
}
return 0;
}

UVA - 11324 The Largest Clique 强连通缩点+记忆化dp的更多相关文章

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

    题目链接 题意:从有向图G中找到一个最大的点集,使得该点集中任意两个结点u,v满足u可达v或v可达u. 解法:先把同处于一个强连通分量中的结点合并(缩点),得到一张DAG图,在DAG上dp即可. 感觉 ...

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

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...

  3. UVA 11324 The Largest Clique(强连通分量+缩点DAG的DP)

    题意:给定一个有向图,求出一个最大的结点集,这个节点集中的随意两个点之间至少一个能到达还有一个点. 思路:假设一个点在这个节点集中,那么它所在的强连通分量中的点一定所有在这个节点集中,反之亦然, 求出 ...

  4. UVA 11324.The Largest Clique tarjan缩点+拓扑dp

    题目链接:https://vjudge.net/problem/UVA-11324 题意:求一个有向图中结点数最大的结点集,使得该结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相 ...

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

    题意:给定一个有向图,求一个最大的结点集,使得任意两个结点,要么 u 能到 v,要么 v 到u. 析:首先,如果是同一个连通分量,那么要么全选,要么全不选,然后我们就可以先把强连通分量先求出来,然后缩 ...

  6. UVA 11324 The Largest Clique(缩点+DAG上的dp)

    求最大团.和等价性证明有类似之处,只不过这个不是求互推,而是只要a->b,或b->a即可. 同样的,容易想到先缩点,得到DAG,每个节点上保存SCC的点数,相信任意一条由根节点(入度为零) ...

  7. UVA 11324 - The Largest Clique(强连通分量+缩点)

    UVA 11324 - The Largest Clique 题目链接 题意:给定一个有向图,要求找一个集合,使得集合内随意两点(u, v)要么u能到v,要么v能到u,问最大能选几个点 思路:强连通分 ...

  8. uva 11324 The Largest Clique

    vjudge 上题目链接:uva 11324 scc + dp,根据大白书上的思路:" 同一个强连通分量中的点要么都选,要么不选.把强连通分量收缩点后得到SCC图,让每个SCC结点的权等于它 ...

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

    Problem B: The Largest Clique Given a directed graph G, consider the following transformation. First ...

随机推荐

  1. 深度分析DataTable如何筛选

    这项DataTable指SpryMedia专为HTML Table设计的插件页面和发展,官方网站:http://legacy.datatables.net/index. 左右oSettings.aiD ...

  2. Word001

    C# Word 类库 2009-08-06 22:10 13470人阅读 评论(10) 收藏 举报 c#objectstring文档microsoftexcel using System;using ...

  3. 慎重使用MySQL auto_increment

    在使用MySQL中,常常会在表中建立一个自增的ID字段,利用自增ID可以高速建立索引,也是MySQL官方比較推荐的一种方式,可是,这样的方式在大量数据且配置主从时,可能会出现因为自增ID导致同步失败的 ...

  4. MVC 中使用 SignalR 实现推送功能

    MVC 中使用 SignalR 实现推送功能 一,简介 Signal 是微软支持的一个运行在 Dot NET 平台上的 html websocket 框架.它出现的主要目的是实现服务器主动推送(Pus ...

  5. 将EBS设为首页worklist删除误报

    参考:How To Remove Error Notifications From The Worklist (Doc ID 357904.1) 1.1. Use one of the error n ...

  6. mysql语句在node.js中的写法

    总结一下mysql语句在node.js中的各种写法,参考了npm网站mysql模块给的实例. 查询 select //1 db.query('select * from tuanshang_users ...

  7. 笔试题&amp;面试题:输入一个维度,逆时针打印出一个指定矩阵

    称号:考虑到用户层面.打印出指定的矩阵,例如,一个给定的用户10,例如,下面的输出应被视为在图: 程序如下所示: #include <stdio.h> #include <mallo ...

  8. IBatis增删改差的实现以及注意点

    此次进讲述对表操作的实现细节.废话不多说,代码见真章. <?xml version="1.0" encoding="utf-8" ?> <sq ...

  9. 文件搜索神器 Everything

    Everything 是一款 NTFS 磁盘格式下的文件搜索工具,1月5日发布测试版本 1.3.0.631b Beta,增加文件列表.收藏夹.自定义快捷键.高级搜索等功能,取消了比较实用的 etp/f ...

  10. 怎样用C#代码管理SharePoint解决方式

    怎样用C#代码管理SharePoint解决方式         本文我们将了解怎样用代码管理SharePoint解决方式.我们使用server端对象模型抽取解决方式.         SharePoi ...