UVa 11324 The Largest Clique (强连通分量+DP)
题意:给定一个有向图,求一个最大的结点集,使得任意两个结点,要么 u 能到 v,要么 v 到u。
析:首先,如果是同一个连通分量,那么要么全选,要么全不选,然后我们就可以先把强连通分量先求出来,然后缩成一个点,然后该图就成了一个DAG,然后就可以直接用DP来做了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e15;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 50;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} vector<int> G[maxn];
int pre[maxn], lowlink[maxn], sccno[maxn];
int dfs_cnt, scc_cnt;
stack<int> S; void dfs(int u){
pre[u] = lowlink[u] = ++dfs_cnt;
S.push(u);
for(int i = 0; i < G[u].sz; ++i){
int v = G[u][i];
if(!pre[v]){
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}
else if(!sccno[v])
lowlink[u] = min(lowlink[u], pre[v]);
}
if(lowlink[u] == pre[u]){
++scc_cnt;
while(1){
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} void find_scc(int n){
dfs_cnt = scc_cnt = 0;
ms(pre, 0); ms(sccno, 0);
for(int i = 1; i <= n; ++i)
if(!pre[i]) dfs(i);
} vector<int> g[maxn];
int num[maxn]; int dp[maxn];
int ans;
bool vis[maxn]; void dfs1(int u){
dp[u] = 0;
for(int i = 0; i < g[u].sz; ++i){
int v = g[u][i];
dfs1(v);
dp[u] = max(dp[u], dp[v]);
}
dp[u] += num[u];
ans = max(ans, dp[u]);
} int main(){
int T; cin >> T;
while(T--){
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i) G[i].cl, g[i].cl;
for(int i = 0; i < m; ++i){
int u, v;
scanf("%d %d", &u, &v);
G[u].pb(v);
}
find_scc(n); ms(num, 0);
ms(vis, 0);
for(int i = 1; i <= n; ++i) ++num[sccno[i]];
for(int i = 1; i <= n; ++i)
for(int j = 0; j < G[i].sz; ++j){
int v = G[i][j];
if(sccno[i] != sccno[v]){
g[sccno[i]].pb(sccno[v]);
vis[sccno[v]] = 1;
}
}
ans = 0;
for(int i = 1; i <= scc_cnt; ++i)
if(!vis[i]) dfs1(i);
printf("%d\n", ans);
}
return 0;
}
UVa 11324 The Largest Clique (强连通分量+DP)的更多相关文章
- UVA 11324 The Largest Clique(强连通分量+缩点DAG的DP)
题意:给定一个有向图,求出一个最大的结点集,这个节点集中的随意两个点之间至少一个能到达还有一个点. 思路:假设一个点在这个节点集中,那么它所在的强连通分量中的点一定所有在这个节点集中,反之亦然, 求出 ...
- uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...
- UVA - 11324 The Largest Clique 强连通缩点+记忆化dp
题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...
- UVA - 11324 The Largest Clique (强连通缩点+dp)
题目链接 题意:从有向图G中找到一个最大的点集,使得该点集中任意两个结点u,v满足u可达v或v可达u. 解法:先把同处于一个强连通分量中的结点合并(缩点),得到一张DAG图,在DAG上dp即可. 感觉 ...
- UVA 11324 - The Largest Clique(强连通分量+缩点)
UVA 11324 - The Largest Clique 题目链接 题意:给定一个有向图,要求找一个集合,使得集合内随意两点(u, v)要么u能到v,要么v能到u,问最大能选几个点 思路:强连通分 ...
- UVA11324 The Largest Clique[强连通分量 缩点 DP]
UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直 ...
- UVA 11324 The Largest Clique (强连通分量,dp)
给出一个有向图,求一个最大的结点集合,任意两个点u,v.u可到达v或v可到达u. 一个强连通分量肯定一起选的.而且只能在一条路径上. 所以先找出所有scc,然后缩点找一条最大权的路径,按拓扑序跑DAG ...
- UVA11324 The Largest Clique —— 强连通分量 + 缩点 + DP
题目链接:https://vjudge.net/problem/UVA-11324 题解: 题意:给出一张有向图,求一个结点数最大的结点集,使得任意两个结点u.v,要么u能到达v, 要么v能到达u(u ...
- uva 11324 The Largest Clique
vjudge 上题目链接:uva 11324 scc + dp,根据大白书上的思路:" 同一个强连通分量中的点要么都选,要么不选.把强连通分量收缩点后得到SCC图,让每个SCC结点的权等于它 ...
随机推荐
- oracle用户解锁
创建一个概要文件 create profile frank_profile limit SESSIONS_PER_USER 5 IDLE_TIME 2 FAILED_LOGIN_ ...
- python开发_python中for循环操作
如果你对python中的for循环不是很清楚,请看看这篇文章:”for循环控制语句——菜鸟的Python笔记“ 下面是我做的一些学习记录供大家参考: #基本的for循环语句 test_list = [ ...
- 使用Spring MVC创建 REST API--2
1.提供资源之外的其他内容 @ResponseBody提供了一种很有用的方式,能够将控制器返回的Java对象转换为发送到客户端的资源表述.实际上,将资源表述发送给客户端只是整个过程的一部分.一个好的R ...
- 前端使用CryptoJs类库进行sha-256、MD5加密
Google的加密库 CryptoJs(点此下载) 包含了很多常用的加解密方式,包括AES.DES.SHA-1.SHA-2.SHA256.MD5等. DES对称加密在之前的文章中也有介绍过,大传送门. ...
- 留存- angularjs 弹出框 $modal
$modal只有一个方法:open,该方法的属性有: templateUrl:模态窗口的地址 template:用于显示html标签 scope:一个作用域为模态的内容使用(事实上,$modal会创建 ...
- PHP - declare tick
今天看php手册,结构流程控制语句,翻到个没见过的关键字declare(申明的意思).于是稍稍看了下. 大概意思是:定义一段代码.关于他合适执行,怎么执行完全依照他的参数. 反正不知道什么意思.先来一 ...
- visjs使用小记-2.option参数注释说明
对部分参数进行中文说明,后续补充 var options = { nodes:{//节点配置 borderWidth: 1,//节点边框的宽度,单位为px borderWidthSelected: 2 ...
- PHP中使用CURL模拟文件上传实例
调用实例: 该方法将本地的E盘文件test.doc上传到接口服务器上的 uploadFile方法中,uploadFile会对上传的文件做进一步处理. 若你想自己对上传的文件做操作,将接口uploadF ...
- VS2008调用VS2012的WCF服务的方式和遇到的问题
1 用添加服务引用的懒方式 2 用http请求方式 3 客户端自己定义一个Contract,跟服务端的一样(可以只写要使用的方法,不用全部写完). 由于规范要求,前两种都不能用,后面根据同事的描述,产 ...
- 02- 画文字和图片-------------之前写的那个微博项目,可以试试用画图片的方式来处理,这样应该比UILabel 代码少点,一会试试
1.画图片 - (void)drawRect:(CGRect)rect { // Drawing code UIImage *image = [UIImage imageNamed:@"pa ...