POJ1236 Tarjan求强连通分量
来源:互联网 发布:网络文明志愿宣言 编辑:程序博客网 时间:2024/06/04 20:08
题目链接:http://poj.org/problem?id=1236
题目大意:
有一个有向图,图中两点之间若有一条有向边代表一份软件可以从弧尾发送到弧头。
题目分两个小问,第一个问题事发送软件到图中的某些点,问最少发送多少个点可以使这份软件可以到达图中的任何一个点
第二小问问,最少向图中添加几条边可以使发送软件到图中任意一个点就可以使该软件到达图中的所有点。
思路:
求强联通分量,一个强联通分量可以当做一个点来看。第一问是求入度为0的点的数量,第二问求入度为0点的数量和出度为0的点的数量中的最大值。
#include<iostream>#include<cstring>#include<stack>using namespace std;const int N = 105;struct Edge{ int to, nx;}e[N*N];stack<int> stk;int dfn[N], low[N], sccn[N], sccCnt, step;int tot, head[N];bool instack[N];int in[N], out[N];void initMap(){ memset(head, -1, sizeof(head)); memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); tot = 0;}void initScc(){ memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); memset(sccn, 0, sizeof(sccn)); memset(instack, false, sizeof(instack)); step = 0;}void addEdge(int from, int to){ e[++tot] = Edge{to, -1}; e[tot].nx = head[from]; head[from] = tot;}void tarjan(int u){ stk.push(u); dfn[u] = low[u] = ++step; instack[u] = true; for (int k = head[u]; k != -1; k = e[k].nx){ int to = e[k].to; if (!dfn[to]){ tarjan(to); low[u] = min(low[u], low[to]); } else if (instack[to]){ low[u] = min(low[u], dfn[to]); } } if (dfn[u] == low[u]){ int v; sccCnt++; while(true){ v = stk.top(); stk.pop(); instack[v] = false; sccn[v] = sccCnt; if (v == u) break; } }}void getScc(int n){ initScc(); for (int i = 1; i <= n; i++) if (!dfn[i]){ tarjan(i); }}int main(){ std::ios::sync_with_stdio(false); int n; while(cin >> n){ initMap(); for (int i = 1; i <= n; i++){ int from, to; from = i; while(cin >> to && to){ addEdge(from, to); } } initScc(); getScc(n); for (int i = 1; i <= n; i++){ for (int k = head[i]; k != -1; k = e[k].nx){ int to = e[k].to; if (sccn[to] != sccn[i]){ in[sccn[to]]++; out[sccn[i]]++; } } } if (sccCnt == 1){ cout << "1" << endl; cout << "0" << endl; continue; } int ans1 = 0, ans2 = 0; for (int i = 1; i <= sccCnt; i++){ if (!in[i]) ans1++; if (!out[i]) ans2++; } cout << ans1 << endl; cout << (ans1>ans2 ? ans1 : ans2) << endl; } return 0;}
阅读全文
0 0
- POJ1236 Tarjan求强连通分量
- POJ1236强连通分量tarjan
- poj1236强连通分量(Tarjan)
- tarjan求强连通分量
- tarjan 求强连通分量
- tarjan求强连通分量
- Tarjan求强连通分量
- tarjan求强连通分量
- Tarjan求强连通分量
- poj1236(强连通分量)
- POJ1236 强连通分量
- POJ1236 Network of Schools【Tarjan】【强连通分量】
- POJ1236 Network of Schools(强连通分量:Tarjan算法)
- poj1236 有线图的强连通分量 tarjan算法判断
- poj1236 Network of Schools 【连通图-强联通分量-tarjan】
- poj1236 Network of Schools(强连通分量,tarjan)
- 上白泽慧音 题解 ---- tarjan求强连通分量
- Tarjan算法求强连通分量
- HBase架构解析一
- web 框架 Django 学习记录 (二)把数据库中表内容展示在页面
- Linux下Redis安装和自启动配置
- 我的第一个hbulider项目--五子棋
- 3中web会话管理机制(转载)
- POJ1236 Tarjan求强连通分量
- vue.js组件传值
- 控制卡初始问题
- convert-sorted-list-to-binary-search-tree
- js中的callee和caller
- H5第一次实训--五子棋
- Android NDK网络通信篇(五)之UDP通信篇
- 表连接的一些笔记
- hdu-1166 敌兵布阵(线段树的增删查改)