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. 如何尽量避免引用jQuery

    Introduction 正如jQuery所宣称的一样,Write Less, Do More.很多时候我们喜欢用它来解决问题.但增加一个库必然意味着更大的网络负担,意味着更高的页面初始载入时间.并且 ...

  2. ES-Mac OS环境搭建(1)

    前言 由于elasticsearch依赖Java,所以先要配置上Java环境,并且Java JDK必须要求1.8以上,这里以安装Java 1.8为例.安装环境如下: elasticsearch6.5. ...

  3. 能挣钱的微信JSSDK+H5混合开发

    H5喊了那么久,有些人都说不实用,有些人却利用在微信中开发H5应用赚得盆满钵满.微信JSSDK + HTML 5,让移动Web开发与微信结合轻而易举!跨平台.零成本,让大众创业变得更方便. 我觉得现在 ...

  4. idea 设置加载多个资源文件,显示本地图片

    idea 经常只会设置一个资源路径,这个路径就是项目的路径.但是当要加载的文件处于其他位置时,则需要增加虚拟路径的配置. 如图:第一个是项目路径 第二个是图片路径

  5. vijos 1034 家族(水题日常)

    描述 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系. 规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚.如果x,y是亲戚 ...

  6. (十四)maven之启动tomcat

    前言:在网上找了好几种方法启动web项目.比较好用的是:①在Project Facets勾上Dynamic....,但是这个方法会改变项目结构(把WebContent的东西都弄出来了):②使用jett ...

  7. UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)

    隐式的图搜索,存不下边,所以只有枚举转移就行了,因为bug的存在状态可以用二进制表示,转移的时候判断合法可以用位运算优化, 二进制pre[i][0]表示可以出现的bug,那么u&pre[i][ ...

  8. SVN中的check out与export的区别

    http://blog.csdn.net/zndxlxm/article/details/7763116 check out跟check in对应,export跟import对应. check out ...

  9. 机器学习(3)- 学习建议<误差出现如何解决?>

    根据Andrew Ng在斯坦福的<机器学习>视频做笔记,已经通过李航<统计学习方法>获得的知识不赘述,仅列出提纲. 1 学习建议 误差太大,如何改进? 使用更多的训练样本→解决 ...

  10. OpenWrite方法打开现有文件并进行写入

    实现效果: 知识运用: File类的OpenWrite方法 //实现打开现有文件以进行写入 public static FileStream OpenWrite (string path) Encod ...