Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22729   Accepted: 8926

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
哦豁,这道水题。我wa了一天,从11点WA到晚上九点。。最后发现少写一句话orz...
忘记给u标记orzorzorzorz然后发现自己上午敲的板子就是错的 wori....
以后我只用bin神的模板!!!
这个题很简单,还是tarjan缩点,分别找一下入度和出度为0的点。
最少通知几个,易得,考虑入度为0的强连通分量~
最少加几条边,sum1和sum2的最大值?为什么呢,因为要让任意选一个点都满足条件,画一下图,得让每个强连通分量有入有出。所以要取最大值。
贴代码
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define inf 1000000000
#define maxn 105
#define maxm 100005
struct node
{
int to,next;
} edge[maxm];
int n,m,head[maxn],vis[maxn],dfn[maxn],low[maxn],cdu[maxn],num[maxn],cnt,timi,stack1[maxn],top,cut,rdu[maxn];
stack <int> s;
void init()
{
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
memset(cdu,,sizeof(cdu));
memset(rdu,,sizeof(rdu));
while(!s.empty()) s.pop();
cnt=;
timi=;
top=;
cut=;
}
void addedge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt;
cnt++;
}
void tarjan(int u)
{
dfn[u]=timi;
low[u]=timi;
timi++;
s.push(u);
vis[u]=;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
{
if(vis[v])
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
cut++;
int x=s.top();
while(x!=u)
{
vis[x]=;
num[x]=cut;
s.pop();
x=s.top();
}
num[x]=cut;
vis[x]=;
s.pop();
}
}
int mp[maxn][maxn];
int main()
{
int a;
while(cin>>n)
{
init();
for(int i=; i<=n; ++i)
{
while(~scanf("%d",&a)&&a) {
addedge(i,a);
mp[i][a]=;
}
}
for(int i=; i<=n; ++i)
{
if(!dfn[i]) tarjan(i);
}
if(cut==)
{
printf("1\n0\n");
continue;
}
for(int i=; i<=n; ++i)
for(int j=; j<=n; ++j)
{
if(mp[i][j]==&&num[i]!=num[j])
{cdu[num[i]]++;rdu[num[j]]++;}
}
int sum1=,sum2=;
for(int i=; i<=cut; ++i)
{
if(!cdu[i]) sum1++;
if(!rdu[i]) sum2++;
}
//cout<<sum1<<" "<<sum2<<endl;
printf("%d\n%d\n",sum2,max(sum1,sum2));
}
return ;
}
												

POJ 1236 Network of Schools Tarjan缩点的更多相关文章

  1. POJ 1236 Network of Schools (Tarjan + 缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12240   Accepted: 48 ...

  2. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  3. POJ 1236 Network of Schools 连通图缩点

    题目大意:有向图连通图,第一问求至少需要多少个软件才能传输到所有学校,第二问求至少需要增加多少条路使其成为强连通图 题目思路:利用Tarjan算法经行缩点,第一问就是求缩点后入度为0的点的个数(特殊情 ...

  4. POJ 1236 Network of Schools —— (缩点的应用)

    题目大意:有N个学校和一些有向边将它们连结,求: 1.最少需要向几个学校发放软件,使得他们中的每一个学校最终都能够获得软件. 2.最少需要增加几条有向边使得可以从任意一个学校发放软件,使得每一个学校最 ...

  5. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  6. POJ 1236 Network of Schools(强连通分量)

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

  7. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  8. [tarjan] poj 1236 Network of Schools

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

  9. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

随机推荐

  1. Spring @Transactional 浅谈

    一般当我们在一个方法里面操作多个数据对象的可持久化操作时,我们通常这些操作能够成功一起事务提交成功.默认情况下,数据库处于自动提交模式.每一条语句处于一个单独的事务中,在这条语句执行完毕时,如果执行成 ...

  2. 【Django】URL中传递中文的问题

     开发环境:Ubuntu16.04+Django 1.11.9+Python2.7 在开发中,在做查找某些信息这个功能的时候,遇到的一个问题.需要在URL中传递查找的关键字,当关键字为中文的时候,并不 ...

  3. composer 自动加载源码解析

    一直在用 composer,最近想看一下具体的原理是什么,就仔细阅读了一下源码,一下是个人理解.在看该文章前最好了解一下 PSR-4 自动加载规范 引入类自动加载文件 # 加载类自动加载文件 requ ...

  4. 企业shell面试题及解答

    1.面试题:使用for循环在/tmp目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串template,示例如下 aaesdffbnv_template.html 方 ...

  5. JZOJ 1265. Round Numbers

    1265. Round Numbers(rndnum.pas/c/cpp) (File IO): input:rndnum.in output:rndnum.out Time Limits: 1000 ...

  6. HDU1301 Jungle Roads

    Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...

  7. TCP/IP网络编程之多线程服务端的实现(二)

    线程存在的问题和临界区 上一章TCP/IP网络编程之多线程服务端的实现(一)的thread4.c中,我们发现多线程对同一变量进行加减,最后的结果居然不是我们预料之内的.其实,如果多执行几次程序,会发现 ...

  8. 决策树python实现小样例

    我们经常使用决策树处理分类问题,近年来的调查表明决策树也是经常使用的数据挖掘算法K-NN可以完成多分类任务,但是它最大的缺点是无法给出数据的内在含义,决策树的主要优势在于数据形式非常容易理解决策树的优 ...

  9. Spring Boot 要点--启动类和热部署

    spring boot需要一个启动类 比如 package com.tianmaying; import org.springframework.boot.SpringApplication; imp ...

  10. IOS开发---菜鸟学习之路--(十七)-利用UITableView实现个人信息界面

    首先来看下我们要实现的效果 需要实现这样的效果 然后我们开始动手吧. 首先选择添加一个新的ViewController 然后打开XIB文件,添加一UITableView 并将样式设置为分组 同时将按住 ...