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

开始用桥来判断是不是同一个连通分量,结果果断错了,其实下图应该就会出错

原因是通过头插法先遍历3,结果3的出度为0,由于2通向3已经访问过,因此不能在访问,因此2-->3的路没有标志cut,没法统计这天边的出入度情况,因此出度为0的变为2个了,正确答案应该是1个,所以错了,不能企图通过桥来算出出度入度

错误代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MOD 100000
#define inf 1<<29
#define LL long long
#define MAXN 20010
#define MAXM = 50010
using namespace std;
struct Edge
{
int to,next;
bool cut;
} edge[MAXN]; int head[MAXN],tot;
int low[MAXN],DFN[MAXN],belong[MAXN];///belong 的值为1-block
int index,top,fenzhiNum;
int block ; ///强连通分量
bool inStack[MAXN];
int bridgeNum; ///桥的数目
int stack[MAXN];
int vis[MAXN];
int inans,outans;
int outdu[MAXN];
int indu[MAXN]; void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].cut = false;
head[u] = tot++ ;
}
void ini(){
index = block = top = fenzhiNum = 0;
inans = 0, outans = 0 ;
memset(DFN,0,sizeof(DFN));
memset(inStack,false,sizeof(inStack));
memset(vis,0,sizeof(vis));
memset(outdu,0,sizeof(outdu));
memset(indu,0,sizeof(indu));
}
void Tarjan(int u)
{
vis[u] = true;
int v;
low[u] = DFN[u] = ++index;
stack[top++] = u;
inStack[u] = true;
for(int i=head[u] ; i!=-1 ; i=edge[i].next)
{
v = edge[i].to;
//if( v == pre ) continue; ///因为是无向图,所以两条是双向的,所以只遍历一条就够了
if( !DFN[v] )
{
Tarjan(v );
if(low[u]>low[v])
low[u] = low[v];
if(low[v] > DFN[u] ){
bridgeNum++;
edge[i].cut = true;
//edge[i^1].cut = true; ///将两条双向边都设置为桥
} }
else if( inStack[v] && low[u] > DFN[v])
low[u] = DFN[v];
}
if(low[u] == DFN[u])
{
block++;
do
{
v=stack[--top]; ///清空当前强连通分量栈 必须清空
inStack[v] = false;
belong[v]=block; ///v节点都编号为block 也就是这是一个块
}
while(v!=u);
}
} void solve(int N)
{
ini();
for(int i=1;i<=N;i++)
if(!vis[i])
Tarjan(i);
for(int i=1; i<=N ; i++){ ///缩点
for(int j=head[i] ; j!=-1 ; j=edge[j].next)
if( edge[j].cut)//belong[i]!=belong[ edge[j].to ])//edge[j].cut )
indu[ belong[ edge[j].to ] ]++,outdu[ belong[i] ]++ ;
}
for(int i=1;i<=block ;i++)
if(indu[i] == 0)
inans++;
for(int i=1;i<=block ;i++)
if(outdu[i] == 0)
outans++;
// printf("indu=%d,outdu=%d\n",inans,outans);
if(block == 1) printf("1\n0\n");
else printf("%d\n%d\n",inans,max(inans,outans));
//printf("%d\n",(ans+1)/2 );
} int main ()
{
int n,m;
while(~scanf("%d",&n))
{
int u,v,mark=0;
tot=0;
memset(head,-1,sizeof(head));
for(int i=1; i<=n; i++)
{
while(scanf("%d",&u)&&u!=0){
mark=0;
for(int j=head[i] ; j!=-1 ; j=edge[j].next) ///去重边
if(edge[j].to == u){
mark = 1;
break;
}
if(!mark) addedge(i,u);
}
}
solve(n);
}
return 0;
}

