Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9073   Accepted: 3594

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

Source

 
 
 
这题给了一个有向图。
 
需要解决两个问题:
第一是需要给多少个点,才能传遍所有点。
 
第二问是加多少条边,使得整个图变得强连通。
 
使用Tarjan进行缩点,得到一个SCC图、
 
这个图有多少个入度为0的,多少个出度为0的。
 
假设有n个入度为0,m个出度为0
 
那么第一个答案就是n,第二个答案是max(n,m)
 
 
具体证明不解释了,貌似以前做过的题目,有解释。
 
需要注意的是假如只有一个强连通分量,即整个图是连通的,那么第一个答案是1,第二个答案是0
 
 
#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 (有向图的强连通分量)的更多相关文章

  1. POJ 1236 Network of Schools 有向图强连通分量

    参考这篇博客: http://blog.csdn.net/ascii991/article/details/7466278 #include <stdio.h> #include < ...

  2. 【POJ 1236 Network of Schools】强联通分量问题 Tarjan算法,缩点

    题目链接:http://poj.org/problem?id=1236 题意:给定一个表示n所学校网络连通关系的有向图.现要通过网络分发软件,规则是:若顶点u,v存在通路,发给u,则v可以通过网络从u ...

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

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

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

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

  5. Poj 1236 Network of Schools (Tarjan)

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

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

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

  7. [tarjan] poj 1236 Network of Schools

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

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

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

  9. POJ 1236 Network of Schools(tarjan算法 + LCA)

    这个题目网上有很多答案,代码也很像,不排除我的.大家的思路应该都是taijan求出割边,然后找两个点的LCA(最近公共祖先),这两个点和LCA以及其他点构成了一个环,我们判断这个环上的割边有几条,我们 ...

随机推荐

  1. hibernate--关联映射(一对多)

    在对象模型中,一对多的关联关系,使用集合来表示. 实例场景:班级对学生:Classes(班级)和Student(学生)之间是一对多的关系. 对象模型: 多对一.一对多的区别: 多对一关联映射:在多的一 ...

  2. 无法生成临时类(result=1)。 error CS0229: “DCSoftDotfuscate.aam.a”与“DCSoftDotfuscate.aam.a()”之间存在二义性

    对于错误无法生成临时类(result=1).error CS0229: “DCSoftDotfuscate.aam.a”与“DCSoftDotfuscate.aam.a()”之间存在二义性 出现这个错 ...

  3. Android 中Activity生命周期分析(二):从AActivity 到BActivity过程分析

    如果你没有动手去演示的话,你一定要去动手试试看,这个东西非学容易出错,面试中经常出现,好了,上代码: package com.king.review.base; import android.app. ...

  4. ios 开发中 developer tools access 总是要输入密码问题的解决

    我一直没有想法去解决这个问题:打开iphone模拟器的时候,老是弹出developer tools access 让我输入密码, 今天我在打开模拟器的时候又弹出这个对话框,我愤怒了,于是我在网上查了一 ...

  5. JAVA中获取项目文件路径

    在java中获得文件的路径在我们做上传文件操作时是不可避免的. web 上运行 1:this.getClass().getClassLoader().getResource("/" ...

  6. 【linux】命令

    pwd 显示路径 whereis jupyterhub find / -name base.py reboot 重启 grep

  7. 门户网站架构Nginx+Apache+MySQL+PHP+Memcached+Squid

    服务器的大用户量的承载方案 一.前言二.编译安装三. 安装MySQL.memcache四. 安装Apache.PHP.eAccelerator.php-memcache五. 安装Squid六.后记 一 ...

  8. Android Studio如何快速生成get,set,tostring,构造函数

    刚开始使用Android Studio时,在创建一个javabean时,很习惯的在JavaBean类中,右键去找生成get,set等选项.但是很遗憾,找不到. 那这边如何快速的set,get或者生成构 ...

  9. [Everyday Mathematics]20150112

    设 $f\in C[0,1]$ 适合 $$\bex \int_x^1 f(t)\rd t\geq \frac{1-x^2}{2},\quad \forall\ x\in [0,1]. \eex$$ 试 ...

  10. linux-LINUX试题

    ylbtech-doc:linux-LINUX试题 LINUX试题 1.A,LINUX试题返回顶部 01.{Linux题目}在使用匿名登录ftp时,用户名为(  )? (选择1项) A) login ...