题目链接:http://poj.org/problem?id=1236

题意:

本题为有向图。

需解决两个问题:

1 须要给多少个点,才干传遍全部点。

2 加多少条边,使得整个图变得强连通。

使用Tarjan进行缩点,得到一个SCC图、

这个图有多少个入度为0的,多少个出度为0的。

如果有n个入度为0,m个出度为0

那么第一个答案就是n,第二个答案是max(n,m)

代码:

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <time.h> using namespace std; const int MAXN = 20010;
const int MAXM = 50010; struct Edge
{
int to, next;
}edge[MAXM]; int head[MAXM], tot;
int Low[MAXN], Dfn[MAXN], Stack[MAXN], Belong[MAXN];//Belong的值为 1 ~ scc
int Index, top;
int scc;//强连通个数
bool Instack[MAXN];
int num[MAXN];//各个强连通包括的点的个数 void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} void Tarjan(int u)
{
int v;
Low[u] = Dfn[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for (int i = head[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if (!Dfn[v])
{
Tarjan(v);
if (Low[u] > Low[v])
Low[u] = Low[v];
}
else if (Instack[v] && Low[u] > Dfn[v])
Low[u] = Dfn[v];
}
if (Low[u] == Dfn[u])
{
scc++;
do
{
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
} while (v != u);
}
} int in[MAXN], out[MAXN]; void solve(int N)
{
memset(Dfn,0,sizeof(Dfn));
memset(Instack,false,sizeof(Instack));
memset(num,0,sizeof(num));
Index = scc = top = 0;
for (int i = 1; i <= N; i++)
{
if (!Dfn[i])
Tarjan(i);
}
if (scc == 1)
{
printf("1\n0\n");
return;
}
for (int i = 1; i <= scc; i++)
in[i] = out[i] = 0;
for (int u = 1; u <= N; u++)
{
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (Belong[u] != Belong[v])
{
in[Belong[v]]++;
out[Belong[u]]++;
}
}
}
int ans1 = 0, ans2 = 0;
for (int i = 1; i <= scc; i++)
{
if (in[i] == 0) ans1++;
if (out[i] == 0) ans2++;
}
//printf("%d\n",scc);
printf("%d\n%d\n",ans1,max(ans1,ans2));
} void init()
{
tot = 0;
memset(head,-1,sizeof(head));
} int main()
{
int n;
int u, v;
while (~scanf("%d", &n))
{
init();
for (int i = 1; i <= n; i++)
{
while (~scanf("%d", &u) && u)
{
addedge(i,u);
}
}
solve(n);
}
return 0;
}

poj 1236 Network of Schools 【Tarjan】的更多相关文章

  1. POJ 1236 Network Of Schools 【Targan】+【缩点】

    <题目链接> 题目大意: 有N个学校,每个学校之间单向可以发送软件,现在给你一些学校之间的收发关系.问你下面两个问题:至少要给多少个学校发送软件才能使得最终所有学校都收到软件:至少要多加多 ...

  2. POJ 1236——Network of Schools——————【加边形成强连通图】

    Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

  3. poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13800   Accepted: 55 ...

  4. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  5. poj 1236 Network of Schools(tarjan+缩点)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

  6. POJ 1236 Network of Schools (Tarjan)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22745   Accepted: 89 ...

  7. POJ 1236 Network of Schools(tarjan)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

  8. POJ 1236 Network of Schools(tarjan算法 + LCA)

    这个题目网上有很多答案,代码也很像,不排除我的.大家的思路应该都是taijan求出割边,然后找两个点的LCA(最近公共祖先),这两个点和LCA以及其他点构成了一个环,我们判断这个环上的割边有几条,我们 ...

  9. POJ 1236 Network of Schools(tarjan求强连通分量+思维)

    题目链接:http://poj.org/problem?id=1236 题目大意: 给你一个网络(有向图),有两个任务: ①求出至少同时需要几份副本可以使得整个网络都获得副本 ②至少添加多少信息表(有 ...

随机推荐

  1. EXTJS下拉树ComboBoxTree参数提交及回显方法

    http://blog.csdn.net/wjlht/article/details/6085245 使用extjs可以构造出下拉数,但是不方便向form提交参数,在此,笔者想到一个办法,很方便Com ...

  2. VirtualBox 4.3.18 启动虚拟机时显示不能加载 R3模块并退出故障解决一例

    VirtualBox 升级到 4.3.1x后一直问题不断.搜了些资料,发现这货从最近的某个版本开始,为了安全,要校验进程完整性,那些在运行时要注入Virtualbox进程的模块都要进行校验.于是乎出现 ...

  3. MEF 导入(Import)和导出(Export)

    前言: MEF不同于其他IOC容器(如:Castle)很重要的原因在于它使用了特性化编程模型(涉及到两个概念:“特性”和“编程模型”). 特性(Attribute):举例来说就是我们在开发过程中在类上 ...

  4. glob函数的使用

    glob库函数用于Linux文件系统中路径名称的模式匹配,即查找文件系统中指定模式的路径.注意,这不是正则表达式匹配,虽然有些相似,但还是有点差别. glob函数原型       #include & ...

  5. .net开发Ae释放com对象的问题

    本文转载自: http://www.cnblogs.com/yhlx125/archive/2011/11/22/2258543.html#2269154我的博文 http://www.cnblogs ...

  6. Android之MVC模式

    MVC (Model-View-Controller):M是指逻辑模型,V是指视图模型,C则是控制器.一个逻辑模型可以对于多种视图模型,比如一批统计数据 你可以分别用柱状图.饼图来表示.一种视图模型也 ...

  7. Valgrind 内存泄漏工具

    Valgrind 是一款 Linux下(支持 x86.x86_64和ppc32)程序的内存调试工具,它可以对编译后的二进制程序进行内存使用监测(C语言中的malloc和free,以及C++中的new和 ...

  8. Windows Server 2012 R2搭建IIS服务器

    1-单击宫格菜单的第一个“服务器管理器”: 2 2-在“快速启动(Q)”子菜单下,单击“2 添加角色和功能”: 3 3-点击左边“安装类型”,然后单击“基于角色或基于功能的安装”,再单击“下一步(N) ...

  9. WordPress 如何修改编辑器TinyMCE里的内容

    //获取编辑器对象,wp中的编辑器ID是"content" var editor = tinymce.get('content'); //获取编辑器内容 var content = ...

  10. BlazeMeter+Jmeter 搭建接口测试框架

    转载:http://www.sohu.com/a/133218497_575744 关于接口测试,笔者认为其难点分别在如下几方面:接口参数的获取和输入.测试数据的准备.场景的串联.测试结果的断言. 接 ...