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 题意: 有好 ...
随机推荐
- linux--介绍和指令练习
Linux Linux就是个操作系统:它和Windows XP.Windows7.8.10什么的一样就是一个操作系统而已! Linux能干什么:能当服务器,在服务器上安装者各种企业应用.服务. 比如: ...
- Hibernate 和Mybatis的区别
Hibernate 和Mybatis的区别 1.hibernate 入门门槛高,是一个标准的ORM框架(对象关系映射),不需要程序写sql,sql语句自动生成,对sql语句进行优化.修改比较困难. ...
- #助力CSP2019# OI中容易出现的**错误汇总
多测不清空,爆0两行泪 3年OI一场空,不开long long见祖宗 线段树空间需要开4倍 读入有负数的时候,如果要写快读,要识别负号 持续更新
- numpy学习(四)
练习篇(Part 4) 41. How to sum a small array faster than np.sum? (★★☆) arr = np.arange(10) print(np.add. ...
- Wannafly Camp 2020 Day 2J 邦邦的2-SAT模板
#include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; cout<<n& ...
- PHP 实现微信小程序敏感图片、内容检测接口
主要是为了调用微信小程序msgSecCheck.imgSecCheck接口. 先附上小程序接口说明文档地址:https://developers.weixin.qq.com/miniprogram/d ...
- centos7下编译安装redis5.05
准备环境: 1.一台centos7机器,配置没有什么要求(能联网) 2.下载好redis压缩包 下载redis包: 1.登录redis官网: https://redis.io/download 2.选 ...
- 全排列(dfs-有重复数字)
给出一个字符串S(可能有重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列.例如:S = "1312", 输出为: 1123 1132 1213 1231 131 ...
- 2019-08-10 纪中NOIP模拟B组
T1 [JZOJ1235] 洪水 题目描述 一天, 一个画家在森林里写生,突然爆发了山洪,他需要尽快返回住所中,那里是安全的. 森林的地图由R行C列组成,空白区域用点“.”表示,洪水的区域用“*”表示 ...
- python之路模块
time模块 print time.time() print time.mktime(time.localtime()) print time.gmtime() #可加时间戳参数 print time ...