题目链接: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. 学习笔记:Tab Bar 控件使用详解

    注意这里是:Tab Bar 不是Tab Bar Controller. Tab bar是继承UIView,所以可以添加到ViewController里.是View就可以add到另一个View上去.Ta ...

  2. iOS Sqlite加密(FMDB/SQLCipher)

    /** * 对数据库加密 * * @param path path description * * @return return value description */ + (BOOL)encryp ...

  3. Hibernate:不容易理解的 lock 和 merge

    背景 lock 和 merge 在字面上很容易理解它们的语义,不过它们的实际行为所代表的语义范围要大一点,本文就简单的记录下来,还请朋友们多批评和指正. Lock 官方的注释 /** * Obtain ...

  4. 初步理解socket

    近期研究下socket,发现自己还是有非常多不明确的地方,索性沉下心来,从最基础開始学习,開始看起,如今对自己的学习做下小小总结,以便和大家分享,如有谬误,敬请指正. 原创文章,转载请注明出处:htt ...

  5. Webharvest网络爬虫应用总结,web-harvest 编写脚本 读取 百度 博客 实例

      Webharvest网络爬虫应用总结 Web-Harvest是一个Java开源Web数据抽取工具.它能够收集指定的Web页面并从这些页面中提取有用的数据.其实现原理是,根据预先定义的配置文件用ht ...

  6. 更为详细的介绍Hadoop combiners-More about Hadoop combiners

    Hadoop combiners are a very powerful tool to speed up our computations. We already saw what a combin ...

  7. ICLR 2016 - Workshop Track International Conference on Learning Representations 论文papers

    ICLR 2016 - Workshop Track International Conference on Learning Representations May 2 - 4, 2016, Car ...

  8. Linux下解决 id_rsa 权限不够

    问题 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @         WARNING: UNPROTECTED PRIVAT ...

  9. TensorFlow进阶(四)---名称域和共享变量

    变量作用域 tensorflow提供了变量作用域和共享变量这样的概念,有几个重要的作用. 让模型代码更加清晰,作用分明 变量作用域域 通过tf.variable_scope(<scope_nam ...

  10. #pragma详细解释(一)

    #pragma详细解释 #pragma详细解释(一) 2010-04-18 14:21:00|  分类: 默认分类 |  标签: |字号大中小订阅     在#Pragma是预处理指令它的作用是设定编 ...