正确代码矩阵:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXV 110
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b) int n,map[MAXV][MAXV],outdegree[MAXV],indegree[MAXV];
int dfn[MAXV]; //第一次访问的步数
int low[MAXV]; //子树中最早的步数
int stap[MAXV],stop; //模拟栈
bool instack[MAXV]; //是否在栈中
int count; //记录连通分量的个数
int cnt; //记录搜索步数
int belong[MAXV]; //属于哪个连通分量 void init(){
count=stop=cnt=0;
memset(instack,false,sizeof(instack));
memset(map,0,sizeof(map));
memset(dfn,0,sizeof(dfn));
} void tarjan(int x){
int i;
dfn[x]=low[x]=++cnt;
stap[stop++]=x;
instack[x]=true;
for(i=1;i<=n;i++){
if(!map[x][i]) continue;
if(!dfn[i]){
tarjan(i);
low[x]=min(low[i],low[x]);
}else if(instack[i])
low[x]=min(dfn[i],low[x]);
//与x相连,但是i已经被访问过,且还在栈中
//用子树节点更新节点第一次出现的时间
} if(low[x]==dfn[x]){
count++;
while(1){
int tmp=stap[--stop];
belong[tmp]=count;
instack[tmp]=false;
if(tmp==x) break;
}
}
} void output(){
int i,j,inzero=0,outzero=0;
for(i=1;i<=n;i++){
indegree[i]=outdegree[i]=0;
}
for(i=1;i<=n;i++) //找连通分量入度与出度
for(j=1;j<=n;j++)
if(map[i][j] && belong[i]!=belong[j]){
indegree[belong[j]]++;
outdegree[belong[i]]++;
}
for(i=1;i<=count;i++){ //找入度与出度为0的点
if(!indegree[i]) inzero++;
if(!outdegree[i]) outzero++;
} if(count==1) //只有1个结点要特判
printf("1\n0\n");
else
printf("%d\n%d\n",inzero,max(inzero,outzero));
} int main(){
int i,a;
while(~scanf("%d",&n)){
init();
for(i=1;i<=n;i++){
while(scanf("%d",&a) && a) map[i][a]=1;
}
for(i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
output();
}
return 0;
}

正确代码邻接表

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MOD 100000
#define inf 1<<29
#define LL long long
#define MAXN 20010
#define MAXM = 50010
using namespace std;
struct Edge
{
int to,next;
bool cut;
} edge[MAXN]; int head[MAXN],tot;
int low[MAXN],DFN[MAXN],belong[MAXN];///belong 的值为1-block
int index,top,fenzhiNum;
int block ; ///强连通分量
bool inStack[MAXN];
int bridgeNum; ///桥的数目
int stack[MAXN];
int vis[MAXN];
int inans,outans;
int outdu[MAXN];
int indu[MAXN]; void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].cut = false;
head[u] = tot++ ;
}
void ini(){
index = block = top = fenzhiNum = 0;
inans = 0, outans = 0 ;
memset(DFN,0,sizeof(DFN));
memset(inStack,false,sizeof(inStack));
memset(vis,0,sizeof(vis));
memset(outdu,0,sizeof(outdu));
memset(indu,0,sizeof(indu));
}
void Tarjan(int u)
{
vis[u] = true;
int v;
low[u] = DFN[u] = ++index;
stack[top++] = u;
inStack[u] = true;
for(int i=head[u] ; i!=-1 ; i=edge[i].next)
{
v = edge[i].to;
//if( v == pre ) continue; ///因为是无向图,所以两条是双向的,所以只遍历一条就够了
if( !DFN[v] )
{
Tarjan(v );
if(low[u]>low[v])
low[u] = low[v];
if(low[v] > DFN[u] ){
bridgeNum++;
edge[i].cut = true;
//edge[i^1].cut = true; ///将两条双向边都设置为桥
} }
else if( inStack[v] && low[u] > DFN[v])
low[u] = DFN[v];
}
if(low[u] == DFN[u])
{
block++;
do
{
v=stack[--top]; ///清空当前强连通分量栈 必须清空
inStack[v] = false;
belong[v]=block; ///v节点都编号为block 也就是这是一个块
}
while(v!=u);
}
} void solve(int N)
{
ini();
for(int i=1;i<=N;i++)
if(!vis[i])
Tarjan(i);
for(int i=1; i<=N ; i++){ ///缩点
for(int j=head[i] ; j!=-1 ; j=edge[j].next)
if( belong[i]!=belong[ edge[j].to ] )
indu[ belong[ edge[j].to ] ]++,outdu[ belong[i] ]++ ;
}
for(int i=1;i<=block ;i++)
if(indu[i] == 0)
inans++;
for(int i=1;i<=block ;i++)
if(outdu[i] == 0)
outans++;
// printf("indu=%d,outdu=%d\n",inans,outans);
if(block == 1) printf("1\n0\n");
else printf("%d\n%d\n",inans,max(inans,outans));
//printf("%d\n",(ans+1)/2 );
} int main ()
{
int n,m;
while(~scanf("%d",&n))
{
int u,v,mark=0;
tot=0;
memset(head,-1,sizeof(head));
for(int i=1; i<=n; i++)
{
while(scanf("%d",&u)&&u!=0){
mark=0;
for(int j=head[i] ; j!=-1 ; j=edge[j].next) ///去重边
if(edge[j].to == u){
mark = 1;
break;
}
if(!mark) addedge(i,u);
}
}
solve(n);
}
return 0;
}

