POJ 1236 Network of Schools(tarjan)题解
题意:一个有向图。第一问:最少给几个点信息能让所有点都收到信息。第二问:最少加几个边能实现在任意点放信息就能传遍所有点
思路:把所有强连通分量缩成一点,然后判断各个点的入度和出度
tarjan算法:问问神奇海螺啥是tarjan
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
const int N=110;
const int MOD=1000;
using namespace std;
int n,index,scc_cnt; //scc_cnt记录SCC
int dfn[N],low[N],sccno[N],in[N],out[N];
vector<int> g[N];
stack<int> s;
void tarjan(int x){
int i;
dfn[x]=low[x]=++index;
s.push(x);
for(i=0;i<g[x].size();i++){
int v=g[x][i];
if(!dfn[v]){ //未走过
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if(!sccno[v]){ //走过且在栈中
low[x]=min(low[x],dfn[v]);
}
}
if(dfn[x]==low[x]){
scc_cnt++; //增加一个大点
int a;
while(1){ //x及以后全部出栈
a=s.top();
s.pop();
sccno[a]=scc_cnt;
if(a==x) break;
}
}
}
void done(){
index=scc_cnt=0;
memset(dfn,0,sizeof(dfn));
memset(sccno,0,sizeof(sccno));
for(int i=0;i<n;i++){
if(!dfn[i]) tarjan(i);
}
}
int main(){
int t;
while(~scanf("%d",&n) && n){
for(int i=0;i<n;i++) g[i].clear();
for(int u=0;u<n;u++){
while(~scanf("%d",&t) && t){
t--;
g[u].push_back(t);
}
}
done();
int ans1,ans2;
for(int i=1;i<=scc_cnt;i++){
in[i]=out[i]=0; //入度出度初始化为无
}
for(int i=0;i<n;i++){
for(int j=0;j<g[i].size();j++){
int v=g[i][j];
if(sccno[i]!=sccno[v]){
in[sccno[v]]=out[sccno[i]]=1; //一个有入度一个有出度
}
}
}
ans1=ans2=0;
for(int i=1;i<=scc_cnt;i++){
if(in[i]==0) ans1++;
if(out[i]==0) ans2++;
}
if(scc_cnt==1) printf("1\n0\n");
else printf("%d\n%d\n",ans1,max(ans1,ans2));
}
return 0;
}
POJ 1236 Network of Schools(tarjan)题解的更多相关文章
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- POJ 1236 Network of Schools (Tarjan + 缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12240 Accepted: 48 ...
- POJ 1236 Network of Schools Tarjan缩点
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22729 Accepted: 89 ...
- 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 ...
- [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(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- 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(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- poj 1236 Network of Schools(tarjan+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
随机推荐
- Html各组件MIME类型
扩展名 类型/子类型 * application/octet-stream 323 text/h323 acx application/internet-property-stream ai appl ...
- li设置float后ul无法包裹li问题解决
解决办法:灰常简单,只需给ul添加样式 ul{ overflow: auto; } 即可
- js-jquery-SweetAlert2【一】使用
概述:SweetAlert2是SweetAlert-js的升级版本,它解决了SweetAlert-js中不能嵌入HTML标签的问题,并对弹出对话框进行了优化,同时提供对各种表单元素的支持,还增加了5种 ...
- js-jquery-SweetAlert【二】配置方法
一.配置 Argument Default value 含义 Description title null (required) 模态对话框的标题.它可以在参数对象的title参数中设置,也可以在 ...
- 不需要更多JS框架了
现在,JavaScript框架已成为Web项目开发不可或缺的一部分.那是因为很长一段时间以来,各种浏览器之间有很大的差别,人们不得不编写框架对此进行屏蔽.问题在于,各种浏览器甚至在基本问题上都难以取得 ...
- SVN出现xcrun: error: invalid active developer path(Mac)
Mac升级了系统,配置PHPStorm的SVN,出现如下错误: 具体提示的内容是:xcrun: error: invalid active developer path (/Library/Devel ...
- (转)Elasticsearch 的坑爹事——记录一次mapping field修改过程
Elasticsearch 的坑爹事 本文记录一次Elasticsearch mapping field修改过程 团队使用Elasticsearch做日志的分类检索分析服务,使用了类似如下的_mapp ...
- -webkit-line-clamp超过两行就出现省略号
-webkit-line-clamp 是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS 规范草案中. 限制在一个块元素显示的文本的行数. 为了实现该 ...
- 【环境变量】Linux 下三种方式设置环境变量
1.在Windows 系统下,很多软件安装都需要配置环境变量,比如 安装 jdk ,如果不配置环境变量,在非软件安装的目录下运行javac 命令,将会报告找不到文件,类似的错误. 2.那么什么是环境变 ...
- excel 批量在一列数据添加单引号以及逗号
在A列后插入一列B1输入="'"&a1&"'," 然后向下填充 就ok 了 向下填充:选中上方连续单元格,鼠标放在选中区域右下角处(会显示“十” ...