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 题意: 有好 ...
随机推荐
- ASP.NET Core 2.1 中的 HttpClientFactory (Part 4) 整合Polly实现瞬时故障处理
原文:https://www.stevejgordon.co.uk/httpclientfactory-using-polly-for-transient-fault-handling发表于:2018 ...
- php 时间 日期
获取月初与月末 /** * 获取当前月初与月末时间 * * */ $month =8; $year = 2019; $startDay = $year . '-' . $month . '-1'; $ ...
- CSS的文本样式
CSS的文本样式 1.颜色 2.文本对齐方式 3.首行缩进 4.行高 5.装饰 1. 文本位置 居中: text-align: center; 靠左: text-align: left; 靠右: te ...
- pandas 读取excel时,遇到数字变为科学计数法了, 怎么破?? 别慌 这样来处理
# 指定字段以string 方式读取 df = pd.read_excel("./test.xlsx", converters={"id": str})
- SDOI2010 粟粟的书架 lg2468(可持久化,前缀和)
题面见https://www.luogu.org/problemnew/show/P2468 然后这道题属于合二为一题,看一眼数据范围就能发现 首先我们先考虑50分,二维前缀和维护一下(反正我不记得公 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛---Find Integer!--hdu6441
问题传送门:https://vjudge.net/contest/320779#problem/D 介绍一个名词:奇偶数列法则 Key part: #include<iostream> # ...
- ubuntu---【nvcc --version】显示错误,提示 sudo apt-get install nvidia-cuda-toolkit
重装了一下cuda,然后发现nvcc命令不存在了,终端提示使用 : sudo apt-get install nvidia-cuda-toolkit 来使用nvcc. 注意不要使用这种方式安装.系统认 ...
- vector 牛逼 +lower_bound+ upper_bound
vector 超级 日白 解决的问题空间问题,可以自由伸缩. 一下用法: 向量大小: vec.size(); 向量判空: vec.empty(); 末尾添加元素: vec.push_back(); / ...
- Java传(2)
__________________________夜夜都是魂牵梦绕. 题目: 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月有生一对兔子,假如兔子都不死,问每个月的兔子 ...
- 在PDB级别中如何切换或重建UNDO表空间
Oracle 12.1版本中,UNDO表空间仅存在CDB级别(共享UNDO),来自于AskScuti博客园. Oracle 12.2版本开始,UNDO表空间同时可以存在每个PDB级别(本地UNDO). ...