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 题意: 有好 ...
随机推荐
- python 元组 列表 字典
type()查看类型 //取整除 **幂 成员运算符: in x在y序列中,就返回true 反之 not in 身份运算符: is is not 逻辑运算符 and or not 字符编码 问题 ...
- java读取解析application.yml
java读取解析application.yml 不用依赖spring容器,可单独使用. bug已修改... 第一步.首先要2个jar <!-- properties和yaml格式化 --> ...
- leetcode - 01
只出现一次的数字 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: ...
- 852. spfa判断负环
给定一个n个点m条边的有向图,图中可能存在重边和自环, 边权可能为负数. 请你判断图中是否存在负权回路. 输入格式 第一行包含整数n和m. 接下来m行每行包含三个整数x,y,z,表示存在一条从点x到点 ...
- ASP.NET MVC5 的请求管道和运行生命周期
https://www.jianshu.com/p/848fda7f79e0 请求处理管道 请求管道是一些用于处理HTTP请求的模块组合,在ASP.NET中,请求管道有两个核心组件:IHttpModu ...
- Leetcode Week3 Merge Two(k) Sorted Lists
Question Q1.Merge two sorted linked lists and return it as a new list. The new list should be made b ...
- AcWing 1058. 股票买卖 V
//初始状态(入口)转移到手中无货的第>=2天 //最终状态(出口)可能从手中无货的第一天转移过来,或者从手中无货的第>=2天 //f[i,0]表示走到第i天,且位于手中有货的状态 //f ...
- Windows中配置maven环境变量
添加maven环境变量 添加新的系统环境变量:MAVEN_HOME 设置其值为你安装的目录:D:\IT_program\apache-maven-3.6.0 更新系统PATH 变量 添加变量值:%MA ...
- Selenium3+python自动化013-自动化数据驱动及模型介绍
一.查看当前运行的浏览等相关信息 driver=webdriver.Chrome() print(driver.capabilities["version"]) #浏览器版本 pr ...
- MySQL 分组并多行拼接 group_concat 用法
数据源 user name age 小红 18 小明 18 小芳 19 ------------------------------------------------------------ ...