uva-11324-SCC+dp
https://vjudge.net/problem/UVA-11324
给出一幅有向图,问最大能找到多少个节点,使得这些节点中任意两个节点之间都至少有一条可达路径。
找出SCC后缩点求权重最大路即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<deque>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<cstdlib>
#include<ctype.h>
#include<ctime>
#include<functional>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define debug puts("debug")
#define mid ((L+R)>>1)
#define lc (id<<1)
#define rc (id<<1|1)
const int maxn=;
const int maxm=;
const double PI=acos(-1.0);
const double eps=1e-;
const LL mod=1e9+;
LL gcd(LL a,LL b){return b==?a:gcd(b,a%b);}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL qpow(LL a,LL b,LL c){LL r=; for(;b;b>>=,a=a*a%c)if(b&)r=r*a%c;return r;}
template<class T>
void prt(T v){for(auto x:v)cout<<x<<' ';cout<<endl;}
struct Edge{int u,v,w,next;}; vector<int>g[maxn],g2[maxn];
int f[maxn],dfn[maxn],low[maxn],scc[maxn],tot[maxn],sum=,scc_cnt=;
stack<int>S;
void dfs(int u){
dfn[u]=low[u]=++sum;
S.push(u);
for(int v:g[u]){
if(!dfn[v]){
dfs(v),low[u]=min(low[u],low[v]);
}else if(!scc[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
++scc_cnt;
for(;;){
int x=S.top();S.pop();
scc[x]=scc_cnt;
tot[scc_cnt]++;
if(x==u)break;
}
}
}
int cal(int u){
if(f[u]!=-)return f[u];
f[u]=tot[u];
int maxx=;
for(int v:g2[u]){
maxx=max(maxx,cal(v));
}
return f[u]=f[u]+maxx;
}
int main(){
int t,n,m,i,j,u,v;
cin>>t;
while(t--){
scanf("%d%d",&n,&m);
for(i=;i<=n;++i)g[i].clear(),g2[i].clear(),tot[i]=low[i]=dfn[i]=scc[i]=;
sum=scc_cnt=;
while(!S.empty())S.pop();
while(m--){
scanf("%d%d",&u,&v);
g[u].pb(v);
}
for(i=;i<=n;++i){
if(!dfn[i]){
dfs(i);
}
}
memset(f,-,sizeof(f));
for(u=;u<=n;++u){
for(int v:g[u]){
if(scc[u]!=scc[v]){
//cout<<scc[v]<<' '<<scc[u]<<
g2[scc[v]].pb(scc[u]);
}
}
}
int ans=;
for(i=;i<=scc_cnt;++i){
f[i]=cal(i);
if(f[i]>ans)ans=f[i];
}
cout<<ans<<endl;
}
return ;
}
/*
1
5 5
1 2
2 3
3 1
4 1
5 2 */
uva-11324-SCC+dp的更多相关文章
- 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...
- uva 11324 The Largest Clique
vjudge 上题目链接:uva 11324 scc + dp,根据大白书上的思路:" 同一个强连通分量中的点要么都选,要么不选.把强连通分量收缩点后得到SCC图,让每个SCC结点的权等于它 ...
- UVA 11324 - The Largest Clique(强连通分量+缩点)
UVA 11324 - The Largest Clique 题目链接 题意:给定一个有向图,要求找一个集合,使得集合内随意两点(u, v)要么u能到v,要么v能到u,问最大能选几个点 思路:强连通分 ...
- UVA.10192 Vacation (DP LCS)
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- UVA 11324.The Largest Clique tarjan缩点+拓扑dp
题目链接:https://vjudge.net/problem/UVA-11324 题意:求一个有向图中结点数最大的结点集,使得该结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相 ...
- UVA - 11324 The Largest Clique (强连通缩点+dp)
题目链接 题意:从有向图G中找到一个最大的点集,使得该点集中任意两个结点u,v满足u可达v或v可达u. 解法:先把同处于一个强连通分量中的结点合并(缩点),得到一张DAG图,在DAG上dp即可. 感觉 ...
- UVA 11324 The Largest Clique(缩点+DAG上的dp)
求最大团.和等价性证明有类似之处,只不过这个不是求互推,而是只要a->b,或b->a即可. 同样的,容易想到先缩点,得到DAG,每个节点上保存SCC的点数,相信任意一条由根节点(入度为零) ...
- UVA 11324 The Largest Clique (强连通分量,dp)
给出一个有向图,求一个最大的结点集合,任意两个点u,v.u可到达v或v可到达u. 一个强连通分量肯定一起选的.而且只能在一条路径上. 所以先找出所有scc,然后缩点找一条最大权的路径,按拓扑序跑DAG ...
- UVa 11324 & 强联通分量+DP
题意: 一张无向图,求点集使其中任意两点可到达. SOL: 强联通分量中的点要么不选要么全都选,然后缩点DAG+DP 记录一下思路,不想写了...代码满天飞.
随机推荐
- Ubuntu yindaoxiufu 引导修复(Boot Repair)
Ubuntu yindaoxiufu 引导修复(Boot Repair) from: http://blog.csdn.net/piaocoder/article/details/50589667 ...
- Derek解读Bytom源码-创世区块
作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...
- AngularJS 笔记1
2017-03-23 本文更新链接: http://www.cnblogs.com/daysme/p/6606805.html 什么是 angularjs 2009年有两个外国人创建,后由谷歌收购并开 ...
- 【译】第42节---EF6-DbSet.AddRange & DbSet.RemoveRange
原文:http://www.entityframeworktutorial.net/entityframework6/addrange-removerange.aspx EF 6中的DbSet引入了新 ...
- PHP 内置函数fgets读取文件
php fgets()函数从文件指针中读取一行 语法: fgets(file,length) 参数 描述 file 必需.规定尧要读取的文件 length 可选 .规定尧都区的字节数.默认是102字 ...
- mapgis IGServer账号
2064803644@qq.com 1576391020@qq.com 密码一样
- 【Ruby】【高级编程】面向对象
# [[面向对象]]#[实例变量]=begin实例变量是类属性,它们在使用类创建对象时就编程对象的属性.每个对象的属性是单独赋值的,和其他对象之间不共享.在类的内部,使用@运算符访问这些属性,在类的外 ...
- python (协程)生产者,消费者
#coding=utf- import gevent from gevent.queue import Queue, Empty import time tasks = Queue(maxsize=) ...
- VC.时间(网页内容收集)
1.VC++获得当前系统时间的几种方案_记忆53秒_新浪博客.html(http://blog.sina.com.cn/s/blog_676271a60101i0hb.html) 1.1.内容保存: ...
- OpenModelica中simulate的用法
先把官网上的说明文档放上来: simulate simulates a modelica model by generating c code, build it and run the simula ...