POJ 1236 Network of Schools (有向图的强连通分量)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9073 | Accepted: 3594 |
Description
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.
Input
Output
Sample Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2
Source
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = ;
const int MAXM = *; struct Edge
{
int to,next;
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];
int Index,top;
int scc;
bool Instack[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 != -;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];
Belong[v] = scc;
Instack[v] = false;
}
while( v!= u);
}
}
int in[MAXN],out[MAXN];
void solve(int N)
{
memset(DFN,,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
Index = scc = top = ;
for(int i = ;i <= N;i++)
if(!DFN[i])
Tarjan(i);
if(scc == )
{
printf("1\n0\n");
return;
}
for(int i = ;i <= scc;i++)
in[i] = out[i] = ;
for(int u = ;u <= N;u++)
{
for(int i = head[u];i != -;i = edge[i].next)
{
int v = edge[i].to;
if(Belong[u] != Belong[v])
{
in[Belong[v]]++;
out[Belong[u]]++;
}
}
}
int ans1=,ans2=;
for(int i = ;i <= scc;i++)
{
if(in[i]==)ans1++;
if(out[i]==)ans2++;
}
printf("%d\n%d\n",ans1,max(ans1,ans2)); }
void init()
{
tot = ;
memset(head,-,sizeof(head));
}
int main()
{
int n;
int v;
while(scanf("%d",&n) == )
{
init();
for(int i = ;i <= n;i++)
{
while(scanf("%d",&v)== && v)
{
addedge(i,v);
}
}
solve(n);
}
return ;
}
POJ 1236 Network of Schools (有向图的强连通分量)的更多相关文章
- POJ 1236 Network of Schools 有向图强连通分量
参考这篇博客: http://blog.csdn.net/ascii991/article/details/7466278 #include <stdio.h> #include < ...
- 【POJ 1236 Network of Schools】强联通分量问题 Tarjan算法,缩点
题目链接:http://poj.org/problem?id=1236 题意:给定一个表示n所学校网络连通关系的有向图.现要通过网络分发软件,规则是:若顶点u,v存在通路,发给u,则v可以通过网络从u ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [tarjan] poj 1236 Network of Schools
主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K To ...
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- POJ 1236 Network of Schools(tarjan算法 + LCA)
这个题目网上有很多答案,代码也很像,不排除我的.大家的思路应该都是taijan求出割边,然后找两个点的LCA(最近公共祖先),这两个点和LCA以及其他点构成了一个环,我们判断这个环上的割边有几条,我们 ...
随机推荐
- shell/bash 交集、并集、差集
方法一(直接用文件名):取两个文本文件的并集.交集.差集并:sort -m <(sort file1 | uniq) <(sort file2 | uniq) | uniq交:sort - ...
- oh my zsh设置
安装oh my zsh sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/inst ...
- Jqgrid入门-使用模态对话框编辑表格数据(三)
Jqgrid是一个强大的表格插件,它提供了多种方式来编辑数据.这三种方式分别是: Cell Editing——只允许修改某一个单元格内容 Inline Editing——允许在jqGr ...
- Mysql 临时变量的 定义 和 赋值 Set 和 Into 赋值; Swith Mysql版本 Case When的用法
一:临时变量的定义和赋值 DECLARE spot SMALLINT; -- 分隔符的位置 DECLARE tempId VARCHAR(64); -- 循环 需要用到的临时的Cid DECLARE ...
- HTMLayout界面CSSS样式解析笔记
HTMLayout学习笔记 by BBDXF 一.界面篇 学习界面需要有一定的HTML.CSS认知,如果你问为什么,那就当我白说. 由于界面库官方没有给一个完善的User guide,所有的学习都靠自 ...
- AngularJS 拦截器和应用例子(转)
$httpAngularJS 的 $http 服务允许我们通过发送 HTTP 请求方式与后台进行通信.在某些情况下,我们希望可以俘获所有的请求,并且在将其发送到服务端之前进行操作.还有一些情况是,我们 ...
- apache开源项目 -- tomee
Apache TomEE,发音是“Tommy”,是一个经Apache.JavaEE6.Web框架认证的适配器,其在Tomcat服务器中是最强大的.Apache TomEE是由香草项目(简化常见编程任务 ...
- Java [Leetcode 278]First Bad Version
题目描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- 动态创建WebService
WebService应用主要是为远程提供接口服务,远程通过代理方式获取WebService资源:但是在现实应用过程中,在Web或者应用程序中如果想用生成远程代理,一般是借助vs里提供的 添加-添加we ...
- Free Pascal的IDE界面乱码解决方法
右击[Free Pascal]快捷图标,选[属性] [属性]窗口中,选择[选项]页,下拉[当前代码页(C)],选择[437 (美国)] 按[确定] 注意:[开始]菜单和桌面中的快捷图标都得设置.