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. 用户会话跟踪机制(session+cookie)

    最近在优化之前给学校写的一个项目,发现了同一个浏览器(IE,Firefox)开多个选项卡的时候不能登录多个用户,后一个登录用户会把前一个用户给覆盖了,我的登录逻辑是把user对象存放到session中 ...

  2. leetcode140 Word Break II

    思路: 直接爆搜会超时,需要使用记忆化搜索.使用map把已经计算过的情况记录下来,避免重复计算. 实现: class Solution { public: vector<string> w ...

  3. 从零开始利用vue-cli搭建简单音乐网站(二)

    1.利用vue-router实现页面跳转 程序可以正常运行之后,下面我们需要配置路由实现页面的局部刷新,这一功能将用来实现网站页面的跳转. 打开程序目录,进入"src\router\inde ...

  4. MySQL报错竞技赛

    以下报错,我几乎没出过几个. ERROR 2 系统找不到文件: mysql-5.6.1X默认的配置文件是在C:\Program Files\MySQL\MySQL Server 5.6\my-defa ...

  5. 目后佐道IT教育:师资团队

    高端技术顾问 1. leepoor 拥有12年的Web开发和架构经验,在阿里巴巴担任高级架构师,参与阿里巴巴基础技术平台开发和www.alibaba.com架构设计.擅长大型网站技术架构,工作中经常使 ...

  6. iOS上架问题解决

    dns问题 http://iphone.91.com/tutorial/syjc/140509/21686339.html 网络问题 手机4g开wifi,上传提交多次 时间问题 东八区下午6点上架成功 ...

  7. afnetworking NSCocoaErrorDomain Code=3840 解决

    afnetworking json解析出错 解决方法1 AFURLResponseSerialization.m 258行修改 responseString = [responseString str ...

  8. Luogu P4463 [国家集训队] calc

    WJMZBMR的题果然放在几年后看来仍然挺神,提出了一种独特的优化DP的方式 首先我们想一个暴力DP,先定下所有数的顺序(比如强制它递增),然后最后乘上\(n!\)种排列方式就是答案了 那么我们容易想 ...

  9. jni 修bug

     1. ReferenceTable overflow (max=512)  内存泄露,程序运行一段时间就挂掉了. 在利用反射调用java中的函数需要释放掉查找到的类 void publishJava ...

  10. Linux网络管理及基础设置

    一.网络管理 1 临时配置网络(ip,网关,dns) 用ifconfig命令设定网卡的IP地址: ens33网卡的IP地址为192.168.16.154, ifconfig ens33 192.168 ...