uva 11324 The Largest Clique(图论-tarjan,动态规划)
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 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;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
uva 11324 The Largest Clique(图论-tarjan,动态规划)的更多相关文章
- uva 11324 The Largest Clique (Tarjan+记忆化)
/*每个环 要么不选 要么全选 可缩点 就得到一个GAD图 然后搞搞算出最大路径*/ #include<iostream> #include<cstdio> #include& ...
- UVA 11324 - The Largest Clique(强连通分量+缩点)
UVA 11324 - The Largest Clique 题目链接 题意:给定一个有向图,要求找一个集合,使得集合内随意两点(u, v)要么u能到v,要么v能到u,问最大能选几个点 思路:强连通分 ...
- 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缩点+拓扑dp
题目链接:https://vjudge.net/problem/UVA-11324 题意:求一个有向图中结点数最大的结点集,使得该结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相 ...
- 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)
给出一个有向图,求一个最大的结点集合,任意两个点u,v.u可到达v或v可到达u. 一个强连通分量肯定一起选的.而且只能在一条路径上. 所以先找出所有scc,然后缩点找一条最大权的路径,按拓扑序跑DAG ...
- Uva 11324 The Largest Clique【强连通 DAG动规 spfa】
白书上的例题 做一遍tarjan后,缩点,每一个scc节点的权为它的结点数,做一次DAG上的动规,求出路径上的最大点权和,就可以了 #include<cstdio> #include< ...
随机推荐
- 命令模式在MVC框架中的应用
事实上在项目开发中,我们使用了大量的设计模式,不过这些设计模式都封装在框架中了,假设你想要不只局限于简单的使用,就应该深入了解框架的设计思路. 在MVC框架中,模式之中的一个就是命令模式,先来看看模式 ...
- Android之场景桌面(一)
声明:转载请务必注明出处,本文代码和主题仅供学习交流,请勿用于商业用途. 引言:最近Android场景桌面开始流行起来了,跟原始的Android桌面相比,场景桌面能逼真的模拟各种自然物体,并且通过点击 ...
- java单例模式(线程安全,效率高,双重推断)
这样的方法,在获取单利的时候,避免了线程锁,导致訪问该方法速度非常慢, 同是,防止了多线程同事房屋该方法就会产生多个实例的问题. 效率高.线程安全. public class TestInstance ...
- 用Javascript评估用户输入密码的强度(Knockout版)
原文:用Javascript评估用户输入密码的强度(Knockout版) 早上看到博友6点多发的一篇关于密码强度的文章(连接),甚是感动(周末大早上还来发文). 我们来看看如果使用Knockout更简 ...
- SVN的revert和update命令的区别
svn中的revert和update 今天有人问到revert和update的问题. 刚开始还真被问住了. 因为感觉revert和update都可以将本地的copy更新到以前的一个版本,会有什么不同呢 ...
- 在mysql数据库中关于日期时间字段的处理
在mysql数据库中关于日期时间字段的处理 在开发中,日期时间字段一般有如下几种设计 假设要获取2013-08-15日到2013-08-16日之间的记录 1. 直接使用日期时间类字段 相关sql语句如 ...
- Codeforces Jzzhu and Sequences(圆形截面)
# include <stdio.h> int f[10]; int main() { int x,y,n,j; while(~scanf("%d%d%d",& ...
- Java 里把 InputStream 转换成 String 的几种方法
我们在 Java 中经常会碰到如何把 InputStream 转换成 String 的情形,比如从文件或网络得到一个 InputStream,需要转换成字符串输出或赋给别的变量. 未真正关注这个问题之 ...
- 工厂方法模式(factory method pattern)
工厂方法模式相对于简单工厂模式的长处是为了更好的拓展,当假设再新加一种产品,对于简单工厂模式来说须要改动核心的工厂类,但对于工厂方法模式则不须要,在工厂方法模式中核心的工厂类不再负责创建全部产品的创建 ...
- XP下採用DirectShow採集摄像头
转载请标明是引用于 http://blog.csdn.net/chenyujing1234 欢迎大家提出意见,一起讨论! 须要演示样例源代码的请独自联系我. 前提: 摄像头能正常工作.摄像头有创建di ...