图论trainning-part-2 C. The Largest Clique
C. The Largest Clique
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的更多相关文章
- uva 11324 The Largest Clique(图论-tarjan,动态规划)
Problem B: The Largest Clique Given a directed graph G, consider the following transformation. First ...
- UVA11324 The Largest Clique[强连通分量 缩点 DP]
UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直 ...
- UVA 11324 - The Largest Clique(强连通分量+缩点)
UVA 11324 - The Largest Clique 题目链接 题意:给定一个有向图,要求找一个集合,使得集合内随意两点(u, v)要么u能到v,要么v能到u,问最大能选几个点 思路:强连通分 ...
- 【UVA11324】 The Largest Clique (Tarjan+topsort/记忆化搜索)
UVA11324 The Largest Clique 题目描述 给你一张有向图 \(G\),求一个结点数最大的结点集,使得该结点集中的任意两个结点 \(u\) 和 \(v\) 满足:要么 \(u\) ...
- 『题解』UVa11324 The Largest Clique
原文地址 Problem Portal Portal1:UVa Portal2:Luogu Portal3:Vjudge Description Given a directed graph \(\t ...
- UVAoj 11324 - The Largest Clique(tarjan + dp)
题意:给定一个有向图,寻找一个点数最大集合,使得这个集合中的任意两个点 u,v, 都有u->v 或者 v->u 或者u<==>v 思路:首先将强连通分量通过tarjan算法求出 ...
- UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)
题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...
- 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 ...
随机推荐
- AJPFX关于单例设计模式
单例设计模式优势:保证一个类在内存中的对象唯一性. 比如:多程序读取一个配置文件时,建议配置文件封装成对象.会方便操作其中数据,又要保证多个程序读到的是同一个配置文件对象,就需要该配置文件对象在内存中 ...
- BZOJ1485: [HNOI2009]有趣的数列(Catalan数,质因数分解求组合数)
题意 挺简洁的. 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…<a ...
- Web开发入门不得不看章
引 如今,各种互联网的Web应用程序层出不穷,那么如何快速入门,成长为一个优秀的Web开发工作者呢? 这个问题不容易回答,几乎所有的培训机构都不能清晰地解答. 所以对于Web开发刚刚入门的菜鸟们,我觉 ...
- Java 堆内存和栈内存
看了一些别人总结的博客,感觉对堆内存和栈内存有了一个初步的认识.所以来写写自己对堆内存和栈内存的理解. Java把内存分成两种,一种叫做栈内存,一种叫做堆内存. 在函数中定义的一些基本类型的变量和对象 ...
- Spring Boot :Druid Monitor
忙里偷个闲,在这里分享一下SpringBoot集成Druid实现数据库监控功能,有什么错误欢迎大家指出! 参考文件: Spring实现Druid监控:https://www.cnblogs.com/w ...
- C# 一维数组 二位数组 多维数组
什么是数组? 数组是一组变量,就是把一些变量串在一起,放在一块. 数组的作用? 假设有一堆变量,每个变量都有一些程序,那么这堆程序放在一起 程序就会混乱,处理起来有些麻烦,那么数组就是把这些变量放在 ...
- Servlet和JSP之有关Servlet和JSP的梳理(一)
大二第一学期的时候有学JSP的课,但是因为在开学之前做过JSP的小项目,所以一个学期的课也没听,直到期末考试成绩出来了,才回想JSP的内容还有多少记得,没想到模模糊糊也记不起多少,赶紧回头学回来.接下 ...
- 使用python批量建立文件
for i in range(101,110): n = repr(i) + '.txt' file = open('c:\\ip\\' + n, 'w')
- 将回车键转换为Tab键
实现效果: 知识运用: KeyEventArgs类的KeyValue属性 public int KeyValue {get;} //获取KeyDown或KeyUp事件的键盘值 SendKeys类的Se ...
- rf统计条数
js模式 直接引用关键字模式