poj 1236 Network of Schools 【Tarjan】
题目链接: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】的更多相关文章
- POJ 1236 Network Of Schools 【Targan】+【缩点】
<题目链接> 题目大意: 有N个学校,每个学校之间单向可以发送软件,现在给你一些学校之间的收发关系.问你下面两个问题:至少要给多少个学校发送软件才能使得最终所有学校都收到软件:至少要多加多 ...
- POJ 1236——Network of Schools——————【加边形成强连通图】
Network of Schools Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u ...
- poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13800 Accepted: 55 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- poj 1236 Network of Schools(tarjan+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- POJ 1236 Network of Schools (Tarjan)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22745 Accepted: 89 ...
- POJ 1236 Network of Schools(tarjan)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- POJ 1236 Network of Schools(tarjan算法 + LCA)
这个题目网上有很多答案,代码也很像,不排除我的.大家的思路应该都是taijan求出割边,然后找两个点的LCA(最近公共祖先),这两个点和LCA以及其他点构成了一个环,我们判断这个环上的割边有几条,我们 ...
- POJ 1236 Network of Schools(tarjan求强连通分量+思维)
题目链接:http://poj.org/problem?id=1236 题目大意: 给你一个网络(有向图),有两个任务: ①求出至少同时需要几份副本可以使得整个网络都获得副本 ②至少添加多少信息表(有 ...
随机推荐
- Java代码优化方案 J2ME内存优化
Java代码优化方案 J2ME内存优化 从几本书上,N个网站上整理的一些JAVA代码优化方案,最近的项目只有1M内存可用,必须很抠门了~J2ME项目更要注意的 避免内存溢出 l 不用的对象释放(置空) ...
- HDU 4699 Editor (2013多校10,1004题)
Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- The New Virtual List Box in Delphi 6 - lbVirtual lbVirtualOwnerDraw
http://users.atw.hu/delphicikk/listaz.php?id=2471&oldal=52 Problem/Question/Abstract: What are t ...
- Virtual Treeview 安装以及入门
Virtual Treeview是一套Delphi下优秀的VCL控件,代码质量高,使用灵活.功能强大.性能非常好,可以用于表达Treeview和表格类数据.它的代码现在托管在google code上. ...
- Process ID, Process handle, Window handle
http://forums.codeguru.com/showthread.php?392273-RESOLVED-How-to-get-window-s-HWND-from-it-s-process ...
- [转].net reactor 学习系列(二)---.net reactor界面各功能说明
安装了.net reactor之后,可以在安装目录下找到帮助文档REACTOR_HELP.chm,目前没有中文版本,里面详细介绍了.net reactor的各功能及使用场景.本系列文章是基于此帮助文档 ...
- UML建模工具Visio 、Rational Rose、PowerDesign的比较
UML建模工具Visio .Rational Rose.PowerDesign的比较 ROSE是直接从UML发展而诞生的设计工具,它的出现就是为了对UML建模的支持,ROSE一开始没有对数据库端建 ...
- 嵌入式mp3播放器
分四部分:按键驱动,声卡驱动,Madplay播放器移植,MP3主播放器处理 按键1:播放,按键2:停止,按键3:上一曲,按键4:下一曲 UA1341内核自带声卡驱动 .解压内核: tar zxvf l ...
- JVM内存参数详解以及配置调优
基本概念:PermGen space:全称是Permanent Generation space.就是说是永久保存的区域,用于存放Class和Meta信息,Class在被Load的时候被放入该区域He ...
- linux系统的磁盘挂载
1.查看数据盘在没有分区和格式化数据盘之前,使用 “df –h”命令,是无法看到数据盘的,可以使用“fdisk -l”命令查看.如下图:2. 对数据盘进行分区执行“fdisk /dev/xvdb”命令 ...