Network of Schools

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24880   Accepted: 9900

题目链接http://poj.org/problem?id=1236

Description:

A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is 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 first line contains an integer N: the number of schools in the network (2 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains 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:

Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.

Sample Input:

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output:

1
2

题意:

给出一个有向图,然后要你输出两个任务的答案:

1.至少需要从多少个点出发,能够到达所有的点;2.最少需要连多少条边,能够使得从任意点出发都能够到达其它所有点。

题解:

这个题我一开始想的就是直接暴力,但很明显第二个问题行不通,所以就要考虑一些性质,或者用一些数学思想。

第一个问题还是比较好想,入度为0的点的个数即位答案,如果不存在入度为0的点,答案就是1。简略证明如下(题目保证图是连通的):

假设入度为0的点为n,那么至少需要n个点才能遍及所有点,然后对于其余入度非0的点来说,必然是由其他点到达的,如果这个点不在环上,那么就必定是从一个入度为0的点来的;如果这个点在环上,这个环中的所有点也会由其余入度为0的点到达;假设这是个单独的环,那么答案为1。

第二个问题要求所有点都互相可以到达。那么我们可以知道的是,图中必然不会存在入度为0以及出度为0的点,假设这两者的个数分别为n,m。

那么最优的连边方法就是入度为0的点与出度为0的点匹配,最后剩下的乱连就行了,所以最后答案就是max(n,m)。证明的话yy一下吧。

因为我们刚才是基于有向无环图来思考的,环的存在应该把它当作一个点,所以考虑Tarjan缩波点就行了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
const int N = ;
int n,tot;
int head[N],in[N],out[N],low[N],dfn[N],vis[N],scc[N];
struct Edge{
int u,v,next;
}e[N*N<<],edge[N*N<<];
void adde(int u,int v){
e[tot].u=u;e[tot].v=v;e[tot].next=head[u];head[u]=tot++;
}
stack <int> s;
int T,num;
void Tarjan(int u){
dfn[u]=low[u]=++T;vis[u]=;
s.push(u);
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(!vis[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}else if(!scc[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
num++;int now;
do{
now = s.top();s.pop();
scc[now]=num;
}while(!s.empty() && now!=u);
}
}
int main(){
scanf("%d",&n);
int m=;
memset(head,-,sizeof(head));
for(int i=;i<=n;i++){
int v;
while(scanf("%d",&v)!=EOF){
if(v==) break ;
edge[++m].u=i;edge[m].v=v;
adde(i,v);
}
}
//cout<<m<<endl;
for(int i=;i<=n;i++){
if(!vis[i]) Tarjan(i);
}
for(int i=;i<=m;i++){
int u=edge[i].u,v=edge[i].v;
if(scc[u]!=scc[v]){
in[scc[v]]++;out[scc[u]]++;
}
}
int cnt1=,cnt2=;
for(int i=;i<=num;i++){
if(!in[i]) cnt1++;
if(!out[i]) cnt2++;
}
//cout<<num<<endl;
if(num==) cout<<<<endl<<;
else cout<<cnt1<<endl<<max(cnt2,cnt1);
return ;
}

POJ1236:Network of Schools (思维+Tarjan缩点)的更多相关文章

  1. POJ1236:Network of Schools(tarjan+缩点)?

    题目: http://poj.org/problem?id=1236 [题意] N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1 ...

  2. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  3. poj 1236 Network of Schools(tarjan+缩点)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

  4. POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度

    题目链接:http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  5. P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools

    P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学 ...

  6. poj1236 Network of Schools【强连通分量(tarjan)缩点】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316263.html  ---by 墨染之樱花 [题目链接]http://poj.org/pr ...

  7. POJ1236 - Network of Schools tarjan

                                                     Network of Schools Time Limit: 1000MS   Memory Limi ...

  8. POJ1236 Network of Schools (强连通)(缩点)

                                                                Network of Schools Time Limit: 1000MS   ...

  9. POJ-1236 Network of Schools,人生第一道Tarjan....

    Network of Schools 题意:若干个学校组成一个计算机网络系统,一个学校作为出发端连接着若干个学校,信息可以传送到这些学校.被链接的学校不需要再次与出发端相连,现在问你:A:最少选几个学 ...

随机推荐

  1. 【xmlHttp_Class 远程访问类】使用说明

    类名:xmlHttp_Class 说明:远程获取外部网站数据信息或执行一个外部网站程序 目录: 类型 名称 参数 返回 说明 属性 [必需] [xmlHttp].url = [urlString] - ...

  2. 原生js实现轮播图原理

    轮播图的原理1.图片移动实现原理:利用浮动将所有所有照片依次排成一行,给这一长串图片添加一个父级的遮罩,每次只显示一张图,其余的都隐藏起来.对图片添加绝对定位,通过控制left属性,实现照片的移动. ...

  3. java学习笔记-9.违例差错控制

      1.违例规范是告诉程序员这个方法可能抛出哪些类型的异常.他的格式在方法声明中,位于自变量(参数)列表的后面,如void f() throws tooBig, tooSmall, divZero { ...

  4. jetbrains系列激活

    没钱,只能DB了. 为了避免某些个人私自搭建服务器,以及自己搭建激活服务器,因此,决定使用破解包~~~. 注意:只要破解,就要屏蔽官方激活服务器:0.0.0.0 account.jetbrains.c ...

  5. Deep Residual Learning for Image Recognition论文笔记

    Abstract We present a residual learning framework to ease the training of networks that are substant ...

  6. codeforces 359E Neatness(DFS+构造)

    Simon loves neatness. So before he goes to bed, Simon wants to complete all chores in the house. Sim ...

  7. POSIX线程学习

    一.什么是线程 在一个程序中的多个执行路线就叫做线程.更准确的定义是:线程是一个进程内部的一个控制序列.所有的进程都至少有一个线程.当进程执行fork调用时,将创建出该进程的一份新副本,这个新进程拥有 ...

  8. 算法与数据结构实验题 6.3 search

    ★实验任务 可怜的 Bibi 刚刚回到家,就发现自己的手机丢了,现在他决定回头去搜索 自己的手机. 现在我们假设 Bibi 的家位于一棵二叉树的根部.在 Bibi 的心中,每个节点 都有一个权值 x, ...

  9. Java中的线程的优先级

    Java 中线程优先级简介: 1. Java 提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程. 按照线程的优先级决定应该调度哪个线程来执行. 2. 线程的优先级用数字表示, 范围从 1 到 ...

  10. 《C陷阱与缺陷》之1词法"陷阱"

    编译器中负责将程序分解为一个一个符号的部分,一般称为"词法分析器".在C语言中,符号之间的空白(包括空格符.制表符或换行符)将被忽略. 1.=不同于== C语言使用符号" ...