kuangbin专题 专题九 连通图 POJ 1236 Network of Schools
题目链接:https://vjudge.net/problem/POJ-1236
题目:有向图,有若干个连通图,点之间有单向边边就可以单向传递信息,问:
(1)至少需要发送几份信息才能使得每个点都传递到信息
(2)至少需要加几条边,才能使得“把一份信息发送到任意某个点就能传播到其他所有点”成立
思路:tarjan求强连通分量,强联通分量可以相互传递消息,然后,按强联通编号重构图,
统计每个强联通分量的入度出度情况。第一个答案,就显然是入度为0的点,第二个就是max(p,q),构成一个强联通图
这里给情况来解释下max (1)1->2 3->2 (2) 1->2 1->3
p = 入度为0的强联通分量数
q = 出为为0的强联通分量数
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = ;
int head[N],dfn[N],low[N],scc_no[N],s[N],ins[N],ru[N],chu[N];
int n,scc,tim,tot,top;
struct node{
int to;
int nxt;
}e[N*N]; inline void add(int u,int v){
e[tot].to = v;
e[tot].nxt = head[u];
head[u] = tot++;
} void tarjan(int now,int pre){
dfn[now] = low[now] = ++tim;
ins[now] = ;
s[top++] = now; int to;
for(int o = head[now]; ~o; o = e[o].nxt){
to = e[o].to;
if(!dfn[to]){
tarjan(to,now);
low[now] = min(low[now],low[to]);
}
else if(ins[to] && low[now] > dfn[to]) low[now] = dfn[to];
} if(dfn[now] == low[now]){
int x;
++scc;
do{
x= s[--top];
ins[x] = ;
scc_no[x] = scc;
}while(now != x);
}
} //重构图,统计入度出度情况
void rebuild(){
int to;
for(int now = ; now <= n; ++now){
for(int o = head[now]; ~o; o = e[o].nxt){
to = e[o].to;
if(scc_no[to] != scc_no[now]){
++ru[scc_no[to]];
++chu[scc_no[now]];
}
}
}
} int main(){ scanf("%d",&n);
for(int i = ; i <= n; ++i) head[i] = -;
int v;
for(int u = ; u <= n; ++u){
while(~scanf("%d",&v) && v){
add(u,v);
}
}
for(int i = ; i <= n; ++i){
if(!dfn[i])
tarjan(i,i);
}
rebuild();
int p = ,q = ;
for(int i = ; i <= scc; ++i){
if(!ru[i]) ++p;
if(!chu[i]) ++q;
}
if(scc == ) printf("1\n0\n");
else printf("%d\n%d\n",p,max(p,q)); return ;
}
kuangbin专题 专题九 连通图 POJ 1236 Network of Schools的更多相关文章
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- 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(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [tarjan] poj 1236 Network of Schools
主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K To ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- POJ 1236——Network of Schools——————【加边形成强连通图】
Network of Schools Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u ...
- POJ 1236 Network of Schools - 缩点
POJ 1236 :http://poj.org/problem?id=1236 参考:https://www.cnblogs.com/TnT2333333/p/6875680.html 题意: 有好 ...
随机推荐
- SSRF服务器端请求伪造
SSRF漏洞原理 SSRF(Server-Side Request Forgery:服务器端请求伪造)是一种由恶意访问者构造形成由服务端发起请求的一个安全漏洞一般情况下,SSRF访问的目标是从外网无法 ...
- 用windows 画图 裁剪照片
图片大小432*312 1.裁剪大小:打开画图--找到矩形选择 形状裁剪完之后,像素会有相应的变化 2.单纯调整像素: 打开画图----重新调整大小(去掉保持纵横比之后可以任意调整大小) 题目:上传 ...
- 归并排序 ALDS1_5_B:Merge Sort
Merge Sort Write a program of a Merge Sort algorithm implemented by the following pseudocode. You sh ...
- 记录 shell学习过程(11 ) shell 对输出流的处理
语法 awk [options] [BEGIN] {program} [END] [file] 常用命令选项 -F fs 指定描绘一行中数据字段的文件分隔符 默认为空格 -f file 指定读取程序 ...
- Java面向对象--类和对象
面向对象是相对于面向过程而言的,是软件开发方法.面向对象把相关的数据和方法组织为一个整体来看待,从更高的层次来进行系统设计,更贴近事物的自然运行模式.本篇博客介绍Java面向对象的类和对象 目录: 面 ...
- flask操作
models.py class CompanyGoodsModel(Base): id=Column(Integer, primary_key=True) company_id = Column(In ...
- python 删除git Jenkinsfile文件
背景:在做ci集成的发现分支超过100个之后,pipline activity列表中前期的分支会被隐藏,这导致master分支在活动视图中不可见 解决方案:删除历史分支的Jenkinsfile 分支太 ...
- 对malloc和free和数据结构和算法的一些感触
当年2013.9.大一学c程序设计,因为当时还没有学数据结构,只学了程序设计,大学上的课真的是承上启下的不好,刚学到这里,就断了旋一样,对这个malloc和free一直很迷惑,这些狗玩意是干嘛,因为用 ...
- oracle sqlplus链接和sid
1.链接 ( 1.sqlplus /nolog 2.conn sys/sys@172.16.17.36/orcl as sysdba ) 2.sid 数据库名称 ----如:orcl 1.监听程序 ...
- Myeclipse异常
打不开文件 问题描述:Myeclipse然打开什么东西都报错了:Could not open the editor: Invalid thread access 解决方法:1.cmd 2.cd 进入你 ...