题意:给出一个有向图,问最少添加几条有向边使得原图强连通。

解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案。

还有个特殊情况就是本身就是强连通的话,答案就是0.

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std; const int maxn = + ; int n, m; vector<int> G[maxn]; stack<int> S;
int pre[maxn], low[maxn], sccno[maxn];
int dfs_clock, scc_cnt; void dfs(int u, int fa)
{
low[u] = pre[u] = ++dfs_clock;
S.push(u); for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if(!pre[v])
{
dfs(v, u);
low[u] = min(low[u], low[v]);
}
else if(!sccno[v]) low[u] = min(low[u], pre[v]);
} if(pre[u] == low[u])
{
scc_cnt++;
for(;;)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} void find_scc()
{
memset(pre, , sizeof(pre));
memset(sccno, , sizeof(sccno));
dfs_clock = scc_cnt = ;
for(int i = ; i <= n; i++) if(!pre[i]) dfs(i, );
} int in[maxn], out[maxn]; int main()
{
int T; scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) G[i].clear();
while(m--)
{
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
} find_scc(); if(scc_cnt == ) { puts(""); continue; } memset(in, , sizeof(in));
memset(out, , sizeof(out));
for(int i = ; i <= n; i++)
for(int j = ; j < G[i].size(); j++)
{
int u = sccno[i], v = sccno[G[i][j]];
if(u != v) { out[u]++; in[v]++; }
} int hehe = , haha = ;
for(int i = ; i <= scc_cnt; i++)
{
if(!in[i]) hehe++;
if(!out[i]) haha++;
}
printf("%d\n", max(hehe, haha));
} return ;
}

代码君

UVa 12167 & HDU 2767 强连通分量 Proving Equivalences的更多相关文章

  1. HDU 3072 (强连通分量)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3072 题目大意:为一个有向连通图加边.使得整个图全连通,有重边出现. 解题思路: 先用Tarjan把 ...

  2. hdu 4685(强连通分量+二分图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4685 题意:n个王子和m个公主,王子只能和他喜欢的公主结婚,公主可以和所有的王子结婚,输出所有王子可能 ...

  3. hdu 4685(强连通分量+二分图的完美匹配)

    传送门:Problem 4685 https://www.cnblogs.com/violet-acmer/p/9739990.html 参考资料: [1]:二分图的最大匹配.完美匹配和匈牙利算法 [ ...

  4. hdu 2767 强连通缩点处理加边问题

    #include <cstring> #include <cstdlib> #include <cstdio> 缩点的好处就是可以将乱七八糟的有向图 转化为无环的有 ...

  5. HDU 2767:Proving Equivalences(强连通)

    http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:给出n个点m条边,问在m条边的基础上,最小再添加多少条边可以让图变成强连通.思路:强连通分量缩点后找 ...

  6. hdu 2767 Proving Equivalences

    Proving Equivalences 题意:输入一个有向图(强连通图就是定义在有向图上的),有n(1 ≤ n ≤ 20000)个节点和m(0 ≤ m ≤ 50000)条有向边:问添加几条边可使图变 ...

  7. HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. HDU 2767 Proving Equivalences (Tarjan)

    Proving Equivalences Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other ...

  9. 有向图 加最少的边 成为强连通分量的证明 poj 1236 hdu 2767

    poj 1236: 题目大意:给出一个有向图, 任务一: 求最少的点,使得从这些点出发可以遍历整张图  任务二: 求最少加多少边 使整个图变成一个强连通分量. 首先任务一很好做, 只要缩点 之后 求 ...

随机推荐

  1. Error occurred while loading plugins. CLI functionality may be limited.

    npm install --save-dev --save-exact @ionic/cli-plugin-ionic-angular@latest @ionic/cli-plugin-cordova ...

  2. C#基础文件file的各种套路

    File的各种套路 //创建一个文件 //File.Create(@"C:\Users\SpringRain\Desktop\new.txt"); //Console.WriteL ...

  3. 【持续更新】Spring相关

    什么是IoC 什么是AoP Bean的实例化方法--3种 Bean的作用域--常用2种 Bean的生命周期 Bean的装配方式 基于xml的2种装配方式 基于Annotaton的装配方式

  4. 集合、迭代器、增强for循环、泛型

    1集合 集合是java中提供的一种容器,可以用来存储多个数据. 数组的长度是固定的.集合的长度是可变的.集合中存储的元素必须是引用类型数据. 1.1ArrayList集合存储元素(复习) 例: pub ...

  5. zuul忽略表达式

    如果有error过滤器,会进入error

  6. HTML5标签选择指引

  7. (2017.10.16) javascript 数据类型转换与操作

    javascript 有 5 种基本数据类型:undefined.null.Boolean.String.Number,还有1 种较复杂的数据类型 Object:各种类型之间可以相互转换,其中有些有趣 ...

  8. Round #322 (Div. 2) 581D Three Logos (模拟)

    先枚举两个矩形,每个矩形横着放或竖着放,把一边拼起来, 如果不是拼起来有缺口就尝试用第三个矩形去补. 如果没有缺口就横着竖着枚举一下第三个矩形和合并的矩形x或y拼接. #include<bits ...

  9. Android(java)学习笔记133:Eclipse中的控制台不停报错Can't bind to local 8700 for debugger

    [DDMS] Can't bind to local 8600 for debugger 改成 Under Window -> Preferences -> Android -> D ...

  10. Android(java)学习笔记131:关于构造代码块,构造函数的一道面试题(华为面试题)

    1. 代码实例: package text; public class TestStaticCon { public static int a = 0; static { a = 10; System ...