这道题的意思是就是

问题

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. 页面PC端 / 移动端整屏纵向翻页,点击/触摸滑动效果功能代码非插件

    页面翻页,滑动功能示范代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  2. leetcode 839 Similar String Groups

    题目 Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that ...

  3. leetcode 76 dp& 强连通分量&并查集经典操作

    800. Similar RGB Color class Solution { int getn(int k){ return (k+8)/17; } string strd(int k){ char ...

  4. Zookeeper安装过程

    zookeeper的安装我反反复复安装了三次,前两次在root用户下安装都失败了,都启动不起来,第三次我改成普通用户安装,没想到成功了,很不可思议,步骤完全一样,接下来介绍一下具体的安装步骤: 1. ...

  5. GYM 101981E(开关反转性质)

    要点 做法是删去连续的k个0或k个1,连消.消消乐的那种,网上博主用个栈\(O(n)\)就很优秀地操作了这个过程 原因是有性质:比如k=3,101000贪心地翻就能翻成000101,所以连续的k个可以 ...

  6. CommonJS、requirejs、ES6的对比

    文件路径 首先先搞清楚文件路径的写法,这里我总是记不住,有点晕,正好这次整理一下. 以 / 为起始,表示从根目录开始解析: 以 ./ 为起始,表示从当前目录开始解析: 以 ../ 为起始,表示从上级目 ...

  7. windows 操作系统下git报filename too long 处理方法

    两种方法解决: 一是通过修改配置文件 [core] repositoryformatversion = filemode = false bare = false logallrefupdates = ...

  8. 那些年,我们见过的Java服务端乱象

    导读 查尔斯·狄更斯在<双城记>中写道:“这是一个最好的时代,也是一个最坏的时代.”移动互联网的快速发展,出现了许多新机遇,很多创业者伺机而动:随着行业竞争加剧,互联网红利逐渐消失,很多创 ...

  9. 考试总结 模拟27(W)

    心得:太弱了,T1问题:理解错了题,矿石可以放到同一处,,太弱了,小凯的疑惑,没什么印象T2问题:拆式子T3问题:换根dp的思想模拟9T1+T2

  10. jquery购物评分

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...