这道题的意思是就是

问题

1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。

2:至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。

其实问题1就是问,这个图的支配集有多少???解决这个问题非常简单,把图缩点后,找到有多少个入度为0的点,就是支配集。一个支配内,所有点可能不是强连通的,但是都可以通过这个点到达。

而问题二其实更加简单,我们只需要把入读为0的点和出度为0的点的数目取一个最大值即可,至于问什么,很简单,我们可以把出度为0的点连接到入度为0的点上面去,这样就保证图的强连通性质。其实这就是求一个图的补图。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N = ,M=;
int ver[M],Next[M],head[N],dfn[N],low[N];
int sta[N],ins[N],c[N],in_deg[N],out_deg[N];
int n,m,tot,num,top,cnt;
void add(int x,int y)
{
ver[++tot]=y,Next[tot]=head[x],head[x]=tot;
}
void tarjan(int x)
{
dfn[x]=low[x]=++num;//序列号+1
sta[++top]=x,ins[x]=;//入栈,并标记在栈内
for (int i=head[x]; i; i=Next[i]) //开始访问
if(!dfn[ver[i]]) //如果没有被处理过
{
tarjan(ver[i]);//DFS
low[x]=min(low[x],low[ver[i]]);
//递归出来,比较谁是谁的儿子/父亲,就是树的对应关系,涉及到强连通分量子树最小根的事情
}
else if (ins[ver[i]])
low[x]=min(low[x],dfn[ver[i]]);
//比较谁是谁的儿子/父亲。就是链接对应关系
if (dfn[x] == low[x])//发现是整个强连通分量的子树里面的最小根。
{
cnt++;
int y;
do
{
y=sta[top--],ins[y]=;
c[y]=cnt;
//scc[cnt].push_back(y);
}
while(x!=y);
}
}
void solve(int n)
{
for (int i=; i<=n; i++)
{
if (!dfn[i])
{
tarjan(i);
}
}
if (cnt==)
{
printf("1\n0\n");
return;
}
for (int u=; u<=n; ++u)
{
for (int j=head[u]; j; j=Next[j])
{
int v=ver[j];
if (c[u]==c[v])
{
continue;
}
out_deg[c[u]]++;
in_deg[c[v]]++;
}
}
int a,b;
a=;
b=;
for (int i=; i<=cnt; i++)
{
if (in_deg[i]==)
{
a++;
}
else if (out_deg[i]==)
{
b++;
}
}
printf("%d\n",a);
printf("%d\n",max(a,b)); }
void init()
{
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(head,,sizeof(head));
memset(in_deg,,sizeof(in_deg));
memset(out_deg,,sizeof(out_deg));
memset(ins,,sizeof(ins));
top=;
tot=;
cnt=;
num=;
}
int main()
{
int tmp;
while(~scanf("%d",&n))
{
init();
for (int i=; i<=n; i++)
{
while(scanf("%d",&tmp)&&tmp)
{
add(i,tmp);
}
}
solve(n);
}
return ;
}

kuangbin专题-连通图A - Network of Schools的更多相关文章

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

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

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

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

  3. [kuangbin]专题六 最小生成树 题解+总结

    kuangbin专题链接:https://vjudge.net/article/752 kuangbin专题十二 基础DP1 题解+总结:https://www.cnblogs.com/RioTian ...

  4. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  5. Network of Schools --POJ1236 Tarjan

    Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...

  6. [强连通分量] POJ 1236 Network of Schools

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16803   Accepted: 66 ...

  7. POJ1236 - Network of Schools tarjan

                                                     Network of Schools Time Limit: 1000MS   Memory Limi ...

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

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

  9. POJ1236 Network of Schools (强连通)(缩点)

                                                                Network of Schools Time Limit: 1000MS   ...

随机推荐

  1. canvas旋转图片

    canvas旋转图片 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  2. 【CodeVS】1023 GPA计算

    1023 GPA计算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 小松终于步入了大学的殿堂,带着兴奋和憧憬,他参加了信息科学 ...

  3. laravel-admin

    laravel-admin 文档地址: http://laravel-admin.org/docs/#/zh/

  4. python csv文件打开错误:_csv.Error: line contains NULL byte

    当python读取文件出现_csv.Error: line contains NULL byte时, # -*- coding:utf-8 -*- import csv with open(r'E:\ ...

  5. 如何用maven tycho构建自己的Eclipse RCP应用

    在你写了一个Eclipse插件之后,也许你就会想如何把它变成一个P2的项目或者是一个Java App让大家可以安装到自己的Eclipse上,dangdangdang~~ 这是你就可以利用maven-t ...

  6. 深入浅出Cocoa之消息(二)-详解动态方法决议(Dynamic Method Resolution) 【转】

    序言 如果我们在 Objective C 中向一个对象发送它无法处理的消息,会出现什么情况呢?根据前文<深入浅出Cocoa之消息>的介绍,我们知道发送消息是通过 objc_send(id, ...

  7. CSS基础学习中的几大要点心得

    CSS是前端学习中较为简单但又非常复杂的课程,说简单是因为学习它并不需要太多推理论证和逻辑思维,说它非常复杂则是更多的侧重在它的“杂”上,因为太多的格式和套路需要我们注意.以下谨列出本人在CSS学习中 ...

  8. 【JZOJ4810】【NOIP2016提高A组五校联考1】道路规划

    题目描述 输入 输出 样例输入 5 1 4 5 2 3 3 4 2 1 5 样例输出 3 数据范围 样例解释 解法 模型显然. 设第一列为a[],第二列为b[],f[i]为前i个数的最大答案. 顺序枚 ...

  9. 如何写JavaScript中的callback回调函数

    如何写回调函数? 如果自己在写一个方法或函数,你有可能会遇到需要一个回调函数.下面就是一个简单的常见回调函数例子: function mySandwich(param1, param2, callba ...

  10. iOS 9 学习系列:UIStack View

    http://www.cocoachina.com/ios/20150921/13492.html 在 iOS9 中,Apple 引入了 UIStackView,他让你的应用可以通过简单的方式,纵向或 ...