[POJ 1236][IOI 1996]Network of Schools
Description
in the distribution list of school A, then A does not necessarily appear in the list of school B
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
the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.
Output
Sample Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2
Source
题目大意:给定一个有向图,求至少要有多少个点, 才干从这些点出发到达全部点;至少要加入多少条边,才干从随意一点出发到达全部点
首先要推出一个定理:在DAG中,对于全部入度不为0的点,一定有入度为0的点可达(由于从入度为0的点倒着走,一定能走到入度不为0的点)
于是此题可用tarjan缩点,求有多少个入度为0的点,这就是第一个问题的答案。
第二个问题的答案为入度为0的点和出度为0的点的最小值。证明比較难。略。
对于这道题,由于仅仅要求入度和出度为0的点,故仅仅需在tarjan过程中记录每一个点归属哪个强连通分量。然后统计输出就可以
#include <iostream>
#include <stdio.h>
#include <string.h> #define MAXE 500
#define MAXV 3000 using namespace std; int N; struct edge
{
int u,v,next;
}edges[MAXV]; int head[MAXE],nCount=0;
int dfn[MAXE],low[MAXE],index=0;
int belong[MAXE],tot=0; //belong[i]=i点所属的强连通分量,tot=强连通分量总数
bool inStack[MAXE];
int stack[MAXE*4],top=0;
bool map[MAXE][MAXE];
int inDegree[MAXE],outDegree[MAXE],inZero=0,outZero=0; //入度。出度 int max(int a,int b)
{
if(a>b) return a;
return b;
} int min(int a,int b)
{
if(a<b) return a;
return b;
} void AddEdge(int U,int V)
{
edges[++nCount].u=U;
edges[nCount].v=V;
edges[nCount].next=head[U];
head[U]=nCount;
} void tarjan(int u)
{
dfn[u]=low[u]=++index;
stack[++top]=u; //该点入栈
inStack[u]=true;
for(int p=head[u];p!=-1;p=edges[p].next)
{
int v=edges[p].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(inStack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
int v;
if(dfn[u]==low[u])
{
tot++;
do
{
v=stack[top--];
belong[v]=tot;
inStack[v]=false;
}
while(u!=v);
}
} int main()
{
int to;
cin>>N;
memset(head,-1,sizeof(head));
for(int i=1;i<=N;i++)
{
while(1)
{
cin>>to;
if(to==0) break;
AddEdge(i,to);
map[i][to]=true;
}
}
for(int i=1;i<=N;i++)
if(!dfn[i]) tarjan(i);
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
{
if(map[i][j]&&belong[i]!=belong[j])
{
inDegree[belong[j]]++;
outDegree[belong[i]]++;
}
}
for(int i=1;i<=tot;i++)
{
if(!inDegree[i]) inZero++;
if(!outDegree[i]) outZero++;
}
if(tot==1) cout<<1<<endl<<0<<endl;
else cout<<inZero<<endl<<max(inZero,outZero)<<endl;
return 0;
}
[POJ 1236][IOI 1996]Network of Schools的更多相关文章
- POJ 1236——Network of Schools——————【加边形成强连通图】
Network of Schools Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u ...
- 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(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- POJ 1236 Network of Schools (有向图的强连通分量)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9073 Accepted: 359 ...
- 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+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- 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 ...
随机推荐
- SqlServer 删除重复记录
在给一个客户上线的系统里发现有一张表里出现了重复的数据,结果通过排查代码发现确实业务逻辑有问题,在修改了代码后需要将为数据库里的重复数据删除 在CSDN上找到解决方案,对线上的数据库尽量不要执行删除操 ...
- 用CSS下划线距离
但在我在CSS中新加了TEXT-DECORATION: underline; 后发现下划线离文本太近了,很难看. 代码一: a { text-decoration: none; background: ...
- Linux统计/监控工具SAR详细介绍
转载:http://www.ctohome.com/FuWuQi/1b/688.html sysstat 工具简介 sysstat 是 Linux 系统中的常用工具包.它的主要用途是观察服务负载,比如 ...
- 在第一段ionic示例的基础上增加底部导航
demo2.html <!DOCTYPE html> <html ng-app="app"> <head> <meta charset=& ...
- 让Android App启动更协调
不知道大伙有没有发现,应用第一次启动的时候一般比较慢(低配置手机尤其如此),黑屏好一段时间,下面是我在模拟器中启动QQ的截图,黑屏差不多有5秒左右,如下图所示~ 显然这种结果很糟 ...
- iOS 7 导航栏颜色设定与适配
iOS7 设置navigationBar的颜色,新增了一个属性 barTintColor CGFloat osVersion = [[UIDevice currentDevice].systemVer ...
- DAS存储未死,再次欲获重生
当我们回想存储发展形态时.我们会想到DAS是最原始最主要的存储方式,在个人电脑.server等低端市场和场景上随处可见. 存储的原始形态也来自于DAS,常见的用于连接DAS和主机系统的协议/标准主要 ...
- Unity Editor类常用方法
http://www.cnblogs.com/zhaoqingqing/p/3944718.html 一些比较常用的Editor功能我在之前的博客中也有提到过所以就不详细写啦,今天参考了一下 麒麟子( ...
- Creating a Unity Game for Windows 8
原地址:http://www.davebost.com/2013/08/30/creating-a-unity-game-for-windows-8 The recent release of Uni ...
- 【Linux】cat命令
用途 cat用于将一个档案的内容连续的打印在屏幕上 全称 cat的全称是Conctaenate 参数 -A :相当于-vTE的整合选项,可列出一些特殊字符而不是空白而已 -b :列出行号,仅针对非空白 ...