Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13800   Accepted: 5504

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的点的个数insum和outsum,则答案为insum和max(insum,outsum) 输入:第一行是一个n代表接下来有n行,每行输入小于n个数以0结尾,代表这个点与第i行(当前行数)的值i点相连
#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#include<algorithm>
#define MAX 1100
#define MAXM 2001000
#define INF 0x7ffffff
using namespace std;
int low[MAX],dfn[MAX];
int head[MAX],ans;
int instack[MAX];
int dfsclock,scccnt;
int in[MAX],out[MAX];
int sccno[MAX];
stack<int>s;
vector<int>newmap[MAX];
vector<int>scc[MAX];
struct node
{
int beg,end,next;
}edge[MAXM];
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[ans].beg=u;
edge[ans].end=v;
edge[ans].next=head[u];
head[u]=ans++;
}
void tarjan(int u)
{
int v,i;
s.push(u);
instack[u]=1;
low[u]=dfn[u]=++dfsclock;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
scccnt++;
while(1)
{
v=s.top();
s.pop();
instack[v]=0;
sccno[v]=scccnt;
if(v==u)
break;
}
}
}
void find(int l,int r)
{
dfsclock=scccnt=0;
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(sccno,0,sizeof(sccno));
for(int i=l;i<=r;i++)
{
if(!dfn[i])
tarjan(i);
}
}
void suodian()
{
int u,v;
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=0;i<ans;i++)
{
u=sccno[edge[i].beg];
v=sccno[edge[i].end];
if(v!=u)
{
newmap[u].push_back(v);
in[v]++;
out[u]++;
}
}
}
void solve()
{
int i,j;
int sum,insum,outsum;
insum=outsum=0;
if(scccnt==1)
{
printf("1\n0\n");
return ;
}
for(i=1;i<=scccnt;i++)
{
if(!in[i])
insum++;
if(!out[i])
outsum++;
}
sum=max(insum,outsum);
printf("%d\n%d\n",insum,sum);
}
int main()
{
int n,m,j,i,a;
while(scanf("%d",&n)!=EOF)
{
init();
for(i=1;i<=n;i++)
{
while(scanf("%d",&a),a)
add(i,a);
}
find(1,n);
suodian();
solve();
}
return 0;
}

  

poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】的更多相关文章

  1. POJ 1236 Network of Schools (强连通分量缩点求度数)

    题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...

  2. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

  3. poj~1236 Network of Schools 强连通入门题

    一些学校连接到计算机网络.这些学校之间已经达成了协议: 每所学校都有一份分发软件的学校名单("接收学校"). 请注意,如果B在学校A的分发名单中,则A不一定出现在学校B的名单中您需 ...

  4. poj 1236 Network of Schools : 求需要添加多少条边成为强连通图 tarjan O(E)

    /** problem: http://poj.org/problem?id=1236 缩点后入度为0的点的总数为需要发放软件的学校个数 缩点后出度为0的点的总数和入度为0的点的总数的最大值为需要增加 ...

  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

    poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...

  8. Poj 1236 Network of Schools (Tarjan)

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

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

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

随机推荐

  1. wordpress mobile templates

    http://themeforest.net/category/wordpress/mobile http://themeforest.net/item/monolith-wp-theme-for-b ...

  2. cocos2dx输出信息重定向到控制台

    重定向输出到控制台,方便调试,代码: // uncomment below line, open debug console #define USE_WIN32_CONSOLE int APIENTR ...

  3. iOS 页面间传值 之 属性传值,代理传值

    手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但 ...

  4. csuoj 1351: Tree Counting

    这是一个动态规划的题: 当初想到要用dp,但是一直想不到状态转移的方程: 题解上的原话: 动态规划,设 g[i]表示总结点数为 i 的方案种数,另设 f[i][j]表示各个孩子的总结点数为i,孩子的个 ...

  5. loadrunner_Controller技巧_overlay

    在scenario运行期间,我们经常有类似于:总结Vu数变化,Tps 或者response time变化的趋势或者对比response time 和 tps,那么我们就用的到 Controller的图 ...

  6. [状压dp]POJ1185 炮兵阵地

    中文题 题意不再赘述 对于中间这个“P” 根据dp的无后效性 我们只需考虑前面的 就变成了 只需考虑: 也就是状压前两行 具体与HDOJ的4539类似: 看HDOJ 4539 仅仅是共存状态的判断不同 ...

  7. <算法竞赛入门经典> 第8章 贪心+递归+分治总结

    虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...

  8. INF文件

    百度百科:http://baike.baidu.com/view/637107.htm?fr=ala0_1_1 INF简介 INF是Device INFormation File的英文缩写,是Micr ...

  9. USB Type-C 连接器规范推出之后,市场很多低质量线材容易损坏设备

    USB Type-C 连接器规范推出之后,已有不少行动装置产品使用,其中最知名的产品为 Apple MacBook,机身仅提供一组 Type-C 端口,同时兼具充电与数据传输之用.市面上第三方厂商也开 ...

  10. VC 透明滑动控件Slider Control

    操作系统:Windows 7软件环境:Visual C++ 2008 SP1本次目的:为滑动控件设置背景透明 经常在编写有背景的程序时,滑动控件Slider Control看起来与背景十分不合,我们可 ...