Network of Schools(强连通分量缩点(邻接表&矩阵))的更多相关文章

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

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

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

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

  3. Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13801   Accepted: 55 ...

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

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

  5. POJ1236Network of Schools[强连通分量|缩点]

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16571   Accepted: 65 ...

  6. poj-1236.network of schools(强连通分量 + 图的入度出度)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27121   Accepted: 10 ...

  7. [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)

    nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...

  8. POJ1236 Network of Schools (强连通分量,注意边界)

    A number of schools are connected to a computer network. Agreements have been developed among those ...

  9. 【强连通分量缩点】poj 1236 Network of Schools

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

随机推荐

  1. 在.net中悄悄执行dos命令,并获取执行的结果(转)

    一.怎样使dos命令悄悄执行,而不弹出控制台窗口? 1.需要执行带“/C”参数的“cmd.exe”命令,它表示执行完命令后立即退出控制台.2.设置startInfo.UseShellExecute = ...

  2. smarty函数-继承extents

    继承<{extends}> {extends}标签用在模版中的第一行: 如果子模板用{extends}标签继承父模板,那么它只能包含{block}标签(内容),其它任何模板内容都将忽略: ...

  3. Winform退出程序

    1.this.Close(); 只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Application.Exit(); 强制所有消息中止,退 ...

  4. 【转】java 自动装箱与拆箱

    java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...

  5. Maven 和 Ant 的区别?

    Maven 和 Ant 有什么不同呢?在回答这个问题以前,首先要强调一点:Maven 和 Ant 针对构建问题的两个不同方面.Ant 为 Java 技术开发项目提供跨平台构建任务.Maven 本身描述 ...

  6. 【转】双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计

    架构简介 前几天网友来信说帮忙实现这样一个架构:只有两台机器,需要实现其中一台死机之后另一台能接管这台机器的服务,并且在两台机器正常服务时,两台机器都能用上.于是设计了如下的架构.此架构主要是由kee ...

  7. Java 中Timer和TimerTask 定时器和定时任务使用的例子

    转自:http://blog.csdn.net/kalision/article/details/7692796 这两个类使用起来非常方便,可以完成我们对定时器的绝大多数需求 Timer类是用来执行任 ...

  8. 黄聪:HtmlAgilityPack,C#实用的HTML解析类简介

    HtmlAgilityPack是.net下的一个HTML解析类库.支持用XPath来解析HTML.这个意义不小,为什么呢?因为对于页面上的元素的xpath某些强大的浏览器能够直接获取得到,并不需要手动 ...

  9. apache部署多个项目

    配置多个ip 简单的说,打开httpd.conf 在最后加入如下内容: <VirtualHost 127.0.0.2:80> DocumentRoot d:/AppServ/www2 Se ...

  10. 华人曾与IBM抗衡! 盘点已远去的IT巨头(转)

    [PConline资讯 ]从算盘到计算器,从大型机到个人PC,再到当今火热的移动终端和云计算,人类计算史已经走过了千年之久,现代IT计算领域也经过了百年浮沉.在世界工业领域,IT技术应该是诞生时间最短 ...