UVA 11324 The Largest Clique(缩点+DAG上的dp)
求最大团。和等价性证明有类似之处,只不过这个不是求互推,而是只要a->b,或b->a即可。
同样的,容易想到先缩点,得到DAG,每个节点上保存SCC的点数,相信任意一条由根节点(入度为零)出发的路径中权值和最大的即为所求,dp即可解决。
#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
using namespace std; const int MAXN=;
const int MAXM=; struct Edge{
int v,next;
}edge[MAXM]; stack<int >stk;
int head[MAXN],tol;
int low[MAXN],pre[MAXN],sccno[MAXN],scc_cnt,TT,sccnum[MAXN];
int dp[MAXN]; void init()
{
tol=;
memset(head,-,sizeof(head));
} void add(int u,int v)
{
edge[tol].v=v;
edge[tol].next=head[u];
head[u]=tol++;
} void dfs(int u)
{
int v;
low[u]=pre[u]=++TT;
stk.push(u);
for(int i=head[u];i!=-;i=edge[i].next)
{
v=edge[i].v;
if(!pre[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}else if(!sccno[v])
low[u]=min(low[u],pre[v]);
}
if(low[u]==pre[u]){
scc_cnt++;
int s=;
do{
v=stk.top();
stk.pop();
sccno[v]=scc_cnt;
s++;
}while(u!=v);
sccnum[scc_cnt]=s;
}
} void tarjan(int n)
{
scc_cnt=TT=;
memset(low,,sizeof(low));
memset(pre,,sizeof(pre));
memset(sccno,,sizeof(sccno)); for(int i=;i<=n;i++)
if(!pre[i])
dfs(i);
} int find_dfs(int u){
if(dp[u])
return dp[u];
else if(head[u]==-)
return dp[u]=sccnum[u]; int m=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
m=max(m,find_dfs(v));
}
return dp[u]=sccnum[u]+m;
} int main()
{
int T,n,m;
int a[MAXM],b[MAXM];
int in[MAXN];
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m); init();
for(int i=;i<=m;i++)
{
scanf("%d%d",&a[i],&b[i]);
add(a[i],b[i]);
}
tarjan(n); init();
memset(in,,sizeof(in));
for(int i=;i<=m;i++)
{
if(sccno[a[i]]!=sccno[b[i]]){
in[sccno[b[i]]]++;
add(sccno[a[i]],sccno[b[i]]);
}
}
int s=;
memset(dp,,sizeof(dp));
for(int i=;i<=scc_cnt;i++)
if(!in[i])
s=max(s,find_dfs(i));
printf("%d\n",s);
}
return ;
}
UVA 11324 The Largest Clique(缩点+DAG上的dp)的更多相关文章
- Uva 11324 The Largest Clique【强连通 DAG动规 spfa】
白书上的例题 做一遍tarjan后,缩点,每一个scc节点的权为它的结点数,做一次DAG上的动规,求出路径上的最大点权和,就可以了 #include<cstdio> #include< ...
- UVA 11324 - The Largest Clique(强连通分量+缩点)
UVA 11324 - The Largest Clique 题目链接 题意:给定一个有向图,要求找一个集合,使得集合内随意两点(u, v)要么u能到v,要么v能到u,问最大能选几个点 思路:强连通分 ...
- UVA 11324 The Largest Clique (强连通分量,dp)
给出一个有向图,求一个最大的结点集合,任意两个点u,v.u可到达v或v可到达u. 一个强连通分量肯定一起选的.而且只能在一条路径上. 所以先找出所有scc,然后缩点找一条最大权的路径,按拓扑序跑DAG ...
- uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...
- UVA 11324 The Largest Clique(强连通分量+缩点DAG的DP)
题意:给定一个有向图,求出一个最大的结点集,这个节点集中的随意两个点之间至少一个能到达还有一个点. 思路:假设一个点在这个节点集中,那么它所在的强连通分量中的点一定所有在这个节点集中,反之亦然, 求出 ...
- UVA - 11324 The Largest Clique (强连通缩点+dp)
题目链接 题意:从有向图G中找到一个最大的点集,使得该点集中任意两个结点u,v满足u可达v或v可达u. 解法:先把同处于一个强连通分量中的结点合并(缩点),得到一张DAG图,在DAG上dp即可. 感觉 ...
- uva 11324 The Largest Clique
vjudge 上题目链接:uva 11324 scc + dp,根据大白书上的思路:" 同一个强连通分量中的点要么都选,要么不选.把强连通分量收缩点后得到SCC图,让每个SCC结点的权等于它 ...
- uva 11324 The Largest Clique(图论-tarjan,动态规划)
Problem B: The Largest Clique Given a directed graph G, consider the following transformation. First ...
- UVA - 11324 The Largest Clique 强连通缩点+记忆化dp
题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...
随机推荐
- latex排版系统
proTeXt - MiKTeX-based distribution for Windows proTeXt aims to be an easy-to-install TeX distributi ...
- static任我行- 为人不注意的static
前几天一直在想,static方法如果没有被调用,会不会分配内存的问题,查了一下资料,终于得到了一个官方的说法了. static 方法调用:使用比较频繁的时候使用,像数据库连接串,Connection ...
- UIView和CALayer是什么关系?
UIView显示在屏幕上归功于CALayer,通过调用drawRect方法来渲染自身的内容,调节CALayer属性可以调整UIView的外观,UIView继承自UIResponder,比起CALaye ...
- 《Cracking the Coding Interview》——第5章:位操作——题目1
2014-03-19 05:45 题目:给定两个数M和N,将N按照二进制位,覆盖到M的特定段位中去. 解法:位操作,请看代码. 代码: // 5.1 Insert one number into th ...
- Windows Phone 图片扩展类
using System.IO; using System.Text; using System.Net; using System.Threading.Tasks; using System.Win ...
- Selenium+Python自动化之如何绕过登录验证码
一.使用Fiddler抓包 1.一般登陆网站成功后,会生成一个已登录状态的cookie,那么只需要直接把这个值拿到,用selenium进行addCookie操作即可. 2.可以先手动登录一次,然后抓取 ...
- mysql:用户管理、索引、视图、函数、存储过程
#创建一个用户并设置密码,注意IP地址要是登录mysql电脑的IP地址 USE mysql CREATE USER lisi@'192.168.149.1' IDENTIFIED BY "1 ...
- SQL 与关系代数
Table of Contents 前言 关系与表 关系代数的基本运算 投影 选择 并运算 集合差运算 笛卡尔积 更名运算 关系代数的附加运算 集合交运算 连接运算 自然连接 内连接 外连接 结语 前 ...
- 解决Navicat for MySQL 连接 Mysql 8.0.11 出现1251- Client does not support authentication protocol 错误
安装MySQL8.0之后,使用Navicat连接数据库,报1251错误. 上网搜索解决方案,网上说出现这种情况的原因是:mysql8 之前的版本中加密规则是mysql_native_password, ...
- Where can I find the IPA logs
Retrieving the IPA logs will differ depending on which base image was used. Operating system that do ...