题目链接: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. Visual Studio 2010 使用 Git Extensions 连接 google code

    下载最新版本 Git Extensions http://code.google.com/p/gitextensions/downloads/list Git Extensions 2.46 Wind ...

  2. Hbulider 支持less保存自动编译

    设置less配置:文件写 .less,路径找lessc.cmd,找不到就下载,命令参数写:%FileName% %FileBaseName%.css这样以后就可以less文件自动生成css了!

  3. Unity3d面试6

    1,如何避免点击UI按钮时穿透,同时触发了相同位置场景模型的点击事件的情况?(NGUI)1,如何避免点击UI按钮时穿透,同时触发了相同位置场景模型的点击事件的情况?(NGUI 判断 是否点击到UI) ...

  4. AC-PC线(前联合-后联合线)

    下面利用一张大脑矢状面(侧视图)来描述ac-pc的空间位置关系.前联合用红色点表示,后联合用黄色表示. 在Talairach 模板的官方文档中,AC-PC线从前联合AC的表面出发,延伸到后联合PC的中 ...

  5. 尝试PWA

    1.一个 PWA 应用首先是一个网页, 可以通过 Web 技术编写出一个网页应用. 随后添加上 App Manifest 和 Service Worker 来实现 PWA 的安装和离线等功能. 2.创 ...

  6. [Android Studio] Android Studio底边栏选项不见了,如何调出来

    转载:http://blog.csdn.net/hyr83960944/article/details/38067785 Android Studio底边有一个选项栏,包含了Run,Android等等 ...

  7. Android从无知到有知——NO.2

    这几天虽说偶遇瓶颈.但也渐入佳境.因为之前没有接触过android,所以作为一个新手不会给自己过高的要求.相比于去年做的分布式资源检索,今年的移动开发着实要简单一些.尽管其本质没有太大的差别,但从用户 ...

  8. Open DJ备份与恢复方案

    最近接手了一个Cognos项目,第三方用户认证采用的是和Open DJ集成.本人之前很多采用的是cjap ,当然这和cjap相比起来简单的多了,最起码你不必具有Java的基础知识就可以完全驾驭了! 一 ...

  9. [Functional Programming] Use Task/Async for Asynchronous Actions

    We refactor a standard node callback style workflow into a composed task-based workflow. Original Co ...

  10. UI设计经常使用站点

    前期:http://www.zcool.com.cn/ 中期:http://www.ui.cn/ 后期:https://dribbble.com/