这道题的意思是就是

问题

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. ADSL pppoe 拔号工具rp-pppoe

    rp-pppoe 目前在各大发行版本都是存在的,比如Redhat/Fedora.红旗.Slackware.Debian.SuSE等系统,都是采用这个拔号软件,所以您大可不必为下载源码编译安装.只需要在 ...

  2. 计蒜客 Red Black Tree(树形DP)

    You are given a rooted tree with n nodes. The nodes are numbered 1..n. The root is node 1, and m of ...

  3. Centos系统Python环境搭建和项目部署

    目录 一.Python 1. 源安装 Python3 2. SCL安装 Python3 3. 虚拟环境venv 4. 安装Flask 5. 安装gunicorn 二.安装Nginx 1. 安装Ngin ...

  4. AppScan操作手册

    AppScan操作手册 转自:http://blog.51cto.com/zhouanya/1388681   1.SQL注入 1.1.什么是sql注入 所谓SQL注入(SQL Injection), ...

  5. asp.net技术(公共方法)

    #region 获取 本周.本月.本季度.本年 的开始时间或结束时间 /// <summary> /// 获取开始时间 /// </summary> /// <param ...

  6. SAS之大话PDV

    SAS之大话PDV 之所以说是数据源,而非输入缓冲区的原因上一条推送已经说明,这里就不再啰嗦啦. 这里我们且将DATA步流程简单地分为从数据源读入到pdv和从pdv写入数据集. IF语句 & ...

  7. ThinkPHP中_after_update、_before_update等的用法

    https://blog.csdn.net/aslackers/article/details/50339163 TP系统\Think\Model类里隐藏了几个有用的方法: _before_inser ...

  8. GeoServer手动发布本地Shapefile地图

    首先,本文实现的结果图给大家展现一下: 放大的样子: 颜色是通过属性中某个字段值来分级的,可以自定义. 上面功能是用ArcGIS切片好数据,在Geoserver 中发布,并用google地图作为底图展 ...

  9. js树状菜单

    html部分 <ul class="tree"> <li><span><a href="#">JavaScrip ...

  10. More Effective C++: 02操作符

    05:谨慎定义类型转换函数 有两种函数允许编译器进行隐式类型转换:单参数构造函数(single-argument constructors)和隐式类型转换运算符.单参数构造函数是指只用一个参数即可以调 ...