http://poj.org/problem?id=1236

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9481   Accepted: 3767

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

 
【题解】:
  这题大意是给一个有向图,求至少给多少个结点发消息能使消息传遍整个网络,并进一步求出至少添加多少条边能使对图中任意一个结点发消息都能使消息传遍整个网络。可以先用kosaraju将强连通分支缩点,得到原图的基图,然后统计入度为0的连通分量个数和出度为0的连通分量个数,入度为0的必须给它发消息,入度不为0的不必给发消息,所以第一问所求即为缩点后的图中入度为0的个数,至于第二问,只需将入度为0的结点与出度为0的结点连接即可满足要求,最少需加边数目为两者之中的较大者,需注意的是,单只有一个连通分量时,输出结果为0 。
 
【code】:
 
 /**
Judge Status:Accepted Memory:772K
Time:0MS Language:G++
Code Length:2155B Author:cj
*/ #include<iostream>
#include<stdio.h>
#include<stack>
#include<string.h>
#include<algorithm>
#include<vector> #define N 110
using namespace std; vector<int> G[N];
int pre[N],lowlink[N],sccno[N],dfs_cnt,scc_cnt;
stack<int> stk; int visit[N][N],in[N],out[N];
void Tarjan(int u)
{
pre[u] = lowlink[u] = ++dfs_cnt;
stk.push(u);
int i;
for(i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!pre[v])
{
Tarjan(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u] = min(lowlink[u],pre[v]);
}
}
if(pre[u]==lowlink[u])
{
int x;
scc_cnt++;
do
{
x = stk.top();
stk.pop();
sccno[x] = scc_cnt;
}while(x!=u);
}
} void findncc(int n)
{
dfs_cnt = scc_cnt = ;
memset(pre,,sizeof(pre));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
int i;
for(i=;i<=n;i++) if(!pre[i]) Tarjan(i);
} void getNewMap(int n)
{
int i,j;
memset(visit,,sizeof(visit));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
for(i=;i<=n;i++)
{
for(j=;j<G[i].size();j++)
{
int v = sccno[G[i][j]];
int u = sccno[i]; //注意是对sccno[i]数组里的强联通分量进行操作,也就是缩点的过程
if(u!=v)
{
if(!visit[u][v])
{
visit[u][v] = ;
in[v]++; //出度入度统计
out[u]++;
}
}
}
}
}
int main()
{
int n;
scanf("%d",&n);
int i;
for(i=;i<=n;i++)
{
int a;
G[i].clear();
while(~scanf("%d",&a)&&a)
{
G[i].push_back(a);
}
}
findncc(n);
getNewMap(n);
int cnt_in = ,cnt_out = ;
for(i=;i<=scc_cnt;i++)
{
if(!in[i]) cnt_in++;
if(!out[i]) cnt_out++;
}
printf("%d\n",cnt_in);
if(scc_cnt!=) printf("%d\n",max(cnt_in,cnt_out)); //联通分量只有一个输出0
else printf("0\n");
return ;
}

最新文章

  1. [python实现设计模式]-5.迭代器模式-一起撸串嗨皮啦
  2. UWP应用开发系列视频教程简介 - Built for Windows 10
  3. u3d_shader_surface_shader_5
  4. 北京联想招聘-IOS高级 加入qq 群:220486180 或者直接在此 留言咨询
  5. Java集合Map接口与Map.Entry学习
  6. 自适应网页设计(Responsive Web Design)(转)
  7. linux进程间通信方式
  8. sql日期函数
  9. JavaScript奇技淫巧44招
  10. 使用strace命令跟踪系统调用
  11. HashTable和HashMap的区别详解(转)
  12. AX2009 批处理作业中使用多线程---批量捆绑
  13. Python-Django&#160;Win7上使用Apache24和mod_wsgi部署Django1.11应用程序
  14. sqlite线程模式的设置
  15. Torchvision 源码安装[Ubuntu]
  16. 响应式网站设计(Responsive Web design)
  17. Ubuntu 16.04下如何安装VMware-Workstation
  18. 数据库sql语句例题(转)
  19. hdu 2063 过山车【匈牙利算法】(经典)
  20. Linux操作系统-系统安装与分区

热门文章

  1. 如何使用CSS实现小三角形效果
  2. 从零单排Linux – 1 – 简单命令
  3. WebAPI GET和POST请求的几种方式
  4. winform无边框拖动
  5. Android 线程Thread的2种实现方法
  6. Linux -Yum 命令详解
  7. android apk 逆向中常用工具一览
  8. plsqldev与sqldeveloper
  9. 【leetcode】363. Max Sum of Rectangle No Larger Than K
  10. Oracle用户system解锁