C. The Largest Clique

Time Limit: 3000ms
Memory Limit: 131072KB

64-bit integer IO format: %lld      Java class name: Main

 

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 nand 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 ofG 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

解题:强连通子图的求解,缩点,DAG上的动态规划。先求出所有的强连通子图后,再对各个强连通子图进行缩点,所谓缩点,即为把这个强连通块作为一个点,进行新图的建立。原来图上的任意一点必然属于某个强连通块。所以根据各点所在的连通块,进行新图的建立,注意方向性,DAG上的动态规划是对于有向图而言的,所以必须保证方向的正确性。建立新图后,求DAG上的最长路径即可。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int low[maxn],dfn[maxn],iindex,sccBlocks;
bool instack[maxn],vis[maxn];
int belong[maxn],val[maxn],dp[maxn],n,m;
stack<int>s;
vector<int>g[maxn];
vector<int>mp[maxn];
void tarjan(int u){
dfn[u] = low[u] = ++iindex;
instack[u] = true;
s.push(u);
for(int i = ; i < g[u].size(); i++){
int v = g[u][i];
if(!dfn[v]){
tarjan(v);
low[u] = min(low[u],low[v]);
}else if(instack[v] && low[u] > dfn[v]) low[u] = dfn[v];
}
if(dfn[u] == low[u]){
int v;
sccBlocks++;
do{
v = s.top();
s.pop();
instack[v] = false;
belong[v] = sccBlocks;
}while(u != v);
}
}
int dag(int u){
if(dp[u]) return dp[u];
else if(mp[u].size() == ) return dp[u] = val[u];
int ans = ;
for(int v = ; v < mp[u].size(); v++){
ans = max(ans,dag(mp[u][v]));
}
return dp[u] = ans+val[u];
}
int main(){
int t,u,v,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i = ; i <= n; i++){
g[i].clear();
dfn[i] = low[i] = ;
instack[i] = false;
val[i] = belong[i] = ;
dp[i] = ;
mp[i].clear();
}
for(i = ; i < m; i++){
scanf("%d%d",&u,&v);
g[u].push_back(v);
}
iindex = sccBlocks = ;
for(i = ; i <= n; i++)
if(!dfn[i]) tarjan(i);
for(u = ; u <= n; u++){
val[belong[u]]++;
memset(vis,false,sizeof(vis));
for(j = ; j < g[u].size(); j++){
v = g[u][j];
if(!vis[belong[v]] && belong[v] != belong[u]){
vis[belong[v]] = true;
mp[belong[u]].push_back(belong[v]);
}
}
}
int ans = ;
for(i = ; i <= sccBlocks; i++)
ans = max(ans,dag(i));
printf("%d\n",ans);
}
return ;
}

图论trainning-part-2 C. The Largest Clique的更多相关文章

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

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

  2. UVA11324 The Largest Clique[强连通分量 缩点 DP]

    UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直 ...

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

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

  4. 【UVA11324】 The Largest Clique (Tarjan+topsort/记忆化搜索)

    UVA11324 The Largest Clique 题目描述 给你一张有向图 \(G\),求一个结点数最大的结点集,使得该结点集中的任意两个结点 \(u\) 和 \(v\) 满足:要么 \(u\) ...

  5. 『题解』UVa11324 The Largest Clique

    原文地址 Problem Portal Portal1:UVa Portal2:Luogu Portal3:Vjudge Description Given a directed graph \(\t ...

  6. UVAoj 11324 - The Largest Clique(tarjan + dp)

    题意:给定一个有向图,寻找一个点数最大集合,使得这个集合中的任意两个点 u,v, 都有u->v 或者 v->u 或者u<==>v 思路:首先将强连通分量通过tarjan算法求出 ...

  7. UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)

    题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...

  8. uva 11324 The Largest Clique

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

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

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

随机推荐

  1. nginx+ssl+pm2 部署 nodejs 服务

    nginx+pm2 部署 nodejs 服务 最近在 centos 上部署 nodejs 服务,记下来步骤: 注意都是使用 root 用户. 下载 nvm: curl -o- https://raw. ...

  2. Web开发入门不得不看章

    引 如今,各种互联网的Web应用程序层出不穷,那么如何快速入门,成长为一个优秀的Web开发工作者呢? 这个问题不容易回答,几乎所有的培训机构都不能清晰地解答. 所以对于Web开发刚刚入门的菜鸟们,我觉 ...

  3. BeanUtils 工具类

    一.BeanUtils 概述     BeanUtils 是阿帕奇提供的一套专门用于将一些数据封装到java对象中的工具类;          名词:javaBean:特定格式的java类称为java ...

  4. cat 参数

    -A 相当于-vET的整合参数 -E 将结尾的换行符$显示出来 -n 显示行号 -T 将tab键以^T显示出来 -v 列出一些看不出来的特殊字符

  5. Ubuntu下软件的搜索与安装

    本文为笔者原创,首发于简书(点击这里查看). 小白玩转linux的第一个拦路虎就是软件的安装了.本文结合自己在Ubuntu14.04下软件安装经验做一个总结. 1.如何搜索软件? apt-cache ...

  6. PHP高端课程

    关于目后佐道IT教育 http://www.cnblogs.com/itpua/p/7710917.html 目后佐道IT教育的师资团队 http://www.cnblogs.com/itpua/p/ ...

  7. WPF知识点全攻略04- XAML页面布局

    名称 说明 Canvas 使用固定坐标绝对定位元素 StackPanel 在水平或竖直方向放置元素 DockPanel 根据外部容器边界,自动调整元素 WrapPanel 在可换行的行中放置元素 Gr ...

  8. caffe修改需要的东西

    https://blog.csdn.net/zhaishengfu/article/details/51971768?locationNum=3&fps=1

  9. JS设置组合快捷键

    为提升用户体验,想要在web页面中通过组合快捷键调出用户帮助页面,具体实现思路是监听keyup事件,在相应的处理函数中进行逻辑编写,代码如下 $(document).keyup(function (e ...

  10. OpenCV2.4.11+VS2012的环境配置+“fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突”的问题解决

    本来OpenCV环境配置的问题是个基础问题,但是步骤有点小烦,所以几乎每次都要百度一下,加上这次遇到的“fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突 ...