POJ 1236Network of Schools 强连通分量分解
来源:互联网 发布:雷迪尊者 知乎 编辑:程序博客网 时间:2024/05/16 01:03
题目:http://poj.org/problem?id=1236
题意:1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。
2:至少需要添加几条边,使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
思路:第一问相当好做啊,求解强连通分量缩点,缩点后入度为0的点的个数就是解。但是第二问我是不会的。。。百度了一下,答案是入度为0的点的个数和出度为0的点的个数中的较大值,简单思路是可以从出度为0的点向入度为0的点连边,把所有的出度或入度为0的点消掉,那么连边个数自然是二者中较大值,没有严格证明。另外特判一下只有一个强连通分量的情况
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;const int N = 1010;struct edge{ int to, next;}G[N*20];int dfn[N], low[N], scc[N], st[N];int head[N];bool vis[N];int n, index, cnt, top, num;void init(){ memset(dfn, -1, sizeof dfn); memset(head, -1, sizeof head); memset(vis, 0, sizeof vis); index = cnt = top = num = 0;}void add_edge(int v, int u){ G[cnt].to = u; G[cnt].next = head[v]; head[v] = cnt++;}void tarjan(int v){ dfn[v] = low[v] = index++; st[top++] = v; vis[v] = true; int u; for(int i = head[v]; i != -1; i = G[i].next) { u = G[i].to; if(dfn[u] == -1) { tarjan(u); low[v] = min(low[v], low[u]); } else if(vis[u]) low[v] = min(low[v], dfn[u]); } if(dfn[v] == low[v]) { num++; do { u = st[--top]; vis[u] = false; scc[u] = num; }while(u != v); }}void slove(){ for(int i = 1; i <= n; i++) if(dfn[i] == -1) tarjan(i); if(num == 1) { printf("%d\n%d\n", 1, 0); return; } int indeg[N], outdeg[N]; memset(indeg, 0, sizeof indeg); memset(outdeg, 0, sizeof outdeg); for(int i = 1; i <= n; i++) for(int j = head[i]; j != -1; j = G[j].next) if(scc[i] != scc[G[j].to]) outdeg[scc[i]]++, indeg[scc[G[j].to]]++; int in0 = 0, out0 = 0; for(int i = 1; i <= num; i++) { if(indeg[i] == 0) in0++; if(outdeg[i] == 0) out0++; } printf("%d\n%d\n", in0, max(in0, out0));}int main(){ int a; while(~ scanf("%d", &n)) { init(); for(int i = 1; i <= n; i++) while(scanf("%d", &a), a) add_edge(i, a); slove(); } return 0;}
0 0
- POJ 1236Network of Schools 强连通分量分解
- POJ 1236 Network of Schools【强连通分量分解&&缩点||tarjan&&缩点】
- POJ 1236 Network of Schools强连通分量缩点
- POJ 1236 Network of Schools (强连通分量+缩点)
- POJ 1236 Network of Schools 强连通分量+缩点
- poj 1236 Network of Schools (强连通分量+缩点)
- poj 1236 Network of Schools 强连通分量
- poj 1236 Network of Schools(强连通分量+缩点)
- POJ 1236 Network of Schools 强连通分量
- poj 1236--Network of Schools [强连通分量]
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- poj 1236 Network of Schools(强连通分量 Tarjan)
- POJ 1236 - Network of Schools(强连通分量)
- poj 1236 Network of Schools(强连通分量缩点)
- Eclipse 常用设置
- Android 6.0 运行时权限处理完全解析
- n组字母和最大
- hdu 3966 Aragorn's Story (树链剖分)
- C#类中索引器的使用
- POJ 1236Network of Schools 强连通分量分解
- 【C语言】指针,数组,数组指针,指针数组
- 概念澄清:Caffe训练过程中的test_iter test_interval等概念
- hibernate使用Query进行查询
- 习题三1011
- leetcode-easy OJ感想
- Bootstrap Multiselect中文api
- 小细节:margin:0 auto 与 text-align:center 的区别
- Ubuntu安装ss-qt5