UVA 11324.The Largest Clique tarjan缩点+拓扑dp
题目链接:https://vjudge.net/problem/UVA-11324
题意:求一个有向图中结点数最大的结点集,使得该结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相互可达也可以)。
思路:同一个强联通分量的结点集中任意两个结点u和v满足题的要求足:要么u可以到达v,要么v可以到达u(相互可达也可以)。把强联通分量收缩点后得到scc图,让每个scc结点的权值等于他的结点数,则求scc图上权最大的路径。拓扑dp,也可以直接bfs,但是要建立一个新的起点,连接所有入度为0的结点。
代码:
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=2e5+,INF=0x3f3f3f3f,MOD=1e9+;
map<int,vector<int> >G;
int pre[MAXN],lowlink[MAXN],sccno[MAXN],dfs_color,scc_cut;
stack<int>S;
map<int,vector<int> >NG;
int deg[MAXN];
int in[MAXN];
void dfs(int u)
{
pre[u]=lowlink[u]=++dfs_color;
S.push(u);
for(int i=; i<G[u].size(); i++)
{
int v=G[u][i];
if(!pre[v])
{
dfs(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
lowlink[u]=min(lowlink[u],lowlink[v]);
}
if(lowlink[u]==pre[u])
{
scc_cut++;
while(!S.empty())
{
int x=S.top();
S.pop();
sccno[x]=scc_cut;
deg[scc_cut]++;
if(x==u) break;
}
}
}
void find_scc(int n)
{
dfs_color=scc_cut=;
memset(pre,,sizeof(pre));
memset(sccno,,sizeof(sccno));
memset(deg,,sizeof(deg));
for(int i=; i<=n; i++)
if(!pre[i]) dfs(i);
}
void re_build(int n)
{
for(int i=; i<=scc_cut; i++) in[i]=,NG[i].clear();
for(int u=; u<=n; u++)
{
for(int i=; i<G[u].size(); i++)
{
int v=G[u][i];
if(sccno[u]==sccno[v]) continue;
in[sccno[v]]++;
NG[sccno[u]].push_back(sccno[v]);
}
}
for(int i=; i<=n; i++) G[i].clear();
}
queue<int>q;
int ans[MAXN];
void topsort()
{
memset(ans,,sizeof(ans));
for(int i=; i<=scc_cut; i++)
if(in[i]==) ans[i]=deg[i],q.push(i);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=; i<NG[u].size(); i++)
{
int v=NG[u][i];
ans[v]=max(ans[v],ans[u]+deg[v]);
in[v]--;
if(in[v]==) q.push(v);
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=; i<=m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
}
find_scc(n);
re_build(n);
topsort();
int Max=;
for(int i=; i<=scc_cut; i++)
Max=max(Max,ans[i]);
cout<<Max<<endl;
}
return ;
}
tarjan缩点+拓扑dp
UVA 11324.The Largest Clique tarjan缩点+拓扑dp的更多相关文章
- UVA - 11324 The Largest Clique 强连通缩点+记忆化dp
题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...
- UVA 11324 The Largest Clique(缩点+DAG上的dp)
求最大团.和等价性证明有类似之处,只不过这个不是求互推,而是只要a->b,或b->a即可. 同样的,容易想到先缩点,得到DAG,每个节点上保存SCC的点数,相信任意一条由根节点(入度为零) ...
- UVA - 11324 The Largest Clique (强连通缩点+dp)
题目链接 题意:从有向图G中找到一个最大的点集,使得该点集中任意两个结点u,v满足u可达v或v可达u. 解法:先把同处于一个强连通分量中的结点合并(缩点),得到一张DAG图,在DAG上dp即可. 感觉 ...
- UVA 11324 The Largest Clique (强连通分量,dp)
给出一个有向图,求一个最大的结点集合,任意两个点u,v.u可到达v或v可到达u. 一个强连通分量肯定一起选的.而且只能在一条路径上. 所以先找出所有scc,然后缩点找一条最大权的路径,按拓扑序跑DAG ...
- UVA 11324 - The Largest Clique(强连通分量+缩点)
UVA 11324 - The Largest Clique 题目链接 题意:给定一个有向图,要求找一个集合,使得集合内随意两点(u, v)要么u能到v,要么v能到u,问最大能选几个点 思路:强连通分 ...
- 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
vjudge 上题目链接:uva 11324 scc + dp,根据大白书上的思路:" 同一个强连通分量中的点要么都选,要么不选.把强连通分量收缩点后得到SCC图,让每个SCC结点的权等于它 ...
- 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 (Tarjan+记忆化)
/*每个环 要么不选 要么全选 可缩点 就得到一个GAD图 然后搞搞算出最大路径*/ #include<iostream> #include<cstdio> #include& ...
随机推荐
- oracle 连接池参数
后来排查出数据库监听异常,发现是ORA-12519拒绝错误.后来发现是数据的连接池达到的极致. 具体解决方案如下: --首先检查process和session的使用情况,在sqlplus里面查看. S ...
- 编译模块的Makefile解析
Makefile # if not defined KERNELRELEASE, command is running from command line,need invoke kbuild sys ...
- shell脚本-删除当天日期前3个月的数据表
#!/bin/bash #author:skycheng #get current date string datestr=`date +'%Y-%m-%d'` start_time=`date +' ...
- 安装linux版zabbix客户端
安装linux版zabbix客户端 一.下载客户端 查看centos系统内核版本 cat /proc/version 如上图,就选择Linux 2.6系统对应的agent版本程序 打开官网:https ...
- Android无法访问本地服务器(localhost/127.0.0.1)的解决方案
[Android无法访问本地服务器(localhost/127.0.0.1)的解决方案] 在Android开发中通过localhost或127.0.0.1访问本地服务器时,会报Java.NET.Con ...
- Docker容器管理及代码调用
这篇文章主要讲解Docker的容器管理,实现服务的部署,以Redis为例.我用的是Utuntu16.04,所以软件直接从库中下载,库中的Docker不是最新版本.但是不影响部署,如需要最新可在官网下载 ...
- swiper轮播的slide高度自适应
方式1:官方给的属性 autoHeight: true, //高度随内容变化 发现实际没效果 方式2:先定义了一个slide的高度数组, //设置slide父级高度 index为slide的索引 fu ...
- 相对熵(KL散度)
https://blog.csdn.net/weixinhum/article/details/85064685 上一篇文章我们简单介绍了信息熵的概念,知道了信息熵可以表达数据的信息量大小,是信息处理 ...
- 中国剩余定理模板 51nod 1079
题目链接:传送门 推荐博客:https://www.cnblogs.com/freinds/p/6388992.html (证明很好,代码有误). 1079 中国剩余定理 基准时间限制:1 秒 空间 ...
- Shell教程 之函数
1.函数定义 shell中函数的定义格式如下: [ function ] funname [()] { action; [return int;] } 说明: 可以带function fun() 定义 ...