POJ 1236 Network of Schools

题目链接

题意:题意本质上就是,给定一个有向图,问两个问题

1、从哪几个顶点出发,能走全全部点

2、最少连几条边,使得图强连通

思路:

#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
using namespace std; const int N = 105; int n;
vector<int> g[N]; int pre[N], sccno[N], dfn[N], dfs_clock, sccn;
stack<int> S; void dfs_scc(int u) {
pre[u] = dfn[u] = ++dfs_clock;
S.push(u);
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!pre[v]) {
dfs_scc(v);
dfn[u] = min(dfn[u], dfn[v]);
} else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]);
}
if (dfn[u] == pre[u]) {
sccn++;
while (1) {
int x = S.top(); S.pop();
sccno[x] = sccn;
if (x == u) break;
}
}
} void find_scc() {
sccn = dfs_clock = 0;
memset(pre, 0, sizeof(pre));
memset(sccno, 0, sizeof(sccno));
for (int i = 1; i <= n; i++)
if (!pre[i]) dfs_scc(i);
} int in[N], out[N]; int main() {
while (~scanf("%d", &n)) {
int v;
for (int i = 1; i <= n; i++) g[i].clear();
int cnt = n;
for (int u = 1; u <= n; u++) {
while (~scanf("%d", &v) && v)
g[u].push_back(v);
}
find_scc();
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
for (int u = 1; u <= n; u++) {
for (int j = 0; j < g[u].size(); j++) {
int v = g[u][j];
if (sccno[u] != sccno[v]) {
in[sccno[v]]++;
out[sccno[u]]++;
}
}
}
int ins = 0, outs = 0;
for (int i = 1; i <= sccn; i++) {
if (!in[i]) ins++;
if (!out[i]) outs++;
}
int ans1 = ins, ans2 = max(ins, outs);
if (sccn == 1) ans2 = 0;
printf("%d\n%d\n", ans1, ans2);
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

POJ 1236 Network of Schools(强连通分量)的更多相关文章

  1. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

  2. POJ 1236 Network of Schools (强连通分量缩点求度数)

    题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...

  3. poj~1236 Network of Schools 强连通入门题

    一些学校连接到计算机网络.这些学校之间已经达成了协议: 每所学校都有一份分发软件的学校名单("接收学校"). 请注意,如果B在学校A的分发名单中,则A不一定出现在学校B的名单中您需 ...

  4. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  5. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  6. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  7. [tarjan] poj 1236 Network of Schools

    主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K To ...

  8. POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度

    题目链接:http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  9. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

随机推荐

  1. Iconfinder 如何杜绝盗版,哈希算法检测图像重复

    原地址:http://blog.jobbole.com/65914/ 本文由 伯乐在线 - 小鱼 翻译自 Silviu Tantos.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. [伯乐在线导读 ...

  2. Eclipse 未开始 【Ubuntu】

    /usr/lib/eclipse/configuration/1408532831122.log : !SESSION 2014-08-20 19:07:11.055 ---------------- ...

  3. Liftoff Software | Next stop, innovation

    Liftoff Software | Next stop, innovation Previous Next Gate One 1.1 Now Available Submitted by Dan M ...

  4. 设置SVN忽略文件和文件夹(文件夹)

    在多数项目中你总会有文件和文件夹不须要进行版本号控制.这可能包含一些由编译器生成的文件,*.obj,*.lst,也许是一个用于存放可运行程序的输出文件夹.仅仅要你提交改动,TortoiseSVN 就会 ...

  5. ARM体系结构与编程

    ARM处理器的7中执行模式:usr.fiq.irq.svc.abt.und.sys. ARM处理器共37个寄存器:31个通用寄存器(未备份寄存器R0-R7,在全部模式下指的都是同一个物理寄存器:备份寄 ...

  6. hotmail邮箱pop3server设置方法

    hotmail邮箱 的POP3/SMTP功能仅仅向Hotmail Plus的用户开放,普通用户想要使用这一功能的话,得进行一些特别的设置.如今这一功能总算面向全部的用户开放了,虽然微软官方还没宣布这一 ...

  7. ios7开发者必知

    如果你想为iOS 设备开发app,你需要知道如何与软件交互,如何设计,你还要知道苹果独特的开发理念和开发工具.真正的能力还需要成功地从其他行业领域借鉴核心概念.最后把所有这些东西糅合进你的信息库中, ...

  8. C++——STL中三种顺序容器的简要差别

    C++ STL 提供了3个顺序容器 :vector, deque, list Vector动态数组.支持高速訪问:list双向链表,支持高速插入和删除. vector 中的元素是顺序存放的.所以随机訪 ...

  9. java去全半角空格,trim(), replaceAll(" +",""),replaceAll("\\s*", ""), replaceAll(" | ", "")

    JAVA中去掉空格 . String.trim() trim()是去掉首尾空格 .str.replace(" ", ""); 去掉所有空格,包括首尾.中间 St ...

  10. Android Application plugin

          在网易云阅读App上看到了插件管理功能,刚好自己也需要以插件的模式来扩展已有的功能,于是研究了一下,下面是一张网易云阅读App提供的插件模式,只需下载相应的插件就扩展了相应的功能,非常方便 ...