kuangbin专题-连通图A - Network of Schools
这道题的意思是就是
问题
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的更多相关文章
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj-1236.network of schools(强连通分量 + 图的入度出度)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27121 Accepted: 10 ...
- [kuangbin]专题六 最小生成树 题解+总结
kuangbin专题链接:https://vjudge.net/article/752 kuangbin专题十二 基础DP1 题解+总结:https://www.cnblogs.com/RioTian ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- Network of Schools --POJ1236 Tarjan
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...
- [强连通分量] POJ 1236 Network of Schools
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16803 Accepted: 66 ...
- POJ1236 - Network of Schools tarjan
Network of Schools Time Limit: 1000MS Memory Limi ...
- POJ 1236 Network of Schools (Tarjan + 缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12240 Accepted: 48 ...
- POJ1236 Network of Schools (强连通)(缩点)
Network of Schools Time Limit: 1000MS ...
随机推荐
- JSP-response(HttpServletResponse)
1 HttpServletResponse概述 2 Response 运行过程 3 通过抓包工具抓取Http响应 4 响应行 5 设置响应头 set 和add的区别 6 重定向 需要完成分析‘ 6 ...
- H5C3--FileReader和拖拽的应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue学习之组件(component)(二)
自定义事件 父组件使用 prop 传递数据给子组件.但子组件怎么跟父组件通信呢?这个时候 Vue 的自定义事件系统就派得上用场了. 1. 使用 v-on 绑定自定义事件 每个vue实例都实现了事件接口 ...
- mybatis-plus思维导图,让mybatis-plus不再难懂
mybatis-plus与mybatis mybatis Mybatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置 ...
- SpingBoot myBatis neo4j整合项目案例
此项目为spring boot - myBatis - neo4j数据库整合项目. 有增删改查(节点关系).动态分页条件排序等一些示例. git下载地址:git clone https://githu ...
- cnn.py cs231n
n import numpy as np from cs231n.layers import * from cs231n.fast_layers import * from cs231n.layer_ ...
- laravel之文件上传
laravel框架中的文件上传我们应该如何实现此功能呢? 之前也是没有使用过laravel的文件上传功能,后来在网上找到一些教程,五花八门.让我看起来有点头疼. 有时候找到测试浪费好长时间最后还是出不 ...
- SQLServer → 09:索引
一.索引概念 用途 我们对数据查询及处理速度已成为衡量应用系统成败的标准,而采用索引来加快数据处理速度通常是最普遍采用的优化方法. 概念 索引是一个单独的,存储在磁盘上的数据结构,它们包含则对数据表里 ...
- 【itsdangerous】的加密解密原理(易懂版)
from itsdangerous import TimedJSONWebSignatureSerializer import time from itsdangerous import Signat ...
- k8s 超详细总结,面试必问
一个目标:容器操作:两地三中心:四层服务发现:五种Pod共享资源:六个CNI常用插件:七层负载均衡:八种隔离维度:九个网络模型原则:十类IP地址:百级产品线:千级物理机:万级容器:相如无亿,K8s有亿 ...