UVa 12167 & HDU 2767 强连通分量 Proving Equivalences
2015-08-12 11:20
471 查看
题意:给出一个有向图,问最少添加几条有向边使得原图强连通。
解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案。
还有个特殊情况就是本身就是强连通的话,答案就是0.
代码君
解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案。
还有个特殊情况就是本身就是强连通的话,答案就是0.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <stack> using namespace std; const int maxn = 20000 + 10; 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 = 0; 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, 0, sizeof(pre)); memset(sccno, 0, sizeof(sccno)); dfs_clock = scc_cnt = 0; for(int i = 1; i <= n; i++) if(!pre[i]) dfs(i, 0); } int in[maxn], out[maxn]; int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); for(int i = 1; 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 == 1) { puts("0"); continue; } memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); for(int i = 1; i <= n; i++) for(int j = 0; j < G[i].size(); j++) { int u = sccno[i], v = sccno[G[i][j]]; if(u != v) { out[u]++; in[v]++; } } int hehe = 0, haha = 0; for(int i = 1; i <= scc_cnt; i++) { if(!in[i]) hehe++; if(!out[i]) haha++; } printf("%d\n", max(hehe, haha)); } return 0; }
代码君
相关文章推荐
- 使用 spice-guest-tools 让虚拟机支持spicec 双屏显示
- iOS中的NSValue
- KMP算法的next、next value数组代码实现及POJ3461
- iOS UICollectionView实现瀑布流(3)
- LeetCode题解:Implement Stack using Queues
- UE4 Editor View Modes
- 更新sdk后遇到This Android SDK requires Android Developer Toolkit version 20.0.0 or above
- iOS基础-UIKit框架-高级视图-UIDatePicker
- WM_CLOSE WM_QUIT WM_DESTROY 三者的区别
- Bit Manipulation - Repeated DNA Sequences
- ios开发之实现长按UITableViewCell弹出UIMenuController
- LeetCode:Implement Queue using Stacks
- xquery 除法 和节点个数
- 自己封装的Metro风格的后台UI框架,暂命名MUI(持续更新)
- iOS UITextView内容添加后,自动滚动到最后一行
- UICollectionView入门--使用系统UICollectionViewFlowLayout布局类
- LeetCode题解:Implement Queue using Stacks
- STL系列之一 deque双向队列
- 各种设备的CSS3 MediaQuery整理及爽歪歪写法
- HDU 3397 Sequence operation(线段树·成段更新·区间合并·混合操作)