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. asp。Net 页面传值

    00.引言 Web页面是无状态的, 服务器对每一次请求都认为来自不同用户,因此,变量的状态在连续对同一页面的多次请求之间或在页面跳转时不会被保留.在用ASP.NET 设计开发一个Web系统时, 遇到一 ...

  2. 接口文档管理工具rap

    git地址:  https://github.com/thx/RAP wiki : https://github.com/thx/RAP/wiki/home_cn 视频教程: http://thx.g ...

  3. 流行的9个Java框架介绍: 优点、缺点等等

    流行的9个Java框架介绍: 优点.缺点等等 在 2018年,Java仍然是世界上最流行的编程语言.它拥有一个巨大的生态系统,在全世界有超过900万Java开发人员.虽然Java不是最直接的语言,但是 ...

  4. 用好js与nodejs中的try...catch

    对异常的捕获和处理是提高程序鲁棒性的一个重要方式,即使在javascript/nodejs等看似“很难写出bug”的弱类型语言里,异常捕获处理仍至关重要,这主要是因为: 1.在一个代码块里,如果程序运 ...

  5. url post 请求方法

    最近的项目是给手机app 提供方法. 因此 此方法可以进行接口测试 static class HttpClient { static CookieContainer cookies = new Coo ...

  6. Node.js 打造实时多人游戏框架

    在 Node.js 如火如荼发展的今天,我们已经可以用它来做各种各样的事情.前段时间UP主参加了极客松活动,在这次活动中我们意在做出一款让“低头族”能够更多交流的游戏,核心功能便是 Lan Party ...

  7. Maven常见知识介绍

    1)pom详解 2)pom详解 3)测试 4)插件与生命周期 5)maven生命周期 6)范围依赖

  8. 00_HTTP协议介绍

    1. 什么是HTTP协议 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到 ...

  9. C#入门(2)

    C#入门(2) Exception 基本异常的核心成员: System.Exception Property Meaning Data read-only,实现了IDirectory接口的一些键值对, ...

  10. httpmodule初识

    .net的请求流程: HttpRequest-->inetinfo.exe->ASPNET_ISAPI.DLL-->Http Pipeline-->ASPNET_WP.EXE- ...