HDU-2767-tarjan/Kosaraju求scc
http://acm.hdu.edu.cn/showproblem.php?pid=2767
问最少添加几条边使得图为强连通。
tarjan跑一下,然后对强连通分量缩点,找下此时出度为零和入度为零的点数输出较大者即可。
tarjan,dfn数组也同时起到了标记的作用,如果未标记说明此时的边就是dfs树上的边,否则的话,如果v已经处于某个scc中说明这条边是交叉边,不用考虑(因为一定无对应的回边,有的话这个点就应该和v属于同一个scc,与过程矛盾)。否则就是回边了,和求桥和割点的方法类似求dfn以及low数组。
最后如果low[u]==dfn[u]说明u沿着他的孩子能回到他自己,这就形成了一个环,环上的所有点构成了一个scc,不断出栈直到u出现为止统计下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;}; int dfn[maxn],low[maxn],scc[maxn],scc_cnt,sum;
int in[maxn],out[maxn];
vector<int>g[maxn];
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;
int x=-;
for(;;){
x=S.top();S.pop();
scc[x]=scc_cnt;
if(x==u)break;
}
}
}
int main(){
int t,n,m,i,j,k,u,v;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=;i<=n;++i)g[i].clear(),dfn[i]=low[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);
}
}
if(scc_cnt==){
cout<<<<endl;
continue;
}
for(i=;i<=scc_cnt;++i)in[i]=out[i]=;
for(u=;u<=n;++u){
for(int v:g[u]){
if(scc[u]!=scc[v]){
in[scc[v]]=out[scc[u]]=;
}
}
}
int a=,b=;
for(i=;i<=scc_cnt;++i)a+=in[i],b+=out[i];
cout<<max(a,b)<<endl;
}
return ;
}
Kosaraju:先按原图跑一编按后序顺序编号,然后按照编号从大到小跑反向图,跑一次就是一个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;}; int vis[maxn],scc[maxn],scc_cnt;
int in[maxn],out[maxn];
vector<int>g[maxn],g2[maxn],q;
void dfs1(int u){
if(vis[u])return;vis[u]=;
for(int v:g[u])dfs1(v);q.pb(u);
}
void dfs2(int u){
if(scc[u])return;scc[u]=scc_cnt;
for(int v:g2[u])dfs2(v);
}
int main(){
int t,n,m,i,j,k,u,v;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
q.clear();
for(i=;i<=n;++i)g[i].clear(),g2[i].clear(),vis[i]=scc[i]=;
scc_cnt=;
while(m--){
scanf("%d%d",&u,&v);
g[u].pb(v);
g2[v].pb(u);
}
for(i=;i<=n;++i){
if(!vis[i]){
dfs1(i);
}
}
for(i=n-;i>=;--i){
if(!scc[q[i]]){
++scc_cnt;
dfs2(q[i]);
}
}
// cout<<"FUCK: "<<scc_cnt<<endl;
if(scc_cnt==){
cout<<<<endl;
continue;
}
for(i=;i<=scc_cnt;++i)in[i]=out[i]=;
for(u=;u<=n;++u){
for(int v:g[u]){
if(scc[u]!=scc[v]){
in[scc[v]]=out[scc[u]]=;
}
}
}
int a=,b=;
for(i=;i<=scc_cnt;++i)a+=in[i],b+=out[i];
cout<<max(a,b)<<endl; }
return ;
}
HDU-2767-tarjan/Kosaraju求scc的更多相关文章
- tarjan算法求scc & 缩点
前置知识 图的遍历(dfs) 强连通&强连通分量 对于有向图G中的任意两个顶点u和v存在u->v的一条路径,同时也存在v->u的路径,我们则称这两个顶点强连通.以此类推,强连通分量 ...
- 转载 - Tarjan算法(求SCC)
出处:http://blog.csdn.net/xinghongduo/article/details/6195337 说到以Tarjan命名的算法,我们经常提到的有3个,其中就包括本文所介绍的求强连 ...
- HDU 2874 /// tarjan离线求森林里两点的距离
题目大意: 在一个森林里 询问 u v 两点 若不能到达输出 "Not connected" 否则输出两点距离 https://blog.csdn.net/keyboarderqq ...
- HDU 2586 /// tarjan离线求树上两点的LCA
题目大意: 询问一棵树里 u 到 v 的距离 可由 dis[ u到根 ] + dis[ v到根 ] - 2*dis[ lca(u,v) ] 得到 https://blog.csdn.net/csyzc ...
- 间谍网络——tarjan求SCC
洛谷传送门 看着这道题给人感觉就是tarjan求SCC,然而还得判断是否能控制全部间谍,这就得先从可以贿赂的点dfs一遍. 如果没有全部被标记了,就输出NO,再从没被标记的点里找最小的标号. 如果全被 ...
- tarjan 缩点 求 scc
算法学自 BYVoid https://www.byvoid.com/zhs/blog/scc-tarjan/ 这个写得很清楚了 当然 你可能不这么认为 而且 如果是让我 一开始就从这个博客 学 ta ...
- HDU 1269 -- 迷宫城堡【有向图求SCC的数目 && 模板】
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1269 迷宫城堡 tarjan算法求强连通分量
基础模板题,应用tarjan算法求有向图的强连通分量,tarjan在此处的实现方法为:使用栈储存已经访问过的点,当访问的点离开dfs的时候,判断这个点的low值是否等于它的出生日期dfn值,如果相等, ...
- 有向图 加最少的边 成为强连通分量的证明 poj 1236 hdu 2767
poj 1236: 题目大意:给出一个有向图, 任务一: 求最少的点,使得从这些点出发可以遍历整张图 任务二: 求最少加多少边 使整个图变成一个强连通分量. 首先任务一很好做, 只要缩点 之后 求 ...
随机推荐
- 用Proxy进行预处理
如果你学过我的Vue的课程,一定会知道钩子函数,那如果你刚接触我的博客,并没有学习Vue,那我这里给你简单解释一下什么是钩子函数.当我们在操作一个对象或者方法时会有几种动作,比如:在运行函数前初始化一 ...
- (转载)C#:Enum、Int和String的互相转换,枚举转换
Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举.注意:枚举类型的基类 ...
- js中属性点.和中括号[]的关系。
本来这里说的是 js 执行一个字符串形式函数的方法. 但是呢看到一个 window['test'] ,居然一下子转不过弯来.这就尴尬了. 不是说好了 [] 和 . 其他都是 “什么的什么” 关系吗?如 ...
- Linux 命令之sed
简介 sed 是一种在线编辑器,它一次处理一行内容.在处理的时候,会先把当前处理的行存储在临时缓冲区,这被称之为 "末世空间", 然后再使用 sed 命令处理缓冲区的内容,处理完成 ...
- data:image/png;base64 上传图像将图片转换成base64格式
大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJ ...
- Jquery loading 效果
function showLoad(tipInfo) { var eTip = document.createElement('div'); eTip.setAttribute('id', 'tipD ...
- 使用外网访问阿里云服务器ZooKeeper
参考网址: zookeeper单机/集群安装详解 使用外网访问阿里云服务器ZooKeeper 阿里云服务管理控制台 1. 阿里云ECS安装zookeeper 环境:我安装的是zookeeper3.4. ...
- spring mvc 形参类型
spring mvc 形参类型 1 没有占位符号的,形参的名字为参数的名称 请求路径为:organtrans/t1/t5?a=1(a为形参的的名称必须一致) @RequestMapping(" ...
- C#连接数据库open函数失败
错误信息:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider ...
- leecode第五题(最长回文子串)
class Solution { public: string longestPalindrome(string s) { int len = s.length(); || len == ) retu ...